Ejemplo n.º 1
0
 protected function _getModelByName($modelName)
 {
     $models = $this->_getVisibleModels();
     $modelsWithName = array_filter($models, propertyEquals('id', $modelName));
     if (!count($modelsWithName)) {
         throw new Garp_Content_Api_Rest_Exception_ModelNotFound(sprintf(self::EXCEPTION_MODEL_NOT_FOUND, $modelName));
     }
     return current($modelsWithName);
 }
Ejemplo n.º 2
0
 /**
  * Interactively insert a new record
  *
  * @param array $args
  * @return bool
  */
 public function insert($args)
 {
     if (empty($args)) {
         Garp_Cli::errorOut('Please provide a model');
         return false;
     }
     $className = "Model_{$args[0]}";
     $model = new $className();
     $fields = $model->getConfiguration('fields');
     $fields = array_filter($fields, not(propertyEquals('name', 'id')));
     $mode = $this->_getInsertionMode();
     if ($mode === 'g') {
         $newData = $this->_createGibberish($fields);
     } else {
         $newData = array_reduce($fields, function ($acc, $cur) {
             $acc[$cur['name']] = Garp_Cli::prompt($cur['name']) ?: null;
             return $acc;
         }, array());
     }
     $id = $model->insert($newData);
     Garp_Cli::lineOut("Record created: #{$id}");
     return true;
 }
Ejemplo n.º 3
0
 /**
  * Get related hasOne records and combine into a single response
  *
  * @param string $datatype
  * @param array $records
  * @param array $with
  * @return array The combined resultsets
  */
 protected function _combineRecords($datatype, array $records, $with)
 {
     $modelName = $this->_normalizeModelName($datatype);
     $rootModel = new $modelName();
     $schema = instance(new Garp_Content_Api_Rest_Schema('rest'))->getModelDetails($datatype);
     $hasOneRelations = array_filter($schema['fields'], propertyEquals('origin', 'relation'));
     $hasOneRelations = array_map(array_get('relationAlias'), $hasOneRelations);
     // Check validity of 'with'
     $unknowns = array_filter($with, callRight(not('in_array'), $hasOneRelations));
     if (count($unknowns)) {
         $err = sprintf(Garp_Content_Api_Rest_Schema::EXCEPTION_RELATION_NOT_FOUND, $datatype, current($unknowns));
         throw new Garp_Content_Api_Rest_Exception($err);
     }
     $self = $this;
     return array_reduce($with, function ($acc, $cur) use($rootModel, $records, $schema, $self) {
         // grab foreign key names from the relation
         $foreignKey = current(array_filter($schema['fields'], propertyEquals('relationAlias', $cur)));
         // Grab foreign key values
         $foreignKeyValues = array_map(array_get($foreignKey['name']), $records);
         // No values to filter on? Bail.
         if (!count(array_filter($foreignKeyValues))) {
             $acc[$cur] = array();
             return $acc;
         }
         $foreignKeyValues = array_values(array_unique($foreignKeyValues));
         // Construct options object for manager
         $options = array('options' => array('query' => array('id' => $foreignKeyValues)), 'datatype' => $foreignKey['model']);
         // fetch with options
         $acc[$cur] = array_get(current($self->_getIndex($options)), 'result');
         return $acc;
     }, array($datatype => $records));
 }