Wil*_*del 7 php mysql cakephp cakephp-3.x cakephp-3.3
我正在使用CakePHP 3.3.6和MySQL 5.7.13.
我的数据库(以及其他)中有这三个表:集合,标签和连接表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
Run Code Online (Sandbox Code Playgroud)
标签表
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
在我的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');
}
Run Code Online (Sandbox Code Playgroud)
在我的表\ TagsTable.php中:
public function initialize(array $config)
{
# A Tag belongsToMany Collections
$this->belongsToMany('Collections');
}
Run Code Online (Sandbox Code Playgroud)
我可以获得所有收藏品或所有标签.有用.但是,如果我尝试使用相关标签获取所有集合,则会出现此错误:
在我的Collections控制器中有此错误时会发生此错误:
class CollectionsController extends AppController
{
public function index()
{
$this->set('collections', $this->Collections->find('all', ['contain' => ['Tags']]));
}
}
Run Code Online (Sandbox Code Playgroud)
这在我的Template\Collections\index.ctp中:
<h1>Hi, this is the Collection > Index page.</h1>
<?php foreach ($collections as $collection): ?>
<p>test</p>
<?php endforeach; ?>
Run Code Online (Sandbox Code Playgroud)
我不知道为什么......我尝试创建一个Table\CollectionsTagsTable.php文件,但它并没有什么区别.
谢谢你的帮助
编辑:我尝试通过TIMESTAMP更改DATETIME字段,通过INT更改TINYINT,它没有改变任何东西.
使用类似的设置在本地测试它.看起来像"user_id"
集合表中的主键索引会导致此处出现问题.删除它,问题就消失了.
我实际上并不了解复合键及其在CakePHP3中的用法,所以也许有经验丰富的人可以告诉我,为什么这会失败.
归档时间: |
|
查看次数: |
4863 次 |
最近记录: |