Register here!

Forgot password?

ASK NEW QUESTION

Home Page » Active Record » how to check existence in beforeSave?
1 Vote
Vote Con!
Solved!

how to check existence in beforeSave?

active recordbeforeSaveexists

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:

  1. protected function beforeSave(){
  2.      if($this::model()->exists(conditions, params){
  3.           $this = $this::model()->find(conditions, params);
  4.      }
  5. }
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

Answers (9)

  1. 1 vote
    Accepted

    Why don’t you just replace the attributes of this.

    1. $this->setAttributes($this::model()->find(conditions, params)->attributes, false);
    $this->setAttributes($this::model()->find(conditions, params)->attributes, false);

    not tested

    By phreak · 588 days 5 hours 34 mins ago

    Questions: 1 Accepted: 0 ( 0% ) | Reputation: 0

  2. 1 vote

    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

  3. 0 votes

    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

  4. 1 vote

    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.
    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 :) . I use beforeSave only to make a comparison for log/auditing of changes to the record.

    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

  5. 0 votes

    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

  6. 0 votes

    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

© YiiAnswers.com 2011. All rights reserved.

Switch to our mobile site