Symfony: Control Model->save() function to perform INSERT or UPDATE


In Symfony Framework, Model->save() function is clever enough to determine to use INSERT or UPDATE for the query. But i’ve tried many ways to perform an UPDATE for my record but failed. It keep INSERT new record. Finally i found the solution to control the Model->save() function to perform INSERT or UPDATE.

Just a simple example:-
Assume there is a User table where ID is the primary key, and there is a user record with id = 5, now we perform the code below, it should be smart enough to perform UPDATE but NO!, the code below will INSERT a new record.

Advertisements


$user_id = 5;
$status = 1;

$user = new User();
$user->setId($user_id);
$user->setStatus($status);
$user->save(); // this will insert instead of update

Solution:


$user_id = 5;
$status = 1;

$user = new User();
$user->setNew(false); // u need to add this line
$user->setId($user_id);
$user->setStatus($status);
$user->save(); // now it will perform a UPDATE query

Special Thanks




Share this with your friends:-

8 Responses to “Symfony: Control Model->save() function to perform INSERT or UPDATE”

  1. akhilesh says:

    i am using this script in code but it does not work…
    no error was shown by this script. If I use same code for another table it works fine..

    Actually, I have created a new table in database and it doesn’t work that..
    Please anyone help me out of this…
    Its urgent.

  2. Bryan says:

    CkNemo is correct. The problem happens because you are using an unmanaged instance of the User class.

  3. Tapu says:

    Hello All,
    I have a problem of Updating and Interting. It seems my source for Update and Insert cannot work when I want to Insert and Update. That is when I comment out the source of Insert it then work fine for Update, same as when I comment out the surce of Update it then work fine for Insert. I.i., I could not set separately for Insert and Update. I am using symfony version 1.0.22. Below is my source. Will be please if someone can help me. Thanks in advance.

    public function executeSave()
    {
    $new_user = new User();
    $new_user->setId($this->getRequestParameter(‘id’));
    $new_user->setName($this->getRequestParameter(‘name’));
    $new_user->setPassword($this->getRequestParameter(‘password’));
    $new_user->setVersion($this->getRequestParameter(‘version’));
    $new_user->save();

    $c = new Criteria(false);
    $c->add(UserPeer::ID, $this->getRequestParameter(‘id’));
    $pass_change = UserPeer::doSelectOne($c);
    $pass_change->setPassword($this->getRequestParameter(‘password’));
    $pass_change->save();
    return $this->forward(‘uc_samp4’, ‘list’);
    }

  4. Lorcan says:

    Hi!

    Do you guy’s maybe know the solution in symfony-1.4.4?

    Have the same problem, can INSERT, can’t UPDATE…

    Have a simple TABLE cityid, cityname

    $city = new cities();
    $city->cityid = 1;
    $city->cityname = ‘Warsaw’;
    $city->save();

    and nothing happens… when

    $city->cityid = ”

    then it INSERT’s a new row…

    $city->setNew(false); // Does not help…

  5. Sampath says:

    CkNemo’s method was correct. But it was inefficient & his comment is invalid. if you use $user->setNew(false), doesn’t set other values to null, unless you set them to null.

  6. CkNemo says:

    Hi!

    Here’s is the correct method to update your User :

    $user_id = 5;
    $status = 1;

    $user = UserPeer::retrieveByPk($user_id);
    $user->setStatus($status);
    $user->save();

    if you use $user->setNew(false); of course your user will be updated, but let’s say you have some other fields on your user, like username and password for example. If you dont set them, they will be updated to NULL in db, Your user will never be able to authenticate anymore.

  7. yul says:

    nice !
    thx a lot dude, I waisted about 2 hours on this issue before reading your tips.

  8. kyordanov says:

    Very, very helpful! Thank you!

Leave a Reply