【问题标题】:Multiple relations to the same model: from CakePHP 2.x to CakePHP 3.x同一模型的多个关系:从 CakePHP 2.x 到 CakePHP 3.x
【发布时间】:2017-08-19 21:45:19
【问题描述】:

我尝试将此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'
    ]);

MessagesController.phpviewindex 方法中:

    $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.phpview 方法中:

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

不要在这个用户view.ctp中显示Related Messages

<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'contain' =&gt; ['Messages'] 中留下hasMany('Messages')UsersController.php 中,我确实在上面的view 中看到Related Messages,但显然只有那些与发件人相关的。

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

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

【问题讨论】:

    标签: php mysql cakephp associations cakephp-3.0


    【解决方案1】:

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

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

    “表“App\Model\Table\MessagesTable”未与“Users”关联是正确的。所以在 MessagesController.php 你应该只包含收件人和发件人:

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

    关于“相关消息”问题,您应该考虑,如果您包含“MessagesSent”和“MessagesReceived”,您的实体将不会有“messages”数组,而是两个不同的数组,分别称为“messages_received”和“messages_sent”。

    【讨论】:

    • 谢谢!错误消失了,我可以添加和编辑消息,这很棒。我已经在 view.ctp 上面的“messages”更改为“messages_sent”,但“相关消息”中仍然没有出现任何内容。我还应该更改消息实体本身和/或控制器中的任何内容吗?
    • 您好文森特,我不知道我是否正确理解了您提出的问题。据我所知,没有什么可以改变的。
    【解决方案2】:

    一切都按照丹尼尔的建议进行,但重要的是不要有

    'propertyName' =&gt; 'MessagesSent'

    'propertyName' =&gt; '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>
    

    【讨论】:

      猜你喜欢
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多