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))
        );
    }
}

Reminder of the Propel Bug 734 - update() with limit() and a workaround

Just because we ran into this issue again. There is known and serious bug in propel whenever you use "update()" in combination with "limit()".
Our workaround right now is to replace the code.


//this will update all entry with the content "bar" in the column "foo"
MyQuery::create()
    ->filterByFoo('bar')
    ->limit(100)
    ->update(
        array(
            'Foo' => 'baz'
        )
    );

//this will only update 100 rows
$ids = (array) MyQuery::create()
    ->filterByFoo('bar')
    ->limit(100)
    ->select(
        array(
            'Id'
        )
    )
    ->find();

MyQuery::create()
    ->filterById($ids)
    ->update(
        array(
            'Foo' => 'baz'
        )
    );