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

Skip property on Collection save

I need a property that doesn't save for a Collection. I tried overriding Phalcon\Mvc\Collection::getReservedAttributes() and adding my property, but that doesn't work.

Is there something similar to Phalcon\Mvc\Model::skipAttributes() for Collections?

Thanks.

Phalcon rulez!



8.7k

I hacked it like this. Static props don't save. I'd still like to know if there is a way to have a non-saving class property.

class Customer extends Phalcon\Mvc\Collection {

    // static properties not saved to Collection
    private static $currSubscription = null;

    // load Subscription when initialized
    public function initialize() {

        $this->getSubscription();
    }

    // load the Subscription when subscription id set
    public function setSubscriptionId($subId) {

        $this->subscriptionId = $subId;
        $this->getSubscription();
        return $this;
    }

    // get the Subscription
    public function getSubscription() {

        // only load if Subscription not loaded and subscription id set
        if(!self::$currSubscription && isset($this->subscriptionId) && $this->subscriptionId) {

            // load Subscription by id
            self::$currSubscription = Subscription::findById($this->subscriptionId);
        }
        return self::$currSubscription;
    }
}