Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Updating properties in 'remote' DomainObject is broken #79

Closed

Conversation

bjornpost
Copy link
Collaborator

I guess the testcase is clear :-).

.....................................................F

Time: 3.54 seconds, Memory: 11.50Mb

There was 1 failure:

1) Pheasant\Tests\DomainObjectTest::testReadingRemoteProperty
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Elastigirl'
+'Mr. Incredible'

@@ -179,6 +181,19 @@ public function testArrayAccess()
$this->assertEquals("Joe", $llama['name']);
}

public function testReadingRemoteProperty()
{
$hero = (new Hero(array('alias' => 'Mr. Incredible')))->save();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this syntax will fail in PHP 5.3.

@lox
Copy link
Owner

lox commented Sep 25, 2013

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.

@bjornpost
Copy link
Collaborator Author

Is this something we would like to solve? Otherwise I'd say this is something that needs a place in the documentation.

@lox
Copy link
Owner

lox commented Oct 3, 2013

Yup, I think this needs to be very clearly documented. Alternately, it's possible we could accomplish this behavior with a per-relationship cache.

@bjornpost
Copy link
Collaborator Author

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

@lox
Copy link
Owner

lox commented Apr 18, 2014

I believe that the with() caching in 1.3 will solve this problem for you. Feel free to re-open if it doesn't.

@lox lox closed this Apr 18, 2014
@Jud
Copy link
Collaborator

Jud commented Apr 23, 2014

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.

@bjornpost bjornpost deleted the bug-setting-remote-properties branch April 15, 2015 09:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants