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

时间:2017-08-19 21:45:20

标签: php mysql cakephp associations cakephp-3.0

我尝试将此example应用于CakePHP3:表usersmessages (id, user_id, recipient_id, body),并在id中使用两个外键users

MessagesTable.php中的以下代码:

    $this->belongsTo('Sender', [
        'foreignKey' => 'user_id',
        'className' => 'Users',
        'propertyName' => 'Sender'
    ]);
    $this->belongsTo('Recipient', [
        'foreignKey' => 'recipient_id',
        'className' => 'Users',
        'propertyName' => 'Recipient'
    ]);
<{1}} MessagesController.phpview方法中的

index
$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> 中的hasMany('Messages')UsersTable.php中的'contain' => ['Messages'],我确实会在上面的UsersController.php中看到Related Messages,但很明显只有与发件人有关的那些。

在这两种情况下,viewadd条消息都会导致错误:edit

有没有办法在CakePHP3.x中修复此问题而不添加连接表?我有一种感觉,我没有正确使用别名,但我无法确定究竟在哪里。

2 个答案:

答案 0 :(得分:0)

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

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

&#34;表&#34; App \ Model \ Table \ MessagesTable&#34;与&#34;用户&#34;&#34;无关。是正确的。因此,在MessagesController.php中,您应该只包含收件人和发件人:

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

关于&#34;相关消息&#34;问题,你应该考虑如果你包含&#39; MessagesSent&#39;和&#39; MessagesReceived&#39;,您的实体将没有&#39;消息&#39;数组,但两个不同的数组称为“messages_received&#39;和&#39; messages_sent&#39;。

答案 1 :(得分:0)

丹尼尔建议,一切都有效,但重要的是不要

'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>