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
Die Kommentarfunktion wurde vom Besitzer dieses Blogs in diesem Eintrag deaktiviert.
Kommentare
Ansicht der Kommentare: Linear | Verschachtelt