Cakephp空关联在一起链接表 [英] Cakephp empty Associations on Linking Tables Together

查看:272
本文介绍了Cakephp空关联在一起链接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做: http://book.cakephp.org/ 3.0 / en / orm / associations.html1
但它不工作
当我想显示另一个模型(ciudad名称)的名称时,我的complejo表中的单元格是空的。我想显示的名称,而不是Id的城市。
在这里您可以看到空列:
点击我

So ..这是我的代码:

So.. This is my code:

ComplejosTable.php b
$ b

ComplejosTable.php

<?php
namespace App\Model\Table;

use App\Model\Entity\Complejo;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
* Complejo Model
*
* @property \Cake\ORM\Association\BelongsTo $Ciudades
*/
class ComplejosTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('complejo');
    $this->displayField('idComplejo');
    $this->displayField('nombre');
    $this->displayField('descripcion');
    $this->displayField('nombreUsuario');
    $this->displayField('contrasenia');
    $this->displayField('direccion');
    $this->displayField('latitud');
    $this->displayField('longitud');
    $this->displayField('telefono');
    $this->displayField('telefono2');
    $this->displayField('vestuario');
    $this->displayField('asador');
    $this->displayField('estacionamiento');
    $this->displayField('requiereSenia');


    $this->belongsTo('Ciudades', [
        'foreignKey' => 'ciudadFK',
        'joinType' => 'INNER'
    ]);
}

/**
 * Default validation rules.
 *
 * @param \Cake\Validation\Validator $validator Validator instance.
 * @return \Cake\Validation\Validator
 */
public function validationDefault(Validator $validator)
{
    $validator
        ->add('id', 'valid', ['rule' => 'numeric'])
        ->allowEmpty('id', 'create');

    $validator
        ->requirePresence('nombre', 'create')
        ->notEmpty('nombre');

    $validator
        ->requirePresence('nombreUsuario', 'create')
        ->notEmpty('nombreUsuario');

    $validator
        ->requirePresence('contrasenia', 'create')
        ->notEmpty('contrasenia');

    $validator
        ->requirePresence('direccion', 'create')
        ->notEmpty('direccion');

    $validator
        ->requirePresence('latitud', 'create')
        ->notEmpty('latitud');

    $validator
        ->requirePresence('longitud', 'create')
        ->notEmpty('longitud');

    $validator
        ->requirePresence('vestuario', 'create')
        ->notEmpty('vestuario');

    $validator
        ->requirePresence('asador', 'create')
        ->notEmpty('asador');

    $validator
        ->requirePresence('estacionamiento', 'create')
        ->notEmpty('estacionamiento');

    $validator
        ->requirePresence('requiereSenia', 'create')
        ->notEmpty('requiereSenia');

    return $validator;
}

/**
 * Returns a rules checker object that will be used for validating
 * application integrity.
 *
 * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
 * @return \Cake\ORM\RulesChecker
 */
public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->existsIn(['ciudadFK'], 'Ciudades'));
    return $rules;
}
}

CiudadesTable.php / p>

CiudadesTable.php

<?php
namespace App\Model\Table;

use App\Model\Entity\Ciudad;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Ciudades Model
*
* @property \Cake\ORM\Association\HasMany $Complejos
*/
class CiudadesTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);
    $this->table('ciudad');
    $this->displayField('nombreCiudad');
    $this->primaryKey('idCiudad');
    //$this->addBehavior('Timestamp');

    $this->hasMany('Complejos', [
    'foreignKey' => 'ciudadFK'
    ]);
}

/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
    $validator
        ->add('idCiudad', 'valid', ['rule' => 'numeric'])
        ->allowEmpty('idCiudad', 'create');

    $validator
        ->requirePresence('nombreCiudad', 'create')
        ->notEmpty('nombreCiudad');

       /*$validator
            ->add('email', 'valid', ['rule' => 'email'])
            ->requirePresence('email', 'create')
            ->notEmpty('email');

        $validator
            ->requirePresence('password', 'create')
            ->notEmpty('password');*/

    return $validator;
}

/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->isUnique(['nombreCiudad']));
    return $rules;
}
}

Index.ctp Complejos

<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
    <li class="heading"><?= __('Actions') ?></li>
    <li><?= $this->Html->link(__('Nuevo Complejo'), ['action' => 'add']) ?>    </li>
    <li><?= $this->Html->link(__('Listar Complejos'), ['controller' => 'Users', 'action' => 'index']) ?></li>
