how to check existence in beforeSave?
I’m trying to check the existence before AR save()
scenario is:
if exists, just read old record, and save will just be update
if not exists, like the old days, insert a new record
my code is like this:
- protected function beforeSave(){
- if($this::model()->exists(conditions, params){
- $this = $this::model()->find(conditions, params);
- }
- }
protected function beforeSave(){
if($this::model()->exists(conditions, params){
$this = $this::model()->find(conditions, params);
}
}but failed, seems $this cannot be replace by the find result
so what’s the problem? can this done with other method?
By taker.wu in Active Record · Asked 588 days 6 hours 12 mins ago
Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0
Possibly related Questions
-
Relational AR to json
November 28, 2012 By tah_206207 1 Answer
TAGS
actioncreate ( x 2 )ACTIVERECORD ( x 4 )AJAX ( x 10 )ajaxSubmitButton ( x 4 )AR ( x 2 )cactivedataprovider ( x 2 )CActiveForm ( x 2 )cactiverecord ( x 2 )CGridView ( x 14 )CJuiAutoComplete ( x 3 )CJuiDialog ( x 8 )CListView ( x 8 )controller ( x 7 )Css ( x 2 )CStarRating ( x 2 )CTreeView ( x 2 )database ( x 5 )dialog ( x 2 )dropdownlist ( x 3 )EGMaps ( x 3 )extension ( x 3 )file ( x 2 )form ( x 6 )Giix ( x 2 )gridview ( x 2 )html ( x 2 )javascript ( x 3 )many_many ( x 4 )merge ( x 2 )model ( x 4 )Modules ( x 3 )multilanguage ( x 2 )php ( x 2 )redirect ( x 4 )Registration ( x 3 )relations ( x 5 )renderPartial ( x 8 )session ( x 2 )sorting ( x 3 )sql ( x 4 )url ( x 3 )urlManager ( x 3 )validation ( x 3 )yii ( x 11 )YiiAnswers Site ( x 4 )This website counts Questions and 429 Answers by 17066 registered users.

Why don’t you just replace the attributes of this.
not tested
By phreak · 588 days 5 hours 34 mins ago
Questions: 1 Accepted: 0 ( 0% ) | Reputation: 0
What you are trying to do is a bit strange. You have a record that afterwards you check whether it exists or not onbeforesave. Why can you find it before? As by selecting it your save will update/save automatically… Also, on your function, what are the conditions? and params? where do you get them?
IMHO, I would check whether exists or not before model creation.
By tonydspaniard · 588 days 4 hours 50 mins ago
Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0
to phreak:
yeah, I try to set attribute, but it is a new record, so save() still insert it
I also use $this->isNewRecord = false; still not work
to tonydspaniard:
thx for your advise, before your opinion, I always:
set attributes, find(check exists), and decide to insert/update
now the flow change as yours: find, set attributes, save()
things work perfect, thanks!
By taker.wu · 588 days 3 hours 57 mins ago
Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0
Setting isNewRecord doesn’t change anything. The key is to set value to the primary key – which reflects isNewRecord result. I thought that setting attributes with second param = false(only safe) would do it.
. I use beforeSave only to make a comparison for log/auditing of changes to the record.
Anyway I just tried to answer you question how to do what you asked, although I myself think that tony’s flow is right. I’m also doing it the same way
By phreak · 588 days 3 hours 46 mins ago
Questions: 1 Accepted: 0 ( 0% ) | Reputation: 0
" I use beforeSave only to make a comparison for log/auditing of changes to the record." +1 :)
By tonydspaniard · 587 days 12 hours 57 mins ago
thx phreak, I forgot to thank you before, forgive me :p
I tried tony’s method, I try to find row, set attributes, save()
but, what if the row doesn’t exist? it will return null for me
and I can’t just set attributes for “null”, all I have to do is check again
is the same as my code before, just work flow changes
My thought is to simplify the code, I hope the check code is implicit in the beforeSave()
I just fill attributes, and save it, and I can get an ID, to do the following works
I don’t want to duplicate the “check exists” code in my controller everywhere
sorry phreak, I’m not notice your second param: false, let me try it
or is there a better way to simplify the “check exists” code?
thank you guys
By taker.wu · 588 days 3 hours 34 mins ago
Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0
"what if the row doesn’t exist? it will return null for me". Yes, that's why you only findByPk or attributes if params are received. If not found, then create new and save... Nevertheless, I am happy it worked for you.
By tonydspaniard · 587 days 12 hours 55 mins ago
yeah...I know that, I just want things done automatically thanks for your direction, learn a lot!
By taker.wu · 585 days 22 hours 13 mins ago
wow, thanks phreak, it’s like a magic
you solved my problem, thanks again
By taker.wu · 588 days 3 hours 21 mins ago
Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0