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

Cannot change model field after magick call to related model

Hello! I have model with relations


<?php
namespace Abka\Business\Models;

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Relation;
use Phalcon\Validation;
use Phalcon\Validation\Validator\InclusionIn as InclusionInValidator;
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;
use Phalcon\Validation\Validator\Numericality as NumericalityValidator;
use Abka\Project\DiscountProgram;
use Abka\Project\DiscountProgram\DiscountProgramInterface;
use Abka\Project;

/**
 */
class UserSellerDealings extends Model
{
    /**
     */
    const CLIENT_TYPE_SYSTEM    = 'system';
    const CLIENT_TYPE_SELLER    = 'seller';

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

    /**
     * @var boolean
     */
    public $userId;

    /**
     * @var boolean
     */
    public $sellerId;

    /**
     * @var integer
     */
    public $clientType;

    /**
     * @var
     */
    public $statusId;

    /**
     * @var
     */
    public $discountProgramSnapshotId;

    /**
     * @var
     */
    public $actionBaseSnapshotId;

    /**
     * @var
     */
    public $shareShortLinkId;

    /**
     * @var boolean
     */
    public $bill;

    /**
     * @var boolean
     */
    public $deferredBill;

    /**
     * @var
     */
    public $economy;

    /**
     * @var
     */
    public $extraEconomy;

    /**
     * @var
     */
    public $visitsCount;

    /**
     * @var boolean
     */
    public $createdTime;

    /**
     * @var boolean
     */
    public $passageTime;

    /**
     * @var boolean
     */
    public $migrationTime;

    /**
     * @var boolean
     */
    public $excuseTime;

    /**
     * @var boolean
     */
    public $referralInfoTime;

    /**
     */
    public function initialize()
    {
        $this->useDynamicUpdate(true);
        $this->keepSnapshots(true);

        // Relations
        $this->belongsTo(
            'sellerId',
            'Abka\\Business\\Models\\Sellers',
            'id',
            [
                'alias'      => 'Seller',
                'foreignKey' => [
                    'action'  => Relation::ACTION_RESTRICT,
                    'message' => 'Seller not exist',
                ],
                'reusable'   => true,
            ]
        );
        $this->belongsTo(
            'userId',
            'Abka\\Business\\Models\\Users',
            'id',
            [
                'alias'      => 'User',
                'foreignKey' => [
                    'action'  => Relation::ACTION_RESTRICT,
                    'message' => 'User not exist',
                ],
                'reusable'   => true,
            ]
        );
        $this->belongsTo(
            'discountProgramSnapshotId',
            'Abka\\Business\\Models\\DiscountProgramsSnapshots',
            'id',
            [
                'alias'      => 'DiscountProgramsSnapshot',
                'foreignKey' => [
                    'allowNulls' => true,
                    'action'    => Relation::ACTION_RESTRICT,
                    'message'   => '...not exist',
                ],
                'reusable'   => true,
            ]
        );
        $this->belongsTo(
            'actionBaseSnapshotId',
            'Abka\\Business\\Models\\ActionBaseSnapshots',
            'id',
            [
                'alias'      => 'ActionBaseSnapshot',
                'foreignKey' => [
                    'allowNulls' => true,
                    'action'    => Relation::ACTION_RESTRICT,
                    'message'   => '...not exist',
                ],
                'reusable'   => false,
            ]
        );
        $this->belongsTo(
            'shareShortLinkId',
            'Abka\\Customer\\Models\\ShortLinks',
            'id',
            [
                'alias'      => 'ShortLink',
                'foreignKey' => [
                    'allowNulls' => true,
                    'action'    => Relation::ACTION_RESTRICT,
                    'message'   => '...not exist',
                ],
                'reusable'   => true,
            ]
        );
    }

}

Controller code

$model = \Abka\Business\Models\UserSellerDealings::findFirstById(823);  // record exists

var_dump($model->actionBaseSnapshotId); // show current value =12

$relatedModel = $model->actionBaseSnapshot;  // acess via magick property to related model

$model->actionBaseSnapshotId = 16;  // set new value (related record exists)
$model->migrationTime = time();         // set another property (for test)

var_dump($model->getChangedFields());       // show changed fields (actionBaseSnapshotId mark as changed)
var_dump($model->hasChanged('actionBaseSnapshotId'));   // amother test of change (result=true)

$status = $model->save();                       // saver model
var_dump($status);                                      // show status of save (true)
var_dump($model->getMessages());        // show error messages (void array)

Field actionBaseSnapshotId cannot change. The problem appears after the magick call $model->actionBaseSnapshot. Result:

string(2) "12"
array(2) {
  [0]=>
  string(20) "actionBaseSnapshotId"
  [1]=>
  string(13) "migrationTime"
}
bool(true)
bool(true)
array(0) {
}

but field actionBaseSnapshotId not changed (migrationTime is changed) and UPDATE query look like

UPDATE `user_seller_dealings` SET `migrationTime` = ? WHERE `id` = ? array (
  0 => 1529764359,
  1 => '823',
)

If i call $model->getRelated('actionBaseSnapshot') insted of $model->actionBaseSnapshot all work fine

Just don't use really those magic thing, best add method getActionBaseSnapshot where you will do getRelated('actionBaseSnapshot')

It is strange that the behavior is different. It is not right.