在Cakephp的两个模型之间链接表 [英] Linking tables together between two models in Cakephp
问题描述
我想获得此结果:点击我(获取链接转到其他关联表) / p>
我遵循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列为空。我想显示的名称,而不是Id的城市。
在这里您可以看到空列:点击我2
当我想显示结果时,在index.ctp $ complejo-> has('ciudad')返回false:
< table cellpadding =0cellspacing =0>
< thead>
< 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' > __('您确定要删除#{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 = [
'contains'=> ['Ciudades']
];
$ this-> set('complejos',$ this-> paginate());
$ this-> set('_ serialize',['complejos']);
}
我可以如何解决我的问题?
只需更改 ciudade p>
< td><?= $ complejo-> has('ciudade')? $ this-> Html-> link($ complejo-> ciudade-> nombreCiudad,['controller'=>'Ciudades','action'=>'view',$ complejo-> ciudade- > id]):''?>< / td>
对于Cakephp, Ciudades 的单数是 Ciudade 不是 Ciudad
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.
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
这篇关于在Cakephp的两个模型之间链接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!