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