我正在使用 CakePHP 3.3.6 和 MySQL 5.7.13。
我的数据库中有这三个表(其中包括):collections、tags和连接表collections_tags。
集合表
CREATE TABLE IF NOT EXISTS `database`.`collections` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`event_date` DATE NOT NULL,
`url_slug` VARCHAR(45) NOT NULL,
`status` TINYINT(1) NOT NULL DEFAULT 0,
`user_id` INT UNSIGNED NOT NULL,
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`, `user_id`),
INDEX `fk_collections_users1_idx` (`user_id` ASC),
CONSTRAINT `fk_collections_users1`
FOREIGN KEY (`user_id`)
REFERENCES `database`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ENGINE = InnoDB
标签表
CREATE TABLE IF NOT EXISTS `database`.`tags` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`))
ENGINE = InnoDB
collections_tags 表
CREATE TABLE IF NOT EXISTS `database`.`collections_tags` (
`id` INT NOT NULL AUTO_INCREMENT,
`collection_id` INT UNSIGNED NOT NULL,
`tag_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`, `collection_id`, `tag_id`),
INDEX `fk_collections_has_tags_tags1_idx` (`tag_id` ASC),
INDEX `fk_collections_has_tags_collections1_idx` (`collection_id` ASC),
CONSTRAINT `fk_collections_has_tags_collections1`
FOREIGN KEY (`collection_id`)
REFERENCES `database`.`collections` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_collections_has_tags_tags1`
FOREIGN KEY (`tag_id`)
REFERENCES `database`.`tags` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
在我的 Table\CollectionsTable.php 中:
public function initialize(array $config)
{
# A collection hasMany Sets
$this->hasMany('Sets', [
'dependent' => True,
]);
# A Collection belongsTo a User
$this->belongsTo('Users');
# A Collection belongsToMany Tags
$this->belongsToMany('Tags');
}
在我的 Table\TagsTable.php 中:
public function initialize(array $config)
{
# A Tag belongsToMany Collections
$this->belongsToMany('Collections');
}
我可以获得所有收藏或所有标签。有用。但是,如果我尝试获取所有带有关联标签的集合,则会出现此错误:
当我在 Collections 控制器中有此错误时,会发生此错误:
class CollectionsController extends AppController
{
public function index()
{
$this->set('collections', $this->Collections->find('all', ['contain' => ['Tags']]));
}
}
这在我的Template\Collections\index.ctp中:
<h1>Hi, this is the Collection > Index page.</h1>
<?php foreach ($collections as $collection): ?>
<p>test</p>
<?php endforeach; ?>
我不知道为什么......我尝试创建一个 Table\CollectionsTagsTable.php 文件,但它没有任何区别。
谢谢你的帮助
编辑:我尝试通过TIMESTAMP更改DATETIME字段,并通过INT更改TINYINT,它没有改变任何东西。