2016-06-05 3 views
0

Я хочу, чтобы получить этот результат: Click Me (получить ссылку, чтобы перейти к другой ассоциированной таблицы)Связывание таблиц вместе между двумя моделями в CakePHP

Я после официального учебника по CakePHP для связывания таблиц вместе, но но он не работает.

Моя база данных, как это:

Таблица ciudades:

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

Таблица complejos:

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

Полный столбец ciudad в моей таблице дополнений пуст, когда я хочу показать имя другой модели асоциации (ciudad nombreCiudad). Я хочу показать имя, а не Ид Сьюдад. Здесь вы можете увидеть пустую колонку: Click Me 2

Когда я хочу, чтобы показать результат, в index.ctp "$ complejo-> имеет ('СЬЮДАД')" возвращает ложь:

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

Что я могу сделать, чтобы решить мою проблему? Спасибо за помощь.

+0

Возможный дубликат of [Cakephp empty Associations on Linking Tables Вместе] (HTTP: // StackOverflow.com/questions/37625943/cakephp-empty-association-on-linking-tables-together) –

ответ

0

просто изменить Ciudad для ciudade

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

Для CakePHP сингулярные Ciudades является Ciudade не Сьюдад

0

Ваш

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

Кажется быть ссылки user_id, который не доступен в вашем CiudadesTable. Вы можете изменить его на

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

Спасибо за ответ, извините, я ошибся с этой частью кода, я исправил его, но все еще не работал –

Смежные вопросы