我正在使用CakePHP 3.3.6和MySQL 5.7.13。
我的数据库中有这三个表(以及其他表):collections,tags和连接表collections_tags。
收藏表。
在我的Table\CollectionsTable.php文件中:
在我的Table\TagsTable.php文件中:
我可以获取所有的收藏夹或标签,这个没问题。 但是如果我尝试获取所有带有它们关联标签的收藏夹,我会遇到这个错误: 这个错误发生在我的 Collections 控制器中有以下代码时:
我不知道为什么...我尝试创建一个Table\CollectionsTagsTable.php文件,但没有任何变化。
谢谢你的帮助。
编辑:我尝试将DATETIME字段更改为TIMESTAMP,并将TINYINT更改为INT,但没有任何变化。
我的数据库中有这三个表(以及其他表):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']]));
}
}
而这是我在模版\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文件,但没有任何变化。
谢谢你的帮助。
编辑:我尝试将DATETIME字段更改为TIMESTAMP,并将TINYINT更改为INT,但没有任何变化。
id
是自增的,那么使用PRIMARY KEY (id, ...)
几乎总是“错误”的。 - Rick James