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 updating model property when accesing to related model via that property

Hello, I can't update the property of a model if previously I access a related model via that property

Let me explain: I'm trying to update $plan_id, but If previously I access to the related Model "Plan" via the property that connects both models (by this way: $subscription->plan->category_id), that property ($plan_id) doesn't update.

If I don't get the related property value ($subscription->plan->category_id), the property I'm trying to change ($plan_id) updates correctly.

This is the code

class Subscription extends \Phalcon\Mvc\Model
{
    public $plan_id;

    public function initialize()
    {
        $this->belongsTo('plan_id', '\Plan', 'id', ['alias' => 'plan']);
    }

    public function changeSubscriptionPlan($user, $newPlan)
    {
        $subscription = new Subscription();
        $subscription = $subscription->findFirstByUserId($user->id);

        if($subscription->plan->category_id == 1)
        {
            $subscription->plan_id = $newPlan->id;
            $currentSubscription->save();
        }
    }

}

Also I have tried get the messages, and no errors occurs

if (!$subscription->save()) {
    foreach ($subscription->getMessages() as $message) {
        $this->getDI()
            ->getFlash()
            ->error($message);
    }
}

Am I doing anything wrong?

hi,

<?php

class Subscription extends \Phalcon\Mvc\Model
{
    public $plan_id;

    public function initialize()
    {
        $this->belongsTo('plan_id', '\Plan', 'id', ['alias' => 'plan']);
    }

    public function changeSubscriptionPlan($user, $newPlan)
    {
        $subscription = new Subscription();
        $subscription = $subscription->findFirstByUserId($user->id);

        if($subscription->plan->category_id == 1)
        {
            $subscription->plan_id = $newPlan->id;
            $currentSubscription->save(); // where this $currentSubscription variable is starting?
        }
    }

}

Your logic is kind of weird. Why Subscription class you are again finding Subscription model? I would change your code to something like:

public function changeSubscriptionPlan($newPlan)
    {
        if($this->getRelated('plan')->category_id == 1) // or add method getPlan which will use getRelated
        {
            $this->plan_id = $newPlan->id;
            $this->save(); // where this $currentSubscription variable is starting?
        }
    }

And then somewhere just do this find $subscription = $subscription->findFirstByUserId($user->id); and $subscription->changeSubscriptionPlan($newPlan)

edited Jan '17

Hi, I reported the issue last summer, it's on the TODO list, basicly the problem if you load a relationship you cannot update the variable which loaded that, my temporary solution is kinda nasty but works:

unset($this->plan, $this->_related['plan']);

After this you can update the variable.

Tryed your solution earlier, but my problem was that you request the plan everytime when using the getRelated fn, so eagerloading and testing multiple times in the same process generates a huge amount of useless query.

You can always set reusable :D