What am i doing wrong here, or is this some kind of Bug?
On 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',
]);
}
On Controller
/**
* 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']);
}
Error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'Tickets'
If you are using SQL keywords as table column names, you can enable identifier quoting for your database connection in config/app.php.
SQL Query:
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 Tickets__registar_id
, Tickets.type_id AS Tickets__type_id
, Tickets.origin_id AS Tickets__origin_id
, Tickets.origin_date AS Tickets__origin_date
, Tickets.process_id AS Tickets__process_id
, Tickets.activity_id AS Tickets__activity_id
, Tickets.category_id AS Tickets__category_id
, Tickets.supplier_id AS Tickets__supplier_id
, Tickets.safety_id AS Tickets__safety_id
, Tickets.priority_id AS Tickets__priority_id
, Tickets.approved AS Tickets__approved
, Tickets.approve_user_id AS Tickets__approve_user_id
, Tickets.close_date AS Tickets__close_date
, Tickets.close_user_id AS Tickets__close_user_id
, Tickets.comment AS Tickets__comment
, Tickets.status_id AS Tickets__status_id
, Tickets.modified_user_id AS Tickets__modified_user_id
, Tickets.modified AS Tickets__modified
, Tickets.created AS Tickets__created
, Users.id AS Users__id
, Users.name AS Users__name
, Users.username AS Users__username
, Users.password AS Users__password
, Users.email AS Users__email
, Users.active AS Users__active
, Users.modified AS Users__modified
, Users.created AS Users__created
FROM tickets Tickets LEFT JOIN tickets Tickets ON Tickets.id = (Tickets.parent_id) LEFT JOIN users Users ON Users.id = (Tickets.registar_id) WHERE Tickets.id = :c0 LIMIT 1
This should work out of the box, the joined in tables should be aliased properly as Registars
and Parents
, and for sure this worked until recently, so yes, this is a bug, possibly related to https://github.com/cakephp/cakephp/pull/5836
Please report this as an issue over at GitHub.
Udate Actually it seems that this has already been reported: https://github.com/cakephp/cakephp/issues/5882