我想进行此查询(此处$ownerQuery
是相关部分):
$searchQuery = $this->Tickets
->find('byStatus',['status' =>$status])
->find('byTitle',['queries' => $queries]);
$ownerQuery = $searchQuery
->innerJoinWith(
'Projects.ProjectsTickets', function($q) use( &$userId){
return $q->where(['Tickets.id' => 'ProjectsTickets.ticket_id'])
->where(['Projects.id' => 'ProjectsTickets.project_id'])
->where(['Projects.user_id' => $userId]);
}
);
或者达到相同效果的东西,但是我尝试过的每一个变体最终都与错误无关。
以下是我的协会:
ProjectsTicketsTable:
class ProjectsTicketsTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('projects_tickets');
$this->displayField('project_id');
$this->primaryKey(['project_id', 'ticket_id']);
//$this->belongsTo('Projects', [
// 'foreignKey' => 'project_id',
// 'joinType' => 'INNER'
//]);
//$this->belongsTo('Tickets', [
// 'foreignKey' => 'ticket_id',
// 'joinType' => 'INNER'
//]);
$this->belongsTo('Projects');
$this->belongsTo('Users');
}
}
ProjectsTable:
class ProjectsTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('projects');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Tags', [
'foreignKey' => 'tag_id',
'joinType' => 'INNER'
]);
//$this->belongsToMany('Tickets', [
// 'foreignKey' => 'project_id',
// 'targetForeignKey' => 'ticket_id',
// 'joinTable' => 'projects_tickets'
//]);
$this->belongsToMany('Tickets', [
'through' => 'ProjectTickets',
]);
$this->belongsToMany('Users', [
'foreignKey' => 'project_id',
'targetForeignKey' => 'user_id',
'joinTable' => 'projects_users'
]);
}
}
TicketsTable:
class TicketsTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('tickets');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
//$this->belongsToMany('Projects', [
// 'foreignKey' => 'ticket_id',
// 'targetForeignKey' => 'project_id',
// 'joinTable' => 'projects_tickets'
//]);
$this->belongsToMany('Projects', [
'through' => 'ProjectTickets',
]);
$this->belongsToMany('Comments', [
'foreignKey' => 'ticket_id',
'targetForeignKey' => 'comment_id',
'joinTable' => 'tickets_comments'
]);
$this->belongsToMany('Users', [
'foreignKey' => 'ticket_id',
'targetForeignKey' => 'user_id',
'joinTable' => 'tickets_users'
]);
}
}
我能解决的关联或查询是否有问题?
编辑:通过更改提供的链接NDM,我确实设法摆脱了“无关联错误”。但是,尝试组合表中列的所有查询总是会产生0结果。
例如,应该返回所有故障单的搜索(因为所有故障单都在ProjectsTickets表中)都没有返回任何内容:
$ownerQuery = $searchQuery
->innerJoinWith(
'Projects.ProjectsTickets', function($q) use( &$userId){
return $q->where(['Tickets.id' => 'ProjectsTickets.ticket_id']);
//->where(['Projects.id' => 'ProjectsTickets.project_id'])
//->where(['Projects.user_id' => $userId]);
}
);
但是,如果我只取消注释用户线,则它正确显示所有结果。
编辑2:虽然关联解决方法有效但匹配的解决方案并不会有问题。例如,如果我想进行此查询而不必在Tickets和ProjectsUsers之间建立关联:
$adminQuery = $searchQuery
->distinct()
->matching('ProjectsUsers')
->where([
'Tickets.id = ProjectsTickets.ticket_id',
'ProjectsTickets.project_id = ProjectsUsers.project_id',
'ProjectsUsers.user_id' => $userId,
'ProjectsUsers.role = Admin'
]);
这仍然导致Tickets is not associated with ProjectsUsers