</ul>
</nav>
<div class="posts index large-9 medium-8 columns content">
<h3><?= __('Complejos') ?></h3>
<table cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th><?= $this->Paginator->sort('idComplejo', 'Nº Complejo') ?></th>
            <th><?= $this->Paginator->sort('nombre', 'Nombre Complejo') ?></th>
            <th><?= $this->Paginator->sort('ciudadFK', 'Ciudad') ?></th>
            <th class="actions"><?= __('Acciones') ?></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('ciudades') ? $this->Html->link($complejo->ciudad->nombreCiudad, ['controller' => 'Ciudades', 'action' => 'view', $complejo->ciudad->idCiudad]) : '' ?></td>
            <td class="actions">
                <?= $this->Html->link(__('Ver'), ['action' => 'view', $complejo->id]) ?>
                <?= $this->Html->link(__('Editar'), ['action' => 'edit', $complejo->id]) ?>
                <?= $this->Form->postLink(__('Eliminar'), ['action' => 'delete', $complejo->id], ['confirm' => __('Desea eliminar el complejo # {0}?', $complejo->nombre)]) ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<div class="paginator">
    <ul class="pagination">
        <?= $this->Paginator->prev('< ' . __('atrás')) ?>
        <?= $this->Paginator->numbers() ?>
        <?= $this->Paginator->next(__('siguiente') . ' >') ?>
    </ul>
    <p><?= $this->Paginator->counter() ?></p>
</div>
</div>

最后,ComplejosController.php

<?php
namespace App\Controller;

use App\Controller\AppController;

/**
* Complejos Controller
*
* @property \App\Model\Table\ComplejosTable $Complejos
*/
class ComplejosController extends AppController
{

/**
 * Index method
 *
 * @return void
 */
public function index()
{     


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


/**
 * View method
 *
 * @param string|null $id Complejo id.
 * @return void
 * @throws \Cake\Network\Exception\NotFoundException When record not found.
 */
public function view($id = null)
{
    $complejo = $this->Complejos->get($id, [
        'contain' => ['Ciudades']
    ]);
    $this->set('complejo', $complejo);
    $this->set('_serialize', ['complejo']);
}

/**
 * Add method
 *
 * @return void Redirects on successful add, renders view otherwise.
 */
public function add()
{
    $complejo = $this->Complejos->newEntity();
    if ($this->request->is('complejo')) {
        $complejo = $this->Complejos->patchEntity($complejo, $this->request->data);
        if ($this->Complejos->save($complejo)) {
            $this->Flash->success(__('El complejo se ha guardado con éxito.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('El complejo no se ha guardado. Por favor intente de nuevo.'));
        }
    }
    $ciudades = $this->Complejos->Ciudades->find('list', ['limit' => 200]);
    $this->set(compact('complejo', 'ciudades'));
    $this->set('_serialize', ['complejo']);
}

/**
 * Edit method
 *
 * @param string|null $id Post id.
 * @return void Redirects on successful edit, renders view otherwise.
 * @throws \Cake\Network\Exception\NotFoundException When record not found.
 */
public function edit($id = null)
{
    $complejo = $this->Complejos->get($id, [
        'contain' => []
    ]);
    if ($this->request->is(['patch', 'complejo', 'put'])) {
        $complejo = $this->Complejos->patchEntity($complejo, $this->request->data);
        if ($this->Complejos->save($complejo)) {
            $this->Flash->success(__('El complejo se ha guardado con éxito.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('El complejo no se ha guardado. Por favor intente de nuevo.'));
        }
    }
    $ciudades = $this->Complejos->Ciudades->find('list', ['limit' => 200]);
    $this->set(compact('complejo', 'ciudades'));
    $this->set('_serialize', ['complejo']);
}

/**
 * Delete method
 *
 * @param string|null $id Post id.
 * @return \Cake\Network\Response|null Redirects to index.
 * @throws \Cake\Network\Exception\NotFoundException When record not found.
 */
public function delete($id = null)
{
    $this->request->allowMethod(['complejo', 'delete']);
    $complejo = $this->Complejos->get($id);
    if ($this->Complejos->delete($complejo)) {
        $this->Flash->success(__('El complejo se ha eliminado.'));
    } else {
        $this->Flash->error(__('El complejo no se ha podido eliminar. Por favor intente de nuevo.'));
    }
    return $this->redirect(['action' => 'index']);
}
}

感谢您的帮助!

推荐答案

我在您的重复问题中提供了答案

I put an answer in your duplicate question

在Cakephp的两个模型之间建立表格

Ciudade 更改 Ciudad

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

为了防止这种错误,我建议你使用英语,在你的例子中,那么你可以做
$ complejo-> has('city')

To prevent this errors I suggest you use english language, in your example, the model shoud named Cities and then you can do $complejo->has('city')

这篇关于Cakephp空关联在一起链接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