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.
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');
}
This! From an architectural perspective, accessing non-associated tables from within tables usually indicates a possible flaw in your application logic.