Skip to content

Instantly share code, notes, and snippets.

@nibsirahsieu
Created April 10, 2012 08:47
Show Gist options
  • Save nibsirahsieu/2349438 to your computer and use it in GitHub Desktop.
Save nibsirahsieu/2349438 to your computer and use it in GitHub Desktop.

Revisions

  1. nibsirahsieu revised this gist Apr 10, 2012. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -127,19 +127,19 @@ class PropelOnDemandFormatter extends PropelObjectFormatter
    return $obj;
    }

    public function getStructuredArrayFromRow($row)
    {
    $columnNames = array_keys($this->getAsColumns ());
    public function getStructuredArrayFromRow($row)
    {
    $columnNames = array_keys($this->getAsColumns ());

    if (count($columnNames) > 1 && count($row) > 1) {
    $finalRow = array();
    foreach ($row as $index => $value) {
    $finalRow[str_replace('"', '', $columnNames[$index])] = $value;
    }
    } else {
    $finalRow = $row[0];
    }
    if (count($columnNames) > 1 && count($row) > 1) {
    $finalRow = array();
    foreach ($row as $index => $value) {
    $finalRow[str_replace('"', '', $columnNames[$index])] = $value;
    }
    } else {
    $finalRow = $row[0];
    }

    return $finalRow;
    }
    return $finalRow;
    }
    }
  2. nibsirahsieu created this gist Apr 10, 2012.
    145 changes: 145 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,145 @@
    <?php

    /**
    * This file is part of the Propel package.
    * For the full copyright and license information, please view the LICENSE
    * file that was distributed with this source code.
    *
    * @license MIT License
    */

    /**
    * Object formatter for Propel query
    * format() returns a PropelOnDemandCollection that hydrates objects as the use iterates on the collection
    * This formatter consumes less memory than the PropelObjectFormatter, but doesn't use Instance Pool
    *
    * @author Francois Zaninotto
    * @version $Revision$
    * @package propel.runtime.formatter
    */
    class PropelOnDemandFormatter extends PropelObjectFormatter
    {
    protected $collectionName = 'PropelOnDemandCollection';
    protected $isSingleTableInheritance = false;
    protected $isSelect = false;

    public function init(ModelCriteria $criteria)
    {
    parent::init($criteria);
    $this->isSingleTableInheritance = $criteria->getTableMap()->isSingleTableInheritance();

    if (!is_null($criteria->getSelect())) {
    $this->isSelect = true;
    }

    return $this;
    }

    public function isObjectFormatter()
    {
    return !$this->isSelect;
    }

    public function format(PDOStatement $stmt)
    {
    $this->checkInit();
    if ($this->isWithOneToMany()) {
    throw new PropelException('PropelOnDemandFormatter cannot hydrate related objects using a one-to-many relationship. Try removing with() from your query.');
    }
    $class = $this->collectionName;
    $collection = new $class();
    $collection->setModel($this->class);
    $collection->initIterator($this, $stmt);

    return $collection;
    }

    /**
    * Hydrates a series of objects from a result row
    * The first object to hydrate is the model of the Criteria
    * The following objects (the ones added by way of ModelCriteria::with()) are linked to the first one
    *
    * @param array $row associative array indexed by column number,
    * as returned by PDOStatement::fetch(PDO::FETCH_NUM)
    *
    * @return BaseObject
    */
    public function getAllObjectsFromRow($row)
    {
    $result = null;

    if ($this->isSelect) {
    if ($object = $this->getStructuredArrayFromRow($row)) {
    $result = $object;
    }
    } else {
    $result = $this->getObjectsFromRow($row);
    }

    return $result;
    }

    public function getObjectsFromRow($row)
    {
    $col = 0;
    // main object
    $class = $this->isSingleTableInheritance ? call_user_func(array($this->peer, 'getOMClass'), $row, $col) : $this->class;
    $obj = $this->getSingleObjectFromRow($row, $class, $col);
    // related objects using 'with'
    foreach ($this->getWith() as $modelWith) {
    if ($modelWith->isSingleTableInheritance()) {
    $class = call_user_func(array($modelWith->getModelPeerName(), 'getOMClass'), $row, $col);
    $refl = new ReflectionClass($class);
    if ($refl->isAbstract()) {
    $col += constant($class . 'Peer::NUM_COLUMNS');
    continue;
    }
    } else {
    $class = $modelWith->getModelName();
    }
    $endObject = $this->getSingleObjectFromRow($row, $class, $col);
    if ($modelWith->isPrimary()) {
    $startObject = $obj;
    } elseif (isset($hydrationChain)) {
    $startObject = $hydrationChain[$modelWith->getLeftPhpName()];
    } else {
    continue;
    }
    // as we may be in a left join, the endObject may be empty
    // in which case it should not be related to the previous object
    if (null === $endObject || $endObject->isPrimaryKeyNull()) {
    if ($modelWith->isAdd()) {
    call_user_func(array($startObject, $modelWith->getInitMethod()), false);
    }
    continue;
    }
    if (isset($hydrationChain)) {
    $hydrationChain[$modelWith->getRightPhpName()] = $endObject;
    } else {
    $hydrationChain = array($modelWith->getRightPhpName() => $endObject);
    }
    call_user_func(array($startObject, $modelWith->getRelationMethod()), $endObject);
    }
    foreach ($this->getAsColumns() as $alias => $clause) {
    $obj->setVirtualColumn($alias, $row[$col]);
    $col++;
    }
    return $obj;
    }

    public function getStructuredArrayFromRow($row)
    {
    $columnNames = array_keys($this->getAsColumns ());

    if (count($columnNames) > 1 && count($row) > 1) {
    $finalRow = array();
    foreach ($row as $index => $value) {
    $finalRow[str_replace('"', '', $columnNames[$index])] = $value;
    }
    } else {
    $finalRow = $row[0];
    }

    return $finalRow;
    }
    }