我在这里做错了什么,或者这是某种Bug?
在TicketsTable.php上
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config) {
$this->table('tickets');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp', [
'events' => [
'Tickets.closed' => [
'close_date' => 'always'
],
]]);
$this->belongsTo('Registars', [
'className' => 'Users',
'foreignKey' => 'registar_id'
]);
$this->belongsTo('Parents', [
'className' => 'Tickets',
'foreignKey' => 'parent_id',
]);
}
在控制器上
/**
* View method
*
* @param string|null $id Ticket id.
* @return void
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function view($id = null) {
$ticket = $this->Tickets->get($id, [
'contain' => ['Parents', 'Registars']
]);
$this->set('ticket', $ticket);
$this->set('_serialize', ['ticket']);
}
错误:
错误:SQLSTATE [42000]:语法错误或访问冲突:1066不唯一的表/别名:'票证'
如果您使用SQL关键字作为表列名,则可以在config / app.php中为数据库连接启用标识符引用。
SQL查询:
SELECT Tickets.id AS Tickets__id
,Tickets.parent_id AS Tickets__parent_id
,Tickets.title AS Tickets__title
,Tickets.description AS Tickets__description
,Tickets.registar_id AS {{ 1}},Tickets.type_id AS Tickets__registar_id
,Tickets.origin_id AS Tickets__type_id
,Tickets.origin_date AS Tickets__origin_id
,Tickets.process_id AS Tickets__origin_date
,Tickets.activity_id AS {{ 1}},Tickets.category_id AS Tickets__process_id
,Tickets.supplier_id AS Tickets__activity_id
,Tickets.safety_id AS Tickets__category_id
,Tickets.priority_id AS Tickets__supplier_id
,Tickets.approved AS {{ 1}},Tickets.approve_user_id AS Tickets__safety_id
,Tickets.close_date AS Tickets__priority_id
,Tickets.close_user_id AS Tickets__approved
,Tickets.comment AS Tickets__approve_user_id
,Tickets.status_id AS {{ 1}},Tickets.modified_user_id AS Tickets__close_date
,Tickets.modified AS Tickets__close_user_id
,Tickets.created AS Tickets__comment
,Users.id AS Tickets__status_id
,Users.name AS {{ 1}},Users.username AS Tickets__modified_user_id
,Users.password AS Tickets__modified
,Users.email AS Tickets__created
,Users.active AS Users__id
,Users.modified AS {{ 1}},Users.created AS Users__name
FROM ticket Tickets LEFT JOIN ticket Tickets ON Tickets.id =(Tickets.parent_id)LEFT JOIN users Users ON Users.id =(Tickets.registar_id)WHERE Ti ckets.id =:c0 LIMIT 1
答案 0 :(得分:0)
这应该是开箱即用的,连接的表应该正确地别名为Registars
和Parents
,并且肯定这个工作直到最近,所以是的,这是一个bug,可能是相关的到 https://github.com/cakephp/cakephp/pull/5836
请在GitHub报告此问题。
Udate 实际上似乎已经报道了这一点: https://github.com/cakephp/cakephp/issues/5882
答案 1 :(得分:0)
请按照以下方式更改您所属的关联类名
$this->belongsTo('Registars', [
'className' => 'Registars',
'foreignKey' => 'registar_id'
]);
$this->belongsTo('Parents', [
'className' => 'Parents',
'foreignKey' => 'parent_id',
]);
'className'=> '用户'应为'className'=> 'Registars'
'className'=> '门票',应该是'className'=> '父母'
由于