cakephp 显示来自两个具有关系的不同表的 ID 显示 table.name

cakephp displays the ID display table.name from two different tables with relations

我应该在"dogs_cats"中保存table"dogs"和"cats"各自的ID,当看到数据时显示狗和猫的名字。

我有这三个 table:

CREATE TABLE IF NOT EXISTS cats (
  id int(11) NOT NULL,
  name varchar(40) NOT NULL,
  surname varchar(40) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS dogs (
  id int(11) NOT NULL,
  name varchar(40) NOT NULL,
  surname varchar(40) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS dogs_cats (
  id int(11) NOT NULL,
  dog_id int(11) NOT NULL,
  cat_id int(11) NOT NULL,
  info varchar(40) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE dogs
ADD PRIMARY KEY (id),
MODIFY id int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE cats
ADD PRIMARY KEY (id),
MODIFY id int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE dogs_cats
ADD PRIMARY KEY (id),
MODIFY id int(11) NOT NULL AUTO_INCREMENT,
ADD FOREIGN KEY(dog_id) REFERENCES dogs(id),
ADD FOREIGN KEY(cat_id) REFERENCES cats(id);

schema DB

实际例子:

table cats:
id name surname
1  tony may

table dogs:
id name surname
1  pop  gray

table dogs_cats:
id dog_id cat_id info
1  1       1       pop and tony

but visualize:
id name_dog name_cat info
1  pop       tony      pop and tony

关系

对于 table "dogs" 的每个 ennuples 可能有一个或多个 table "dogs_cats": "dogs" hasMany "dogs_cats" and "dogs_cats"属于"dogs"

对于 table "cats" 的每个 ennuples 可能有一个或多个 table "dogs_cats": "cats" hasMany "dogs_cats" and "dogs_cats"属于"cats"

src>型号>Table>DogsTable.php

<?php

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

use Cake\ORM\Table;

class DogsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');


        $this->hasMany('DogsCats',[
            'foreignKey'=>'dog_id'
        ]);
    }


}
?>

src>型号>Table>CatsTable.php

<?php

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

use Cake\ORM\Table;

class DogsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');


        $this->hasMany('DogsCats',[
            'foreignKey'=>'cat_id'
        ]);
    }


}
?>

src>模型>Table>狗CatsTable.php

<?php

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

use Cake\ORM\Table;

class DogsCatsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');


        $this->belongsTo('dogs', [
            'foreignKey' => 'id',
            'joinType'=>'INNER',
        ]);

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


}
?>

如何从控制器 DogsCatsController.php 获取 table 的狗和猫的字段?

如果你的关联规则没问题那么 您只需在 contains 中使用以下内容(根据需要更改)即可:

$query = $this->DogsCats
        ->find('all');
$query->contain(  ['Dogs','Cats']
                );
$results = $query->toArray();
$this->set('results', $results );

如果你只想拥有 id ,那么你可以尝试以下方法:

$query->contain(
                  [
                    'Dogs'=>['fields' => ['id']],
                   'Cats'=>['fields' => ['id']]
                  ]
                );

解决方法:

src>模型>实体>Dog.php

<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Dog extends Entity
{
    protected $_accessible = [
        '*' => true,
    ];

}

src>模型>实体>Cat.php

<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Cat extends Entity
{
    protected $_accessible = [
        '*' => true,
    ];

}

src>模型>实体>狗Cat.php

<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class DogsCat extends Entity
{
    protected $_accessible = [
        '*' => true,
    ];

}

src>模型>Table>DogsTable.php

<?php

namespace App\Model\Table;

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

class DogsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');

        $this->hasMany('DogsCats',[
            'foreignKey'=>'dog_id'
        ]);
    }


}

src>模型>Table>CatsTable.php

<?php
namespace App\Model\Table;

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



class CatsTable extends Table
{
    public function initialize(array $config)
    {

        $this->addBehavior('Timestamp');


        $this->hasMany('DogsCats',[
            'foreignKey'=>'cat_id',
        ]);
    }


}

src>模型>Table>狗CatsTable.php

<?php
namespace App\Model\Table;

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



class DogsCatsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');


       $this->belongsTo('Dogs', [
            'foreignKey' => 'id',
            'joinType'=>'INNER'
        ]);

        $this->belongsTo('Cats', [
            'foreignKey' => 'id',
            'joinType'=>'INNER'
        ]);



    }


}

src>控制器>DogsCatsController.php

<?php

namespace App\Controller;


class DogsCatsController extends AppController
{
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Paginator');
        $this->loadComponent('Flash'); // Include the FlashComponent
    }

    public function index()
    {

        $result = $this->DogsCats->find('all')->contain(['Dogs','Cats']);
        $this->set('farms',$result);

    }


}

src>模板>DogsCats>index.ctp

<h1>Dogs and Cats</h1>


<table class="table table-hover">
    <tr>
        <th>id</th>
        <th>name dog</th>
        <th>name cat</th>
        <th>info</th>

    </tr>

    <!-- Here is where we iterate through our $farms query object, printing out farm info -->

    <?php foreach ($farms as $farm): ?> 

            <?php echo $this->Html->tableHeaders(


            [$farm->id,$farm->dog->name,$farm->cat->name,$farm->info],
            ['class' => 'status'],
            ['class' => 'table-default']
            );

            ?>


    <?php endforeach; ?>

</table>