Esempio n. 1
0
 protected function _getForeignKeyWhereClause(Garp_Db_Table_Row $record, $model, $foreignKeyColumns)
 {
     $whereData = array_get_subset($record->toArray(), $foreignKeyColumns);
     // Assume one "id" primary key
     if (count($foreignKeyColumns) > 1) {
         throw new Exception("Can't deal with multiple foreign keys right now!");
     }
     $whereData = array_combine(array('id'), array_values($whereData));
     return $model->arrayToWhereClause($whereData);
 }
Esempio n. 2
0
 /**
  * Callback after walking over the results.
  * Returns everything to its original state.
  * @return Void
  */
 protected function _afterWalk()
 {
     // return the pointer of a Rowset to 0
     if ($this->_result instanceof Garp_Db_Table_Rowset) {
         $this->_result->rewind();
     } else {
         // also, return results to the original format if it was no Rowset to begin with.
         $this->_result = $this->_result[0];
     }
 }
Esempio n. 3
0
 /**
  * @param   Garp_Db_Table_Row   $row        Unfiltered row
  * @param   Array               $columns    Elasticsearchable column names of the base model
  * @return  Array                           Filtered row
  */
 public function filter(Garp_Db_Table_Row $row, array $columns)
 {
     $rowArray = $row->toArray();
     $filteredRow = $this->_filterRow($rowArray, $columns);
     return $filteredRow;
 }
Esempio n. 4
0
 /**
  * Find a related recordset.
  * @param Garp_Model $model The model that spawned this data
  * @param Garp_Db_Row $row The row object
  * @param Garp_Util_Configuration $options Various relation options
  * @return String The name of the method.
  */
 protected function _getRelatedRowset(Garp_Model $model, Garp_Db_Table_Row $row, Garp_Util_Configuration $options)
 {
     /**
      * An optional passed SELECT object will be passed by reference after every query.
      * This results in an error when 'clone' is not used, because correlation names will be
      * used twice (since they were set during the first iteration). Using 'clone' makes sure
      * a brand new SELECT object is used every time that hasn't been soiled by a possible
      * previous query.
      */
     $conditions = is_null($options['conditions']) ? null : clone $options['conditions'];
     $otherModel = $options['modelClass'];
     if (!$otherModel instanceof Zend_Db_Table_Abstract) {
         $otherModel = new $otherModel();
     }
     /**
      * Do not cache related queries. The "outside" query should be the only
      * query that's cached.
      */
     $originalCacheQueriesFlag = $otherModel->getCacheQueries();
     $otherModel->setCacheQueries(false);
     $modelName = get_class($otherModel);
     $relatedRowset = null;
     // many to many
     if (!empty($options['bindingModel'])) {
         $relatedRowset = $row->findManyToManyRowset($otherModel, $options['bindingModel'], $options['rule2'], $options['rule'], $conditions);
     } else {
         /**
          * 'mode' is used to clear ambiguity with homophilic relationships. For example,
          * a Model_Doc can have have child Docs and one parent Doc. The conditionals below can never tell
          * which method to call (findParentRow or findDependentRowset) from the referenceMap.
          * Therefore, we can help the decision-making by passing "mode". This can either be
          * "parent" or "dependent", which will then force a call to findParentRow and findDependentRowset,
          * respectively.
          */
         if (is_null($options['mode'])) {
             // belongs to
             try {
                 $model->getReference($modelName, $options['rule']);
                 $relatedRowset = $row->findParentRow($otherModel, $options['rule'], $conditions);
             } catch (Exception $e) {
                 if (!Garp_Content_Relation_Manager::isInvalidReferenceException($e)) {
                     throw $e;
                 }
                 try {
                     // one to many - one to one
                     // The following line triggers an exception if no reference is available
                     $otherModel->getReference(get_class($model), $options['rule']);
                     $relatedRowset = $row->findDependentRowset($otherModel, $options['rule'], $conditions);
                 } catch (Exception $e) {
                     if (!Garp_Content_Relation_Manager::isInvalidReferenceException($e)) {
                         throw $e;
                     }
                     $bindingModel = $model->getBindingModel($modelName);
                     $relatedRowset = $row->findManyToManyRowset($otherModel, $bindingModel, $options['rule2'], $options['rule'], $conditions);
                 }
             }
         } else {
             switch ($options['mode']) {
                 case 'parent':
                     $relatedRowset = $row->findParentRow($otherModel, $options['rule'], $conditions);
                     break;
                 case 'dependent':
                     $relatedRowset = $row->findDependentRowset($otherModel, $options['rule'], $conditions);
                     break;
                 default:
                     throw new Garp_Model_Exception('Invalid value for "mode" given. Must be either "parent" or ' . '"dependent", but "' . $options['mode'] . '" was given.');
                     break;
             }
         }
     }
     // Reset the cacheQueries value. It's a static property,
     // so leaving it FALSE will affect all future fetch() calls to this
     // model. Not good.
     $otherModel->setCacheQueries($originalCacheQueriesFlag);
     return $relatedRowset;
 }
