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

ORM Problem

Here I have 4 tables Admins, Roles, RolePermissions, RoleResources

Definition below:

Admins

public function initialize() {
    $this->belongsTo('role_id','Mallex\Mvc\Models\Roles','id',array('alias' => 'Role'));
}

Roles

public function initialize() {
    $this->hasMany('id','Mallex\Mvc\Models\Admins','role_id',array('alias' => 'Admins'));
    $this->hasOne('id','Mallex\Mvc\Models\RoleResources','role_id',array('alias' => 'RoleResource'));
    $this->hasOne('id','Mallex\Mvc\Models\RolePermissions','role_id',array('alias' => 'RolePermission'));
}

RolePermissions

public function initialize() {
    $this->belongsTo('role_id','Mallex\Mvc\Models\Roles','id',array('alias' => 'Role'));
}

RoleResources

public function initialize() {
    $this->belongsTo('role_id','Mallex\Mvc\Models\Roles','id',array('alias' => 'Role'));
}

and now, I want to update admin's role_id, it is always returning 1, the role_id field cannot be updated...

Help plz..



12.8k
edited Jul '14
public function updateAction() {
    if($this->request->isPost()) {
        $id = $this->request->getPost('id','int');
        $admin = \Mallex\Mvc\Models\Admins::findFirst(array(
            'conditions' => 'id = :id:',
            'bind' => array(
            'id' => $id
        ),
        'bindType' => array(
        'id' => \Phalcon\Db\Column::BIND_PARAM_INT
    )
));
$admin->role_id = $this->request->getPost('roleid','int');
$admin->salt = \Mallex\Mvc\Extensions\Utils::RandomChars(16,false);
$admin->pwd = sha1(md5(md5(md5($this->request->getPost('pwd','string') . $admin->salt))));
$admin->region_id = $this->request->getPost('regionid','int');
$admin->truename = $this->request->getPost('truename','string');
$admin->sex = $this->request->getPost('sex','int');
$admin->identity = $this->request->getPost('identity','string');
$admin->address = $this->request->getPost('address','string');
if($admin->save()) {
$this->flash->success($this->lang->admin->success);
$this->dispatcher->forward(array(
'controller' => 'success',
'action' => 'show'
));
} else {
$this->flash->success($this->lang->admin->error);
$this->dispatcher->forward(array(
'controller' => 'success',
'action' => 'show'
));
}
}
}


98.9k

Your code:

public function updateAction()
{
    if ($this->request->isPost()) {
        $id = $this->request->getPost('id','int');

        $admin = \Mallex\Mvc\Models\Admins::findFirst(array(
            'conditions' => 'id = :id:',
            'bind' => array('id' => $id),
            'bindType' => array('id' => \Phalcon\Db\Column::BIND_PARAM_INT)
        ));

        $admin->role_id = $this->request->getPost('roleid','int');
        $admin->salt = \Mallex\Mvc\Extensions\Utils::RandomChars(16, false);        
        $admin->pwd = sha1(md5(md5(md5($this->request->getPost('pwd','string') . $admin->salt))));
        $admin->region_id = $this->request->getPost('regionid','int');
        $admin->truename = $this->request->getPost('truename','string');
        $admin->sex = $this->request->getPost('sex','int');
        $admin->identity = $this->request->getPost('identity','string');
        $admin->address = $this->request->getPost('address','string');
        if ($admin->save()) {
            $this->flash->success($this->lang->admin->success);
            $this->dispatcher->forward(array(
                'controller' => 'success',
                'action' => 'show'
            ));
        } else {
            $this->flash->success($this->lang->admin->error);
            $this->dispatcher->forward(array(
                'controller' => 'success',
                'action' => 'show'
            ));
        }
    }
}

Some recommendations: maybe you want to use the security component (https://docs.phalcon.io/en/latest/reference/security.html), it provides better security without doing this: $admin->pwd = sha1(md5(md5(md5($this->request->getPost('pwd','string') . $admin->salt))));

This code:

$admin = \Mallex\Mvc\Models\Admins::findFirst(array(
    'conditions' => 'id = :id:',
    'bind' => array('id' => $id),
    'bindType' => array('id' => \Phalcon\Db\Column::BIND_PARAM_INT)
));

Can be just:

$admin = \Mallex\Mvc\Models\Admins::findFirstById($id);

Regarding your problem could you print_r($_POST) before saving?