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

Problem to save an attribute.

Hello, I have a problem when I save a USER in database. The field PROFILEID don't works. I can save other attributes, but this one I can't.

I don't know what's the problem.

I put below the related codes.

    public function createAction()
    {

        if ($this->request->isPost()) {

            $user = new Users();

            $user->assign(array(
                'name' => $this->request->getPost('name', 'striptags'),
                'profiles_id' => $this->request->getPost('profilesId', 'int'),
                'email' => $this->request->getPost('email', 'email')
            ));

            if (!$user->save()) {
                $this->flash->error($user->getMessages());
            } else {

                $this->flash->success("User was created successfully");

               //Tag::resetInput();
                   return $this->dispatcher->forward(array(
                    'action' => 'search',
                    'params' => $this->dispatcher->getParams()
                ));

            }
        }else{
            $this->view->form = new UsersForm(null);
        }
    }

<?php namespace Apponte\Models;

use Phalcon\Mvc\Model;

/**

  • Apponte\Models\Profiles
  • All the profile levels in the application. Used in conjenction with ACL lists */ class Profiles extends Model {

    /**

    • ID
    • @var integer */ public $id;

    /**

    • Name
    • @var string */ public $name;

    /**

    • Define relationships to Users and Permissions */ public function initialize() { $this->hasMany('id', 'Apponte\Models\Users', 'profilesId', array( 'alias' => 'users', 'foreignKey' => array( 'message' => 'Profile cannot be deleted because it\'s used on Users' ) ));

      $this->hasMany('id', 'Apponte\Models\Permissions', 'profilesId', array( 'alias' => 'permissions' )); } }

<?php namespace Apponte\Models;

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Uniqueness;

class Users extends Model
{
    const STATUS_INACTIVE   =   0;
    const STATUS_ACTIVE     =   1;
    const STATUS_SUSPENDED  =   2;
    const STATUS_BANNED     =   3;
    const STATUS_DELETED    =   4;

    public $id;
    public $profilesId=99;
    public $name;
    public $email;
    public $password;
    public $facebook_id;
    public $facebook_name;
    public $facebook_data;
    public $gplus_id;
    public $gplus_name;
    public $gplus_data;
    public $twitter_id;
    public $twitter_name;
    public $twitter_data;
    public $created_at;
    public $hits;
    public $status = 0;

    public function getSource()
    {
        return 'users';
    }

    public function initialize()
    {

       /* $this->hasMany('id', 'Apponte\Models\Logs', 'user_id', array(
            'alias' => 'logs',
            'reusable' => true,
            'foreignKey' => array(
                'action' => \Phalcon\Mvc\Model\Relation::ACTION_CASCADE,
            ),
        ));*/

        $this->belongsTo('profilesId', 'Apponte\Models\Profiles', 'id', array(
            'alias' => 'profile',
            'reusable' => true
        ));
    }

    public function beforeValidationOnCreate()
    {
        if (empty($this->password))
        {
            $tempPassword = preg_replace('/[^a-zA-Z0-9]/', '', base64_encode(openssl_random_pseudo_bytes(12)));
            $this->password = $this->getDI()->getSecurity()->hash($tempPassword);
        }

        if (empty($this->status)) {
            $this->status == static::STATUS_INACTIVE;
        }

        if (empty($this->created_at)) {
            $this->created_at = new \Phalcon\Db\RawValue('now()');
        }
    }

    public function validation()
    {
        $this->validate(new Uniqueness(array(
            'field' => 'email',
            'message' => 'Já existe uma conta com este e-mail.'
        )));

        return true !== $this->validationHasFailed();
    }
}


3.8k
Accepted
answer

Solved. The cache was out of date.