Ich möchte dieses Ergebnis erhalten: Click Me (erhalten einen Link zum anderen assoziierten Tisch zu gehen)Tabellen verknüpfen zusammen zwischen zwei Modellen in CakePHP
Ich bin nach dem offiziellen Tutorial von CakePHP zu Tabellen zusammen Verknüpfung aber aber es funktioniert nicht.
Meine Datenbank ist wie folgt:
Tabelle ciudades:
| id (int 4) PK | nombreCiudad (varchar 60) |
Tabelle Complejos:
| idComplejo (int 11) PK | Nombre (varchar 25) | ciudad_id (int 4) FK |
Die vollständige Ciudad-Spalte in meiner Complejo-Tabelle ist leer, wenn ich den Namen des anderen Assoziationsmodells (ciudad nombreCiudad) anzeigen möchte. Ich möchte den Namen zeigen, nicht die Id von Ciudad. Hier können Sie die leere Spalte sehen: Click Me 2
Als ich das Ergebnis in index.ctp "$ complejo-> hat ('ciudad')" kehrt falsch angezeigt werden soll:
<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>
hier können Sie die Beziehung zwischen ciudades und Complejos sehen 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;
}
und hier ist 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'
]);
}
schließlich in meinem ComplejosController ich habe:
public function index()
{
$this->paginate = [
'contain' => ['Ciudades']
];
$this->set('complejos', $this->paginate());
$this->set('_serialize', ['complejos']);
}
Was kann ich tun, um mein Problem zu lösen? Danke fürs Helfen.
Mögliche doppelte von [CakePHP leere Assoziationen zu Verknüpfungstabellen Zusammen] (http: // stackoverflow.com/questions/37625943/cakephp-leere-associations-on-linking-tables-zusammen) –