You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As a forward, I'm working with a database that was designed very poorly (namely the references between tables are not done off PKs in many cases.) As such I have to override column names in most relations, but this hasn't been a problem. The basic structure is:
Creditor
_rowid (PK)
id
Debitor
_rowid (PK)
creditor_id (references Creditor.id)
invoices (just refactored this, hence moving towards using PKs, and more appropriate naming standards)
id (PK)
debtor_id (references Debitor._rowid)
My relationship from Creditor to Invoice is: return $this->hasManyThrough(Invoice::class, Debtor::class, 'creditor_id', 'debtor_id', 'id');
If I lazy load by doing: $creditor = Creditor::find($id); $creditor->invoices
Everything works fine.
If I try it with eager loading: $creditor = Creditor::with('invoices')->find($id);
The invoices relationship on the creditor is empty.
Similarly, calling: $creditor->load('invoices')
does not work.
In investigating the SQL generated, it seems that in the eager-loading situation, the PK of the Creditor table is being used, instead of the 'id' column that I specified.
The text was updated successfully, but these errors were encountered:
I managed to come up with a fix. I modified two lines in HasManyThrough.php as follows:
Line 139: $this->query->whereIn($table.'.'.$this->firstKey, $this->getKeys($models, $this->localKey));
Line 174: $key = isset($this->localKey) ? $model->{$this->localKey} : $model->getKey();
This has seemed to work for my application, though I believe this is the only place I am presently using hasManyThrough, so not positive there isn't a case where this could break.
As a forward, I'm working with a database that was designed very poorly (namely the references between tables are not done off PKs in many cases.) As such I have to override column names in most relations, but this hasn't been a problem. The basic structure is:
Creditor
Debitor
invoices (just refactored this, hence moving towards using PKs, and more appropriate naming standards)
My relationship from Creditor to Invoice is:
return $this->hasManyThrough(Invoice::class, Debtor::class, 'creditor_id', 'debtor_id', 'id');
If I lazy load by doing:
$creditor = Creditor::find($id); $creditor->invoices
Everything works fine.
If I try it with eager loading:
$creditor = Creditor::with('invoices')->find($id);
The invoices relationship on the creditor is empty.
Similarly, calling:
$creditor->load('invoices')
does not work.
In investigating the SQL generated, it seems that in the eager-loading situation, the PK of the Creditor table is being used, instead of the 'id' column that I specified.
The text was updated successfully, but these errors were encountered: