【问题标题】:CakePHP 3 `setEntityClass()` for a contained table包含表的 CakePHP 3 `setEntityClass()`
【发布时间】:2018-07-30 12:39:58
【问题描述】:

我有什么

  • CakePHP 3.6
  • PostsTablebelongsToAuthorsTable

我想要什么

  1. 选择包含作者的帖子
  2. 在一种特定情况下,为作者使用自定义实体

我尝试过的

谷歌了很多,找不到任何东西。不得不即兴发挥。

// 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


【解决方案1】:

它不起作用,因为contain() 接受一个数组|字符串|null,而您正试图将Table 实例传递给它。将表名传递给它,而不是用于水合:

// 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->getTable());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2017-03-09
    • 2015-06-25
    • 2016-11-23
    • 2015-09-29
    • 1970-01-01
    • 2018-06-27
    相关资源
    最近更新 更多