Skip to content

Propel, the PropelOnDemandFormatter, self loaded runtime properties and your special reload data if needed usecase- howto

Propel column representation offering the option to add some business logic inside. We are making this from time to time by enriching the existing object with more data, but only when a method is called explicit.


class MyTable extends BaseMyTable
{
    /** @var null|\My\Enriched\MyTable\Data */
    protected $enrichedData;

    /**
     * @return null|\My\Enriched\MyTable\Data
     */
    public function getEnrichedData()
    {
        //prevent reloading enriched data if this method is called more than once
        if ($this->noEnrichedDataYetLoaded()) {
            //do something heavy data lifting
            $this->enrichedData = $this->tryToLoadEnrichedData();
        }

        return $this->enrichedData;
    }

    /**
     * @return bool
     */
    private function noEnrichedDataYetLoaded()
    {
        return (is_null($this->enrichedData));
    }
}

If you would use the corrosponding MyTableQuery object in combination with a PropelOnDemandFormatter and iterating over an collection after calling find(), you would get the same enriched data for different MyTable objects.
Why? Because the PropelOnDemandFormatter does a smart thing by reusing the on MyTable object and "just" updating the properties.
Following is a workaround I am using to fix this (totally right) behaviour. Anyways, be caution if you do things like that. This should not be your regular way of doing it.


class MyTable extends BaseMyTable
{
    /** @var null|\My\Enriched\MyTable\Data */
    protected $enrichedData;

    /** @var int */
    protected $myId;

    /**
     * @return null|\My\Enriched\MyTable\Data
     */
    public function getEnrichedData()
    {
        //prevent reloading enriched data if this method is called more than once
        if ($this->noEnrichedDataYetLoaded()) {
            //do something heavy data lifting
            $this->enrichedData = $this->tryToLoadEnrichedData();
            $this->myId         = $this->getId();
        }

        return $this->enrichedData;
    }

    /**
     * @return bool
     */
    private function noEnrichedDataYetLoaded()
    {
        return (
            ($this->myId == $this->getId())
            && (is_null($this->enrichedData))
        );
    }
}

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

The author does not allow comments to this entry

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Markdown format allowed
Form options