在Cakephp

时间:2016-06-05 20:38:55

标签: php mysql cakephp cakephp-3.0

我想得到这样的结果:Click Me(获取转到其他关联表的链接)

我按照cakephp的官方教程将表连接在一起但是 但它没有用。

我的数据库是这样的:

表ciudades:

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

表格补充:

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

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

当我想显示结果时,在index.ctp中&#34; $ complejo-&gt;有(&#39; ciudad&#39;)&#34; 返回false:< / p>

<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']);
}

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

2 个答案:

答案 0 :(得分:0)

您:

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

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

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

答案 1 :(得分:0)

ciudade

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

对于Cakephp, Ciudades 的单数是 Ciudade 而非 Ciudad

相关问题