gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:56:32 28 4
gpt4 key购买 nike

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

我有这三个表:

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

关系

对于表“dogs”的每个ennuple,表“dogs_cats”可能有一个或多个ennuples:“dogs”有许多“dogs_cats”和“dogs_cats”属于“dogs”

对于表“cats”的每个ennuple,表“dogs_cats”可能有一个或多个ennuples:“cats”有许多“dogs_cats”和“dogs_cats”属于“cats”

源> 型号 >表> 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'
]);
}


}
?>

源> 型号 >表> 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'
]);
}


}
?>

源> 型号 >表> DogsCatsTable.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',
]);
}


}
?>

如何从 Controller DogsCatsController.php 中获取表狗和猫的字段?

最佳答案

如果你的关联规则没问题,那么
您可以简单地通过在 contains(change as per your need) 中使用以下内容来实现:

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

关于php - cakephp 显示 ID 显示 table.name 来自两个不同表的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59141547/

28 4 0