0

What I have

  • CakePHP 3.6
  • PostsTable belongsTo AuthorsTable

What I want

  1. Select posts containing their authors
  2. In one specific case, use a custom entity for authors

What I've tried

Googled a lot, couldn't find anything. Had to improvise.

// in my controller action:
// set custom class for authors
$authorsTable->setEntityClass('App\Model\Entity\AuthorCustom');
// get authors
$results = $authorsTable->find('all');

That works fine.

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

That doesn't work because contain() doesn't accept a table instance.

Does anyone know a way to set a different entity on just one find() call?

ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57
  • Why exactly do you need to do that in the first place? What's the problem that this is going to solve? – ndm Jul 31 '18 at 08:06
  • @ndm I need the entity to behave differently when I'm preparing it for a Google API endpoint. I did not want to clutter the main entity with that logic, so thought about using a separate entity just for this – ᴍᴇʜᴏᴠ Jul 31 '18 at 10:09

1 Answers1

0

It doesn't work because contain() accepts an array|string|null and you are trying to pass it a Table instance. Pass it the table name instead used for hydration:

// 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());
Derek Fulginiti
  • 333
  • 1
  • 9