Example #1
0
 /**
  * Check if there are bindings to fetch related records and
  * start the related rowset fetching.
  * @param Garp_Model $model The model that spawned this data
  * @param Garp_Db_Table_Row|Garp_Db_Table_Rowset $data The fetched root data
  * @return Void
  */
 protected function _combineResultsWithBindings(Garp_Model $model, $data)
 {
     $tableName = $model->getName();
     if ($data) {
         $bindings = $model->getBindings();
         if (!empty($bindings)) {
             // check if it's a rowset or a single row
             if (!$data instanceof Garp_Db_Table_Rowset) {
                 $data = array($data);
             }
             foreach ($bindings as $binding => $bindOptions) {
                 /**
                  * We keep tabs on the outer call to fetch() by checking getRecursion.
                  * If it is 0, this is the first call in the chain. That way we can
                  * clean up the recursion when we're done, since all subsequent fetch() calls
                  * will happen within this one and the if ($cleanup) line ahead will only
                  * fire on the first fetch(). Look at it like this:
                  *
                  * fetch()
                  *   cleanup = 1
                  *     fetch()
                  *       fetch()
                  *     fetch()
                  *       fetch()
                  *     fetch()
                  *       fetch()
                  *   if (cleanup) resetRecursion()
                  *
                  */
                 $cleanup = false;
                 if (Garp_Model_Db_BindingManager::getRecursion(get_class($model), $binding) == 0) {
                     $cleanup = true;
                 }
                 if (Garp_Model_Db_BindingManager::isAllowedFetch(get_class($model), $binding)) {
                     Garp_Model_Db_BindingManager::registerFetch(get_class($model), $binding);
                     foreach ($data as $datum) {
                         // there's no relation possible if the primary key is not among the fetched columns
                         $prim = (array) $model->info(Zend_Db_Table::PRIMARY);
                         foreach ($prim as $key) {
                             try {
                                 $datum->{$key};
                             } catch (Exception $e) {
                                 break 2;
                             }
                         }
                         $relatedRowset = $this->_getRelatedRowset($model, $datum, $bindOptions);
                         $datum->setRelated($binding, $relatedRowset);
                     }
                 }
                 if ($cleanup) {
                     Garp_Model_Db_BindingManager::resetRecursion(get_class($model), $binding);
                 }
             }
             // return the pointer to 0
             if ($data instanceof Garp_Db_Table_Rowset) {
                 $data->rewind();
             }
         }
     }
 }