Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Saving a model updates all the records !

Hi all,

I'm just starting to work with Phalcon models/ORM and I'm encoutering a really weird issue (Phalcon 2.05). I have a user model, no relation with other models for now, and I just try to update a set of rows as a test. Problem: all my rows end up being updated with the same value (the last one in the loop).

Here is the code:

    $aoUsers = $oUser::find(array('order' => 'id DESC'));
    if(!$aoUsers)
    {
        dump('Error');
    }
    else
    {
            // Traversing with a while
            $aoUsers->rewind();
            while ($aoUsers->valid()) 
            {
                $oUser = $aoUsers->current();

                dump('update 1 user Model');
                $sEmail = 'updatedEmail'.rand(9999,9999999).'@email.com';
                dump($sEmail);
                $oUser->email = $sEmail;
                if(!$oUser->save())
                {
                    dump('error saving user');
                    dump($oUser->getMessages());
                }

                $aoUsers->next();
            }
    }

Am I doing something wrong ? Or is there a really weird thing going on in the model ? Cheers



2.1k
Accepted
answer

My bad, I've found out the problem. My DB table was missing the primarykey/autoincrement, and as such all the rows had the same ID. So naturally Phalcon was updated all the rows.