Esempio n. 5
0
 protected function _pushRecord(Garp_Model_Db $model, Garp_Model_Behavior_Abstract $phpBehavior, Garp_Db_Table_Row $record)
 {
     $primaryKey = current($record->toArray());
     $phpBehavior->afterSave($model, $primaryKey);
 }
Esempio n. 6
0
 protected function _executeJob(Garp_Db_Table_Row $job, $serverId)
 {
     $commandParts = explode(' ', $job->command);
     $class = $commandParts[0];
     $method = $commandParts[1];
     $argumentsIn = array_slice($commandParts, 2);
     $argumentsOut = array();
     foreach ($argumentsIn as $argument) {
         if (strpos($argument, '=') === false) {
             $argumentsOut[] = $argument;
         } else {
             $argumentParts = explode('=', $argument);
             $argumentName = substr($argumentParts[0], 2);
             $argumentValue = $argumentParts[1];
             $argumentsOut[$argumentName] = $argumentValue;
         }
     }
     $fullClassNameWithoutModule = 'Cli_Command_' . $class;
     $appClassName = 'App_' . $fullClassNameWithoutModule;
     $garpClassName = 'Garp_' . $fullClassNameWithoutModule;
     if (class_exists($appClassName)) {
         $className = $appClassName;
     } elseif (class_exists($garpClassName)) {
         $className = $garpClassName;
     } else {
         throw new Exception("Cannot load {$appClassName} or {$garpClassName}.");
     }
     $acceptMsg = 'Accepting job: ' . $className . '.' . $method;
     if ($argumentsOut) {
         $acceptMsg .= ' with arguments: ' . str_replace(array("\n", "\t", "  "), '', print_r($argumentsOut, true));
     }
     Garp_Cli::lineOut($acceptMsg);
     // Update the job with acceptance data
     $job->accepter_id = $serverId;
     $job->last_accepted_at = date('Y-m-d H:i:s');
     $job->save();
     // Execute the command
     $class = new $className();
     $class->{$method}($argumentsOut);
 }
Esempio n. 7
0
 /**
  * Merge translated fields into the main records
  *
  * @param Garp_Db_Table_Row $result
  * @return void
  */
 public function mergeTranslatedFields($result)
 {
     if (!isset($result->{self::I18N_MODEL_BINDING_ALIAS})) {
         return;
     }
     $translationRecordList = $result->{self::I18N_MODEL_BINDING_ALIAS};
     if ($translationRecordList instanceof Zend_Db_Table_Rowset_Abstract) {
         $translationRecordList = $translationRecordList->toArray();
     }
     $translatedFields = array();
     $allLocales = Garp_I18n::getLocales();
     foreach ($this->_translatableFields as $translatableField) {
         // provide default values
         foreach ($allLocales as $locale) {
             $translatedFields[$translatableField][$locale] = null;
         }
         foreach ($translationRecordList as $translationRecord) {
             $lang = $translationRecord[self::LANG_COLUMN];
             $translatedFields[$translatableField][$lang] = $translationRecord[$translatableField];
         }
         $result->setVirtual($translatableField, $translatedFields[$translatableField]);
         //$lang] = $translationRecord[$translatableField];
     }
     // We now have a $translatedFields array like this:
     // array(
     //   "name" => array(
     //     "nl" => "Schaap",
     //     "en" => "Sheep"
     //   )
     // )
     //$result->setFromArray($translatedFields);
     unset($result->{self::I18N_MODEL_BINDING_ALIAS});
 }