我应该将表“狗”和“猫”的ID分别保存在"dogs_cats“中,当看到数据时显示狗和猫的名称。
我有三张桌子:
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);实习前:
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_cats“的含意:”hasMany“dogs_cats和"dogs_cats”belongTo“belongTo”。
对于表“猫”的每一个含意,都可能有一个或多个对表"dogs_cats“的含意:”猫“hasMany "dogs_cats”和“dogs_cats”belongTo“cat”。
src>Model>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>Model>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>Model>Table>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',
]);
}
}
?>如何从控制器DogsCatsController.php中获取表、狗和猫的字段?
发布于 2019-12-02 15:57:26
如果您的关联规则没有问题,那么只需在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']]
]
);发布于 2019-12-03 15:04:05
解决办法:
src>Model>Entity>Dog.php
<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
class Dog extends Entity
{
protected $_accessible = [
'*' => true,
];
}src>Model>Entity>Cat.php
<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
class Cat extends Entity
{
protected $_accessible = [
'*' => true,
];
}src>Model>Entity>DogsCat.php
<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
class DogsCat extends Entity
{
protected $_accessible = [
'*' => true,
];
}src>Model>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>Model>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>Model>Table>DogsCatsTable.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>Controller>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>Template>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>https://stackoverflow.com/questions/59141547
复制相似问题