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

Single column isn't updating after save. Relational Models seems to be the culprit

So I have a really odd problem that i can't figure out. This one column won't update on ->save().

I have tested it like this. this is in my edit action.

  $account = Accounts::findFirstById($id);
  //...... some logic here
  $edit = $this->request->getPost();
  $update = array();
  foreach($edit as $k => $v){
      $update[$k] = htmlspecialchars($v);
  }   
  // First output below. 
  $account->assign($update);
  // second output below, notice the userId has changed as the asignment should have done it. 
  if (!$account->save()) {
      $this->flash->error($account->getMessages());
  } else {
      // Third output here. So no error happend, but the userId is now back to its original value....
      $this->flashSession->success("User was updated successfully");
      $this->response->redirect('accounts/view/'.$account->id);
  }

FIRST DUMP - befor ethe assign.

Array
(
    [id] => 83
    [name] => this is insaneasdfasdfasdf
    [phone] => xxx-xxx-xxxx
    [email] => [email protected]
    [accountId] => 
    [userId] => 6
)

SECOND DUMP - after the assign.

Array
(
    [id] => 83
    [name] => this is insaneasdfasdfasdf
    [phone] => xxx-xxx-xxxx
    [email] => [email protected]
    [accountId] => 
    [userId] => 8
)

THIRD DUMP - after save

Array
(
    [id] => 83
    [name] => this is insaneasdfasdfasdf
    [phone] => xxx-xxx-xxxx
    [email] => [email protected]
    [accountId] => 
    [userId] => 6
)

So i was curious if it had something to do wtih my relationals listed on the account or user model.

To test this i changed the ID of userId, to a user that disnt' exist. in my case id 7. Doing this makes it so that i can change value. When the alvue is set to an existing user i have this extra bit in the dump the id being the same as the id that won't change seemd suspicious.

[_uniqueKey:protected] => `id` = ?
[_uniqueParams:protected] => Array
(
    [0] => 6
)

[_uniqueTypes:protected] => Array
(
    [0] => 6
)

here is my instantiation in account model

public function initialize()
    {
        $this->belongsTo("userId", "Nova\Models\Users", "id", array(
             'alias' => 'accountOwner'
        ));  
        $this->belongsTo("createdBy", "Nova\Models\Users", "id", array(
            'alias' => 'createdName'
        ));
        $this->hasMany('id', 'Nova\Models\AccountingLineItems','accountId', array(
            'alias' => 'accountingLineItems'
        ));
        $this->hasMany("id", "Nova\Models\Payments", "accountId", array(
            'alias' => 'payments',
            'foreignKey' => array(
                'message' => 'This account cannot be deleted because it has payments'
        )));
        $this->hasMany('id','Nova\Models\AccountStatus','accountId',array(
            'alias' => 'accountStatus'
        ));
        $this->hasMany('id','Nova\Models\Notes','accountId', array(
            'alias' => 'notes'
        ));
        $this->hasMany('id', 'Nova\Models\Tasks', 'accountId', array(
            'alias' => 'tasks'
        ));
        $this->hasMany('id','Nova\Models\Files','objectId', array(
            'alias' => 'files',
            'params' => array(
                'conditions' => "type = 'account'"
        )));
        $this->hasMany('id','Nova\Models\AccountSettings','accountId',array(
            'alias'=>'accountSettings'
        ));
    }

And here is hte user. its large :P

