Phalcon save not working

11,228

Checking for errors with $user->getMessages() always helps debugging what's wrong with $model->save().

More info here: http://docs.phalconphp.com/en/latest/reference/models.html#creating-updating-records

Share:
11,228
André Figueira
Author by

André Figueira

I'm an experienced web application developer. I've worked for a large variety of clients some of which are quite high profile including Time Inc, Allianz and a few others. I currently work in London as a Senior Systems Engineer

Updated on June 06, 2022

Comments

  • André Figueira
    André Figueira almost 2 years

    I'm fairly new to Phalcon, I really like it, currently working on my first project in it, however I'm currently coming across an issue with the ORM.

    I seem to be unable to save.

    My Users model currently has a simple table setup

    id (int, PK) name (string) username (string) email (string) active (int) createdDate (int)

    and my model has these defined as properties using the annotations strategy:

    <?php
    
    /**
     * Users class
    *
    * Represents Holla users
    *
     */
    class Users extends Phalcon\Mvc\Model
    {
    
    /**
     * @Primary
     * @Identity
     * @Column(type="integer", nullable=false)
     */
    public $id;
    
    /**
     * @Column(type="string", length=70, nullable=false)
     */
    public $name;
    
    /**
     * @Column(type="string", length=70, nullable=false)
     */
    public $username;
    
    /**
     * @Column(type="string", length=256, nullable=false)
     */
    public $password;
    
    /**
     * @Column(type="integer", nullable=false)
     */
    public $active;
    
    /**
     * @Column(type="integer", nullable=false)
     */
    public $createdDate;
    

    My initialize has the following:

    public function initialize()
    {
    
        $this->setSource('users');
        $this->hasManyToMany('id', 'UsersAttributes', 'userId', 'attributeId', 'Attributes', 'id', array('alias' => 'attributes'));
        $this->hasMany('id', 'Status', 'userId');
        $this->hasMany('id', 'UsersNeighbors', 'userId');
        $this->hasMany('id', 'UsersFriends', 'userId');
        $this->hasMany('id', 'UsersNeighborhoods', 'userId');
    
    }
    

    I then have a simple registration form which I'm pointing to a method in one of my controllers where i do some cvalidation, then attempt to do the save:

            $user = new Users();
    
            $user->name = $name;
            $user->username = strtolower(str_replace(' ', '', $name));
            $user->password = $this->security->hash($password);
    
            if($user->save())
            {
    
                $this->flash->success('Registered successfully!');
    
                $this->session->set('auth', array(
                    'id' => $user->id,
                    'name' => $user->name
                ));
    
                return $this->response->redirect('user/home');
    
            }
            else
            {
    
                $this->flash->error('An error occured, unable to register');
    
                return $this->response->redirect('');
    
            }
    

    I have setup the profiler to save to a log and all that seems to happen when I do a registration is:

    [Wed, 16 Jul 14 21:46:44 +0000][INFO] SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='users'
    [Wed, 16 Jul 14 21:46:44 +0000][INFO] DESCRIBE `users`
    

    Then It just ports me through to the main view with the flash error "An error occured" as I've defined abovem so I'm kind of stuck as to why it isn't creating the new user and just returning false on user save.

    Thanks in advance