【发布时间】:2018-07-30 12:39:58
【问题描述】:
我有什么
- CakePHP 3.6
-
PostsTablebelongsToAuthorsTable
我想要什么
- 选择包含作者的帖子
- 在一种特定情况下,为作者使用自定义实体
我尝试过的
谷歌了很多,找不到任何东西。不得不即兴发挥。
// in my controller action:
// set custom class for authors
$authorsTable->setEntityClass('App\Model\Entity\AuthorCustom');
// get authors
$results = $authorsTable->find('all');
效果很好。
// in my controller action:
// set custom class for authors
$authorsTable->setEntityClass('App\Model\Entity\AuthorCustom');
// get products including categories
$results = $postsTable->find('all')->contain($authorsTable);
这不起作用,因为contain() 不接受表实例。
有没有人知道在一个 find() 调用中设置不同实体的方法?
【问题讨论】:
-
为什么你首先需要这样做?这要解决什么问题?
-
@ndm 我需要实体在为 Google API 端点准备时表现不同。我不想用那种逻辑把主要实体弄得乱七八糟,所以考虑为此使用一个单独的实体
标签: cakephp associations cakephp-3.0 model-associations contain