同一模型的多个关系:从 CakePHP 2.x 到 CakePHP 3.x

Multiple relations to the same model: from CakePHP 2.x to CakePHP 3.x

我尝试将此 example 应用于 CakePHP3:tables usersmessages (id, user_id, recipient_id, body) 有两个外键到 id in users.

以下MessagesTable.php中的代码:

    $this->belongsTo('Sender', [
        'foreignKey' => 'user_id',
        'className' => 'Users',
        'propertyName' => 'Sender'
    ]);
    $this->belongsTo('Recipient', [
        'foreignKey' => 'recipient_id',
        'className' => 'Users',
        'propertyName' => 'Recipient'
    ]);

in MessagesController.php in view and index methods:

    $message = $this->Messages->get($id, [
        'contain' => ['Users'],
        'contain' => ['Sender'],
        'contain' => ['Recipient']
    ]);

UsersTable.php中:

    $this->hasMany('MessagesSent', [
        'foreignKey' => 'user_id',
        'className' => 'Messages',
        'propertyName' => 'MessagesSent'

    ]);

    $this->hasMany('MessagesReceived', [
        'foreignKey' => 'recipient_id',
        'className' => 'Messages',
        'propertyName' => 'MessagesReceived'
    ]);

UsersController.php 中的 view 方法:

    $user = $this->Users->get($id, [
        'contain' => ['MessagesSent'],
        'contain' => ['MessagesReceived']        
    ]);

不显示 Related Messages 在此用户 view.ctp:

<h4><?= __('Related Messages') ?></h4>
    <?php if (!empty($user->messages)): ?>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th scope="col"><?= __('Id') ?></th>
            <th scope="col"><?= __('User Id') ?></th>
            <th scope="col"><?= __('Recipient Id') ?></th>
            <th scope="col"><?= __('Body') ?></th>
        </tr>
        <?php foreach ($user->messages as $messages): ?>
        <tr>
            <td><?= h($messages->id) ?></td>
            <td><?= h($messages->user_id) ?></td>
            <td><?= h($messages->recipient_id) ?></td>
            <td><?= h($messages->body) ?></td>

        </tr>
        <?php endforeach; ?>
    </table>

如果我在 UsersTable.php 中只留下 hasMany('Messages'),在 UsersController.php 中只留下 'contain' => ['Messages'],我会在上面的 view 中看到 Related Messages,但显然只有那些与发件人有关的。

在这两种情况下 addedit 消息都会导致错误:Table "App\Model\Table\MessagesTable" is not associated with "Users".

有没有办法在 CakePHP3.x 中解决此问题而不添加连接 table?我感觉我没有正确使用别名,但我不知道具体在哪里。

首先,除非你在MessagesTable.php中有这样的东西:

$this->belongsTo('Users', []);

"Table "App\Model\Table\MessagesTable" is not associated with "Users"" 是正确的。所以在 MessagesController.php 你应该只包含收件人和发件人:

$message = $this->Messages->get($id, [
    'contain' => ['Sender', 'Recipient'],
]);

关于"Related Messages"问题,你应该考虑如果你包含'MessagesSent'和'MessagesReceived',你的实体将不会有一个'messages'数组,而是两个不同的数组称为 'messages_received' 和 'messages_sent'.

一切都按照 Daniel 的建议进行,但重要的是不要

'propertyName' => 'MessagesSent'

'propertyName' => 'MessagesReceived'

UsersTable.php!

上面的 view.ctp 例如发送的消息应如下所示:

<h4><?= __('Related Sent Messages') ?></h4>
<?php if (!empty($user->messages_sent)): ?>
<table cellpadding="0" cellspacing="0">
    <tr>
        <th scope="col"><?= __('Id') ?></th>
        <th scope="col"><?= __('User Id') ?></th>
        <th scope="col"><?= __('Recipient Id') ?></th>
        <th scope="col"><?= __('Body') ?></th>
    </tr>
    <?php foreach ($user->messages_sent as $messages): ?>
    <tr>
        <td><?= h($messages->id) ?></td>
        <td><?= h($messages->user_id) ?></td>
        <td><?= h($messages->recipient_id) ?></td>
        <td><?= h($messages->body) ?></td>

    </tr>
    <?php endforeach; ?>
</table>