【问题标题】:Linking tables together between two models in Cakephp在 Cakephp 中将两个模型之间的表链接在一起
【发布时间】:2016-06-05 20:38:54
【问题描述】:

我想得到这个结果:Click Me(获取链接到另一个关联表)

我正在关注 cakephp 的官方教程来将表格链接在一起,但是 但它不起作用。

我的数据库是这样的:

餐桌:

| id (int 4) PK | nombreCiudad (varchar 60)|

表格完成:

| idComplejo (int 11) PK |名词 (varchar 25)| ciudad_id (int 4) FK |

当我想显示另一个关联模型 (ciudad nombreCiudad) 的名称时,complejo 表中的完整 ciudad 列为空。我想显示名称,而不是 Ciudad 的 ID。 这里可以看到空栏:Click Me 2

当我想显示结果时,在 index.ctp "$complejo->has('ciudad')" 返回 false:

<table cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th><?= $this->Paginator->sort('idComplejo') ?></th>
            <th><?= $this->Paginator->sort('nombre') ?></th>
            <th><?= $this->Paginator->sort('ciudad_id') ?></th>
            <th class="actions"><?= __('Actions') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($complejos as $complejo): ?>
        <tr>
            <td><?= $this->Number->format($complejo->idComplejo) ?></td>
            <td><?= h($complejo->nombre) ?></td>
            <td><?= $complejo->has('ciudad') ? $this->Html->link($complejo->ciudad->nombreCiudad, ['controller' => 'Ciudades', 'action' => 'view', $complejo->ciudad->id]) : '' ?></td>
            <td class="actions">
                <?= $this->Html->link(__('View'), ['action' => 'view', $complejo->idComplejo]) ?>
                <?= $this->Html->link(__('Edit'), ['action' => 'edit', $complejo->idComplejo]) ?>
                <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $complejo->idComplejo], ['confirm' => __('Are you sure you want to delete # {0}?', $complejo->idComplejo)]) ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

在这里你可以看到 ciudades 和 complejos 之间的关系 ComplejosTable.php

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('complejos');
    $this->displayField('idComplejo');
    $this->displayField('nombre');


    $this->belongsTo('Ciudades', [
        'foreignKey' => 'ciudad_id',
        'joinType' => 'INNER'
    ]);
}

 public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->existsIn(['ciudad_id'], 'Ciudades'));
    return $rules;
}

这里是 CiudadesTable.php

...


public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('ciudades');
    $this->displayField('nombreCiudad');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->hasMany('Complejos', [
        'foreignKey' => 'ciudad_id'
    ]);



}

最后,在我的 ComplejosController 我有:

public function index()
{

    $this->paginate = [
        'contain' => ['Ciudades']
    ];
    $this->set('complejos', $this->paginate());
    $this->set('_serialize', ['complejos']);
}

我可以做些什么来解决我的问题?感谢您的帮助。

【问题讨论】:

标签: php mysql cakephp cakephp-3.0


【解决方案1】:

只需将 ciudad 更改为 ciudad

<td><?= $complejo->has('ciudade') ? $this->Html->link($complejo->ciudade->nombreCiudad, ['controller' => 'Ciudades', 'action' => 'view', $complejo->ciudade->id]) : '' ?></td>

Ciudades 的 Cakephp 单数是 Ciudade 而不是 Ciudad

【讨论】:

    【解决方案2】:

    你的:

    <th><?= $this->Paginator->sort('user_id') ?></th>
    

    似乎引用了您的 CiudadesTable 中不可用的 user_id。您可能想将其更改为

      <th><?= $this->Paginator->sort('ciudad_id') ?></th>
    

    【讨论】:

    • 感谢您的回答,抱歉这部分代码有错误,我已更正,但仍然无法正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    相关资源
    最近更新 更多