Cakephp将表链接到一起时的空关联

Cakephp将表链接到一起时的空关联,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,我想这样做: 但它不起作用 当我想显示另一个模型的名称(ciudad name)时,complejo表中的单元格为空。我要显示的是名字,不是城市的Id。 在这里可以看到空列: 所以。。这是我的代码: ComplejosTable.php <?php namespace App\Model\Table; use App\Model\Entity\Complejo; use Cake\ORM\Query; use Cake\ORM\RulesChecker; use Cake\ORM\T

我想这样做:

但它不起作用

当我想显示另一个模型的名称(ciudad name)时,complejo表中的单元格为空。我要显示的是名字,不是城市的Id。 在这里可以看到空列:

所以。。这是我的代码:

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

    最后,ComplejosController.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;
    }
    }
    
    <?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;
    }
    }
    
    <?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']);
    }
    }
    

    我在你重复的问题中给出了答案

    更改CiudadCiudade

    <td><?= $complejo->has('ciudade') ? $this->Html->link($complejo->ciudade->nombreCiudad, ['controller' => 'Ciudades', 'action' => 'view', $complejo->ciudade->id]) : '' ?></td>
    
    
    
    为了防止出现这种错误,我建议您使用英语,在您的示例中,模型应该命名为Cities,然后您可以这样做
    $complejo->has('city')

    谢谢!现在开始工作了!你怎么知道这是单数?有链接吗?试着把
    放在foreach循环中,然后你可以看到这个“Ciudade”对象,你可以理解创建复数单词的英语规则,但不理解西班牙语或语法,然后freamework只需从prulars单词中删除“s”。