CakePHP3 - Parent BelongsTo - 数据库错误 PDOException

CakePHP3 - Parent BelongsTo - Database Error PDOException

我哪里做错了,或者这是某种错误?

在 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 不唯一table/alias:'Tickets'

如果您使用 SQL 关键字作为 table 列名称,您可以在 config/app.php.[=43= 中为数据库连接启用标识符引用]

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 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 LEFT JOIN tickets ON Tickets.id = (Tickets.parent_id) LEFT JOIN users 用户 ON Users.id = (Tickets.registar_id) WHERE Tickets.id = :c0 LIMIT 1

这应该是开箱即用的,加入的表应该正确地别名为 RegistarsParents,并且肯定这直到最近才有效,所以是的,这是一个错误,可能与 https://github.com/cakephp/cakephp/pull/5836

有关

请在 GitHub 报告此问题。

Udate 其实好像已经有人报道了: https://github.com/cakephp/cakephp/issues/5882

请按如下方式更改您所属的协会类名

$this->belongsTo('Registars', [
        'className' => 'Registars',
        'foreignKey' => 'registar_id'
    ]);
    $this->belongsTo('Parents', [
        'className' => 'Parents',
        'foreignKey' => 'parent_id',
    ]);
  1. 'className' => 'Users' 应该是 'className' => 'Registars'

  2. 'className' => 'Tickets', 应该是 'className' => 'Parents'

谢谢