Closed bjornpost closed 10 years ago
Pheasant doesn't maintain any internal identity maps. A call to a relationship will always return a brand new object. The new with()
support will still maintain this contract, it will just cache the source data within the collection to prevent multiple queries being fired.
Is this something we would like to solve? Otherwise I'd say this is something that needs a place in the documentation.
Yup, I think this needs to be very clearly documented. Alternately, it's possible we could accomplish this behavior with a per-relationship cache.
A cache might be a good thing, as we don't cache relationships before sending them to our templates. This currently results in a lot of unnecessary trips to the database:
// in controller
$p = \Model\Project::byId(1234);
// in our template (example):
{{ p.id }} {{ p.name }} - {{ p.Client.id }} {{ p.Client.name }}
I believe that the with()
caching in 1.3 will solve this problem for you. Feel free to re-open if it doesn't.
I'd like to reopen this, as it seems like the intended behavior in this issue is actually happening. We have two domain objects Credit
and CreditProduct
, in a typical hasMany and belongsTo relationship.
Suppose CreditProduct #1
belongsTo Credit #1
. The following observed behavior seems to contradict this thread:
# In CreditProduct afterSave, we save the credit to bust caches, etc
# function afterSave(){
# $this->credit->save();
# }
# In a controller:
$credit = Credit::find(1);
$credit->reason = "Hello";
$credit_product = CreditProduct::create(array(
'credit_id' => $credit->id,
'product_id' => 2,
));
# "Hello" has been saved to the db as the credit reason,
# even though the afterSave function in CreditProduct should get a new instance.
I guess the testcase is clear :-).