0
我試圖使用CakePHP調用存儲過程。使用CakePHP從MySQL存儲過程中檢索輸出變量
當前,返回值由存儲過程中第一條SQL select語句的第一條記錄集組成。
儘管在存儲過程(即select @project_id into project_id
)中設置了輸出變量,但它並未顯示在查詢結果的var_dump中。
存儲過程:
CREATE DEFINER = 'admin'@'%'
PROCEDURE thebuggenie.cmdb_project_team_init(
IN project_name VARCHAR(200),
IN project_key VARCHAR(200),
IN project_homepage VARCHAR(200),
IN team_name VARCHAR(200),
OUT project_id INT(10))
BEGIN
-- start transaction
start transaction;
-- init variables
set @project_id = 0;
set @team_id = 0;
set @assoc_count = 0;
set @scope_id = 1;
-- select team and set variable
select @team_id := id
from tbg3_teams
where name = team_name;
-- if team_id = 0, insert team and set variable
if @team_id is NULL or @team_id = '' or @team_id = 0 then
-- insert new project
insert into tbg3_teams(ondemand, name, scope) values(0, team_name, @scope_id);
-- set team_id variable
set @team_id = LAST_INSERT_ID();
end if;
-- select project and set variable
select @project_id := id
from tbg3_projects
where name = project_name;
-- if project_id = 0, insert project and set variable
if @project_id is NULL or @project_id = '' or @project_id = 0 then
-- insert project
insert into tbg3_projects (name, locked, use_scrum, `key`, homepage, deleted, owner_team, scope, workflow_scheme_id, issuetype_scheme_id) values(project_name, 0, 1, project_key, project_homepage, 0, @team_id, @scope_id, 1, 1);
-- set project_id variable
set @project_id = LAST_INSERT_ID();
end if;
select @assoc_count := count(*)
from tbg3_projectassignedteams
where uid = @team_id
and project_id = @project_id;
if(@assoc_count = 0 and @project_id > 0 and @team_id > 0) then
insert into tbg3_projectassignedteams (project_id, role_id, uid, scope) values(@project_id, 35, @team_id, @scope_id);
end if;
-- setup default views
INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (101, 0, 0, @project_id, 2, 1);
INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (102, 0, 0, @project_id, 2, 1);
INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (110, 0, 0, @project_id, 2, 1);
INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (105, 0, 0, @project_id, 2, 1);
INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (106, 0, 0, @project_id, 2, 1);
INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (111, 0, 0, @project_id, 2, 1);
commit;
-- return values
select @project_id INTO project_id;
END
PHP代碼:
$sql = "call thebuggenie.cmdb_project_team_init(";
$sql .= '\''.$results[0]['Asset']['project_name'].'\'';
$sql .= ',\''.$results[0]['Asset']['project_name'].'\'';
$sql .= ',\'\'';
$sql .= ',\''.$results[0]['Repository']['team_name'].'\'';
$sql .= ',@project_id';
$sql .= ');';
$sql .= 'select @project_id as project_id';
var_dump($sql);
$results = $this->Asset->query($sql);
print_r($results);
PHP代碼輸出:
string 'call thebuggenie.cmdb_project_team_init('CMDB','CMDB','','team-app-platforms',@project_id);select @project_id as project_id;'
Array ([0] => Array ([0] => Array ([@team_id := id] => 6)))
注:我還沒有最終確定錯誤陷阱呢。