duanmen1887 2016-06-05 20:38 采纳率: 0%
浏览 7
已采纳

在Cakephp中将表连接在两个模型之间

I want to get this result: Click Me (get a link to go to the other associate table)

I'm following the official tutorial of cakephp to linking tables together but but it's not working.

My database is like this:

Table ciudades:

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

Table complejos:

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

The complete ciudad column in my complejo table is empty when I want to show the name of the another asociation model (ciudad nombreCiudad). I want to show the name, not the Id of Ciudad. Here you can see the empty column:Click Me 2

When I want to show the result, in index.ctp "$complejo->has('ciudad')" returns 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>

Here you can see the relationship between ciudades and 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;
}

And here is 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'
    ]);



}

Finally, in my ComplejosController I have:

public function index()
{

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

What can I do to resolve my problem? Thanks for helping.

  • 写回答

2条回答 默认 最新

  • drutjkpsr67393592 2016-06-06 08:32
    关注

    just change ciudad for ciudade

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

    For Cakephp singular of Ciudades is Ciudade not Ciudad

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?