Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing relation in model causes dirty state to change to transient #11042

Closed
nsossonko opened this issue Oct 21, 2015 · 7 comments
Closed

Comments

@nsossonko
Copy link
Contributor

Code:

  class Parent extends \Phalcon\Mvc\Model {
    public $id;
    public function initialize() {
      $this->hasMany('id', 'Child', 'parent', array(
        'foreignKey' => array('action' => Relation::ACTION_RESTRICT),
        'alias'  => 'relatedchilds'));
    }
  }
  class Child extends \Phalcon\Mvc\Model {
    public $parent;
    public $id;
    public function initialize() {
      $this->belongsTo('parent', 'Parent', 'id', array(
          'alias' => 'relatedparent',
          'foreignKey' => array('message' => 'Must have parent')
      ));
    }
  }

  $children = Child::find(); // get all child records
  foreach ($children as $child) {
    echo "Initial state: {$child ->getDirtyState()} <br />";
    $parent = $child ->relatedparent;
    echo "After accessing parent - state: {$child ->getDirtyState()} <br />";
  }

Is this by design? If so, why? This is causing us big headaches in our application, so any help would be appreciated.

@andresgutierrez
Copy link
Contributor

I'm not sure how can this work:

$child = Child::find(); // this returns a resultset
  echo "Initial state: {$child ->getDirtyState()} <br />";
  $parent = $child ->relatedparent; // you can't do this

@nsossonko
Copy link
Contributor Author

What's the problem, I'm grabbing the related parent of the child...?

On Thu, Oct 29, 2015 at 12:27 PM, Andy Gutierrez [email protected]
wrote:

I'm not sure how can this work:

$child = Child::find(); // this returns a resultset echo "Initial state: {$child ->getDirtyState()}
"; $parent = $child ->relatedparent; // you can't do this


Reply to this email directly or view it on GitHub
#11042 (comment).

@andresgutierrez
Copy link
Contributor

Child::find() returns a resultset you can't access the related parent on a resultset.

@nsossonko
Copy link
Contributor Author

Oh, I see what you're saying...it's a mistake in the code. Just assume there's a foreach loop there and this is done on one of the actual models...sorry it was pseudo-code

@nsossonko
Copy link
Contributor Author

Should be clearer now, I updated the code

@nsossonko
Copy link
Contributor Author

Btw, is there any reason the dirty state should change if the property is being set to an object? That itself also doesn't make sense to me...this record is in the database and is going to be updated, why should it be in TRANSIENT state? Setting any other property on the record doesn't change the dirty state...

andresgutierrez added a commit that referenced this issue Nov 20, 2015
#11042 - Fix setting relation should not always cause dirty state change
@nsossonko
Copy link
Contributor Author

This is fixed by PR #11055

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants