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

Can't modify related model's data in beforeSave when using saveAll #1765

Closed
ghost opened this issue Oct 11, 2013 · 5 comments
Closed

Can't modify related model's data in beforeSave when using saveAll #1765

ghost opened this issue Oct 11, 2013 · 5 comments
Milestone

Comments

@ghost
Copy link

ghost commented Oct 11, 2013

Created by kennethfrey (at gmail), 5th May 2010. (originally Lighthouse ticket #671):


Post hasMany Comment. Comment belongsTo Post.

$this->data = array(
'Post' => array(
'title' => 'mytitle',
'text' => 'mytext')
'Comment' => array(
0 => array('text' => 'mytext'),
1 => array('text' => 'mytext')
)
)

I am trying to use saveAll($this->data, array('validate' => 'first')) to save a Post and a multiple Comments. I am trying to modify $this->data['Comment'][0]['text'] to a different value. I can set it and debug just before returning true in beforeSave and the data looks correct.

I remove the debug let it save and it doesn't get set. I can change the data from the Comment model beforeSave but then the rest of $this->data isn't available and I need the other information in order to change the value how I want it. Is this a bug or feature?

@ghost
Copy link
Author

ghost commented Oct 11, 2013

7th May 2010, Mark Story said:


Its a feature, as currently saveAll only lets you use beforeSave to modify the record that's about to be saved, not the full data set. I personally don't feel that should change, but its something that could be discussed.

@ghost
Copy link
Author

ghost commented Oct 11, 2013

7th May 2010, kennethfrey (at gmail) said:


Thanks for responding Mark. I don't mind modifying the related data in its own beforeSave i.e. doing the logic in the Comment's beforeSave but when you do that you don't have access to the rest (the Post's data) of this->data. Am I incorrect about that?

@ghost
Copy link
Author

ghost commented Oct 11, 2013

7th May 2010, Mark Story said:


Don't assign tickets to me please. But yes, you won't have access to all of the data being processed by saveAll. You will have any primary keys for the parent record, so you could access the data that way.

@ravage84
Copy link
Member

@markstory I guess you still feel the same about this.
But what about 3.x?
Else, close?

@markstory
Copy link
Member

This is actually implemented in 3.0. The existing implementation of saveAll() was removed and replaced with per entity saves. Since entities are objects the pass by value issues that caused associated data to not be updated in callbacks no longer exist.

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

No branches or pull requests

2 participants