How to load a table object in another table object?

In CakePHP 4.0 I need to use a table object in another table object. How can I do it ? Thx

There’s at least 3 different ways to go about that. Can you give more specifics? Most importantly, at the two tables associated with each other?

Non, it is not associated, i need smth like $articles = $this->loadModel(ā€˜Users’) in Model not in Controller

If they’re not associated, you could use:

$this->TableName = TableRegistry::getTableLocator()->get(ā€˜TableName’);

Most of the time I need to use this, I realize that I’m writing code in the wrong place.

1 Like

As @mvojwe indicates, the table locator is one way. In fact the LocatorAwareTrait makes this more convenient and testable than the static TableRegistry

class SomeTable extends Table
{
   use LocatorAwareTrait;

   public function __construct() {
      $this->ExtraTable = $this->fetchTable('Extra');
}
1 Like

This! From an architectural perspective, accessing non-associated tables from within tables usually indicates a possible flaw in your application logic.

1 Like

How do I access some information about another table that is associated to the table I am currently in?

Asking here because this is the first result in search.

My use case: I need to ->join() a table that’s already associated and I do not want to hardcode its database table name, instead I want to just use ->getTable()

UPDATE Figured I can do $this->getAssociation('Alias') when inside a Table class and take it from there

You should really start a new topic for this, including more details about your use case. ā€œAccess some information about another tableā€ is extremely vague. Show us what you’ve tried, and explain what’s not working about it.