8

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

André Figueira
  • 6,048
  • 14
  • 48
  • 62
  • 2
    Check `$user->getMessages()` for errors like here: http://docs.phalconphp.com/en/latest/reference/models.html#creating-updating-records – jodator Jul 17 '14 at 05:12
  • @jodator Thanks yeah that worked and I got the save to work, want to pop it in an answer and I'll accept it? – André Figueira Jul 17 '14 at 08:46

1 Answers1

17

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

jodator
  • 2,445
  • 16
  • 29