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.

Updating a single record

Hey all,

my problem is when I try to update a single record, than it dosent update the record. Here is my code that I use in the model.

        public static function updateProfile($user_id) {
            $profile = self::findFirst(array('mUser_id = :user_id:', 'bind' => array('user_id' => $user_id)));
            $profile->setVisits($profile->getVisits() + 1);
            $profile->update();
        }

Thx for help :)

I have same problem, except mine says $profile (my equivalent object) does not have update() method, because it is resultset simple, or result row. Resorted to using raw sql ($this->db->query($sql)), bypassing ORM, i remember it working correctly when following the $robots tutorial, would appreciate answer to this also.



46.7k

Perhaps you could run something like this:

        public static function updateProfile($user_id) {
//            $profile = self::findFirst(array('mUser_id = :user_id:', 'bind' => array('user_id' => $user_id)));
//            $profile->setVisits($profile->getVisits() + 1);
            $profile = self::findFirstByUserId($user_id);
            $profile->visits = 12;
            if (!$profile->update()) {
              $message = implode("\n", $profile->getMessages());
              error_log($message);
            }
        }

I'm not sure how the camel case and underscore will work though with the findFirstBy* magic methods. If they don't work then try perhaps renaming your field names to camelCase. I'm not sure about that though.



19.5k
public static function updateProfile($user_id) {
    $profile = self::findFirst([
        'conditions' => 'mUser_id = :user_id:',
        'bind' => ['user_id' => $user_id],
        'bindTypes' => [Phalcon\Db\Column::BIND_PARAM_STR]
    ]);

    $profile->setVisits($profile->getVisits() + 1);
    return $profile->update();
}

@Edward Hew - your problem was almost certainly because you were using the find() method, which returns a result set. findFirst() returns just a single object.

@flaver12 I'd suggest following @dschissler 's advice and find a way to see what error messages (if any) are being generated.



4.4k
Accepted
answer

A new day and the problem is solved. Thx for all your input. The Error was that i extended form the false class, so in our application we just use a BaseModel and I think it works now because of this line here

        public function onConstruct() {
        Model::setup(array(    
            'notNullValidations' => false
        ));
    }


46.7k

Thats fine but maybe you shouldn't be calling that static method every time a model is created.

@dschissler Thx for the input :)

Thats fine but maybe you shouldn't be calling that static method every time a model is created.

Yeah - you can call that code in the initialize() method, and it will only be called once per model per page load and save your server a lot of work.