Я пуст для новых идей - я близок к тому, чтобы бросить полотенце. Доктрина просто не хочет играть со мной.Createquery, leftjoin и innerjoin в Symfony Doctrine
Моя программа создана против веб-интерфейса MySQL и php и основана на www.qdpm.com. На данный момент у меня есть публичная функция для выполнения экспорта элементов в моей таблице «Задачи» в файл csv. Все идет нормально. Но моя цель - также экспортировать комментарии из таблицы «Tasks_comments», связанные с таблицей «Задачи», и пользователя, создавшего комментарий из таблицы «Пользователи». Это легко можно сделать с помощью запроса SQL:
SELECT `tasks`.`description`, `tasks_comments`.`description`, `users`.`name`
FROM tasks, tasks_comments
INNER JOIN users on `tasks_comments`.`created_by` = `users`.`id`
Твердая часть получает это в доктрину - и заставить его работать. Я искал первые 200 страниц в Google и проваливал примерно столько же раз. Если не больше.
У меня есть модуль action.class.php, который включает мою публичную функцию executeExport (полный код: http://pastebin.com/uWqfd4WC) и component.class.php (полный код: http://pastebin.com/NAJRvaBp).
публичная функция executeExport:
public function executeExport(sfWebRequest $request)
{
/*check access*/
if($request->hasParameter('projects_id'))
{
$this->forward404Unless($this->projects = Doctrine_Core::getTable('Projects')->createQuery()->addWhere('id=?',$request->getParameter('projects_id'))->fetchOne(), sprintf('Object projects does not exist (%s).', $request->getParameter('projects_id')));
$this->checkProjectsAccess($this->projects);
$this->checkTasksAccess('view',false,$this->projects);
}
else
{
$this->checkTasksAccess('view');
}
/*Form with checklist on what I want to export*/
$this->columns = array(
'Projects' => t::__('Project Name'),
'id' => t::__('Id'),
'TasksGroups' => t::__('Group'),
'Versions' => t::__('Version'),
'ProjectsPhases' => t::__('Phase'),
'TasksPriority' => t::__('Priority'),
'TasksLabels' => t::__('Label'),
'name' => t::__('Name'),
'TasksStatus' => t::__('Status'),
'TasksTypes' => t::__('Type'),
'assigned_to' => t::__('Assigned To'),
'Users' => t::__('Created By'),
'estimated_time' => t::__('Est. Time'),
'work_hours' => t::__('Work Hours'),
'start_date' => t::__('Start Date'),
'due_date' => t::__('Due Date'),
'progress' => t::__('Progress'),
'created_at' => t::__('Created At'),
'description' => t::__('Description'),
);
$extra_fields = ExtraFieldsList::getFieldsByType('tasks',$this->getUser(),false,array('all'=>true));
foreach($extra_fields as $v)
{
$this->columns['extra_field_' . $v['id']]=$v['name'];
}
$this->columns['url']=t::__('Url');
if($fields = $request->getParameter('fields'))
{
$separator = "\t";
$format = $request->getParameter('format','.csv');
$filename = $request->getParameter('filename','tasks');
header("Content-type: Application/octet-stream");
header("Content-disposition: attachment; filename=" . $filename . "." . $format);
header("Pragma: no-cache");
header("Expires: 0");
$content = '';
foreach($fields as $f)
{
$content .= str_replace(array("\n\r","\r","\n",$separator),' ',$this->columns[$f]) . $separator;
}
$content .= "\n";
if($format=='csv')
{
echo chr(0xFF) . chr(0xFE) . mb_convert_encoding($content, 'UTF-16LE', 'UTF-8');
}
else
{
echo $content;
}
if(strlen($request->getParameter('selected_items')==0)) exit();
/*Query at the time*/
$q = Doctrine_Core::getTable('Tasks')->createQuery('t')
->leftJoin('t.TasksPriority tp')
->leftJoin('t.TasksStatus ts')
->leftJoin('t.TasksLabels tl')
->leftJoin('t.TasksTypes tt')
->leftJoin('t.TasksGroups tg')
->leftJoin('t.ProjectsPhases pp')
->leftJoin('t.Versions v')
->leftJoin('t.Projects p')
->leftJoin('t.Users')
->leftJoin('t.TasksComments')
->whereIn('t.id',explode(',',$request->getParameter('selected_items')));
schema.yml:
Tasks:
connection: doctrine
tableName: tasks
columns:
id:
type: integer(4)
fixed: false
unsigned: false
primary: true
autoincrement: true
projects_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
default: '0'
notnull: true
autoincrement: false
tasks_status_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
tasks_priority_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
tasks_type_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
tasks_label_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
tasks_groups_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
projects_phases_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
versions_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
created_by:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
name:
type: string(255)
fixed: false
unsigned: false
primary: false
default: ''
notnull: true
autoincrement: false
description:
type: string()
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
assigned_to:
type: string(255)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
estimated_time:
type: float()
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
due_date:
type: date(25)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
created_at:
type: timestamp(25)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
tickets_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
closed_date:
type: date(25)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
discussion_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
start_date:
type: date(25)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
progress:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
relations:
Projects:
local: projects_id
foreign: id
type: one
Tickets:
local: tickets_id
foreign: id
type: one
TasksStatus:
local: tasks_status_id
foreign: id
type: one
TasksPriority:
local: tasks_priority_id
foreign: id
type: one
TasksTypes:
local: tasks_type_id
foreign: id
type: one
TasksLabels:
local: tasks_label_id
foreign: id
type: one
TasksGroups:
local: tasks_groups_id
foreign: id
type: one
ProjectsPhases:
local: projects_phases_id
foreign: id
type: one
Versions:
local: versions_id
foreign: id
type: one
Users:
local: created_by
foreign: id
type: one
TasksComments:
local: id
foreign: tasks_id
type: many
TasksComments:
connection: doctrine
tableName: tasks_comments
columns:
id:
type: integer(4)
fixed: false
unsigned: false
primary: true
autoincrement: true
tasks_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
default: '0'
notnull: true
autoincrement: false
created_by:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
tasks_status_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
tasks_priority_id:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
due_date:
type: date(25)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
worked_hours:
type: float()
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
description:
type: string()
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
created_at:
type: timestamp(25)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
progress:
type: integer(4)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
relations:
Tasks:
local: tasks_id
foreign: id
type: one
Users:
local: created_by
foreign: id
type: one
TasksStatus:
local: tasks_status_id
foreign: id
type: one
TasksPriority:
local: tasks_priority_id
foreign: id
type: one
Три таблицы, которые я хочу использовать это:
Задачи (картинка из phpadmin: s2.postimg.org/3sf75m055/web16_meebox_net_db16_meebox_net_commissi_qd.png)
Tasks_comments (картинка из phpadmin: s11.postimg.org/lfrtdnw9v/web16_meebox_net_db16_meebox_net_commissi_qd.png)
Пользователи
Это может быть, возможно, с подзапросом? Или у вас есть волшебная строка кода, которую мне нужно добавить? Просто появилась идея, я играю!
спасибо.