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

Phalcon save method not working

Hello,

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 above 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



98.9k
Accepted
answer

You have to check what validation messages are generated by the ORM:

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 {

    $messages = '';
    foreach ($user->getMessages() as $message) {
        $messages .= $message->getMessage();
    }

    $this->flash->error('An error occured, unable to register. Reason: ' . $messages);
    return $this->response->redirect('');
}

I'll give it a go!

Thanks that worked, told me I had to explicitly define the fields to save if they are defined.