We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



33.8k
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.



9.3k
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
        ));
    }

@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.