public function initialize(){

        $this->belongsTo('userRegionalId', 'Nova\Models\Users', 'id', 
            array('alias' => 'regional'
        ));

        $this->belongsTo('userManagerId', 'Nova\Models\Users', 'id', 
            array('alias' => 'manager'
        ));    

        $this->belongsTo('userRecruiterId', 'Nova\Models\Users', 'id', 
            array('alias' => 'recruiter'
        ));    

        $this->hasMany('id', 'Nova\Models\Users', 'userRegionalId',
            array('alias' => 'regionalSubs'
        ));

        $this->hasMany('id', 'Nova\Models\Users', 'userManagerId',
            array('alias' => 'managerSubs'
        ));
        $this->hasMany('id', 'Nova\Models\Users', 'userRecruiterId',
            array('alias' => 'recruiterSubs'
        ));    

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

        $this->hasMany("id", "Nova\Models\Accounts", "userId", array(
            'alias' => 'accounts',
            'reusable' => true
        ));

        $this->hasMany('id', 'Nova\Models\SuccessLogins', 'usersId', array(
            'alias' => 'successLogins',
            'foreignKey' => array(
                'message' => 'User cannot be deleted because he/she has activity in the system'
            )
        ));

        $this->hasMany('id', 'Nova\Models\PasswordChanges', 'usersId', array(
            'alias' => 'passwordChanges',
            'foreignKey' => array(
                'message' => 'User cannot be deleted because he/she has activity in the system'
            )
        ));

        $this->hasMany('id', 'Nova\Models\ResetPasswords', 'usersId', array(
            'alias' => 'resetPasswords',
            'foreignKey' => array(
                'message' => 'User cannot be deleted because he/she has activity in the system'
            )
        ));

        $this->hasMany('id', 'Nova\Models\Payments', 'accountUserId', array(
            'alias' => 'payments',
            'foreignKey' => array(
                'message' => "user cannot be dleted because he has payments assigned to him"
        )));

        $this->hasMany('id', 'Nova\Models\Draws', 'userId', array(
            'alias' => 'draws',
            'foreignKey' => array(
                'message' => 'user cannat be deleted because he/she has draws'
        )));

        $this->hasMany('id', 'Nova\Models\Accounts', 'userId', array(
            'alias' => 'accounts',
            'foreignKey' => array(
                'message'=> 'This user still has accounts assigned to it'
        )));

        // FILES HERE
        $this->hasMany('id','Nova\Models\Files','objectId', array(
            'alias' => 'files',
            'params' => array(
                'conditions' => "type = 'user'"
        )));
        $this->hasOne('id','Nova\Models\Files', 'objectId', array(
            'alias' => 'userImage',
            'params' => array(
                'conditions' => "type = 'userImage'"
        )));
        $this->hasMany('id','Nova\Models\Files', 'objectId',array(
            'alias' => 'userIdImages',
            'params' => array(
                'conditions' => "type = 'userIdImages'" 
        )));
        $this->hasMany('id','Nova\Models\Files', 'objectId', array(
            'alias' => 'userAgreementFiles',
            'params' => array(
                'conditions' => "type = 'userAgreementFiles'"
        )));

        $this->hasMany('id', 'Nova\Models\UsersCompliance','userId',array(
            'alias' => 'usersCompliance',
            'foreignKey' => array(
                'action' => Relation::ACTION_CASCADE
        )));
    }
edited Mar '16

quick update. the lines from the account model... when commented out the save goes through just fine.

$this->belongsTo("userId", "Nova\Models\Users", "id", array(
    'alias' => 'accountOwner',
)); 

this breaks a lot of other functionality not having this relation...

It ssomethign with the user to account rleation that is breaking it.. ther eare lots of items inter-related in the system. I don't know why this ouwld cause the problem though? any ideas?

edited Mar '16

I found the culprit code but i am not sure why it caused the single column to not update. It must be somethign with the relation between the Account, the user, and the suese other information.

here is the suspect code

This is the in teh account afterFetch() function.

It has something to dow tih the @ i am sure. suppressing the error messages are probably causing the issue for whatever reason. I am going to rewrite it and be back. the commented blocks are the old values before i patched it to work overnight.

$commissionsDetails = array(
            'rep' => array(
                'paid' => 0,
                'esti' => 0),// ($this->accountOwner) ? $this->systemSize * (($this->accountOwner->basePayOveride == 0) ? 220 : $this->accountOwner->basePayOveride) : 0),
            'man' => array(
                'paid' => 0,
                'esti' => 0),//($this->accountOwner->manager) ? $this->systemSize * (($this->accountOwner->manager->regOverride == 0) ? 30 : $this->accountOwner->manager->manOverride) : 0),
            'reg' => array(
                'paid' => 0,
                'esti' => 0), //(@$this->accountOwner->regional) ? $this->systemSize * (($this->accountOwner->regional->regOverride == 0) ? 16 : $this->accountOwner->regional->regOverride) : 0),
            'rec' => array(
                'paid' => 0,
                'esti' => 0),//(@$this->accountOwner->recruiter) ? $this->accountOwner->recruiter->recruitPay : 0)
        );
edited Mar '16

So rewrote it to handle errors better but it turned out hat wasn't the issue.

edited Mar '16

It would appear that any access to the accountOwner relation in my afterFetch() breaks the save of that relation change.