Ejemplo n.º 1
0
 /**
  * Try to find the object with all params from the $request
  *
  * @param string  $classQuery the query class
  * @param Request $request
  *
  * @return mixed
  */
 protected function findOneBy($classQuery, Request $request)
 {
     $query = $this->getQuery($classQuery);
     $hasCriteria = false;
     foreach ($this->filters as $column => $value) {
         if (!in_array($column, $this->exclude)) {
             try {
                 $query->{'filterBy' . PropelInflector::camelize($column)}($value);
                 $hasCriteria = true;
             } catch (\Exception $e) {
             }
         }
     }
     if (!$hasCriteria) {
         return false;
     }
     if (!$this->hasWith) {
         return $query->findOne();
     } else {
         return $query->find()->getFirst();
     }
 }
Ejemplo n.º 2
0
 /**
  * @dataProvider dataProviderForTestCamelize
  */
 public function testCamelize($word, $expected)
 {
     $this->assertEquals($expected, PropelInflector::camelize($word));
 }
Ejemplo n.º 3
0
 /**
  * Loads the data using the generated data model.
  *
  * @param array|null $data The data to be loaded
  */
 protected function loadDataFromArray($data = null)
 {
     if ($data === null) {
         return;
     }
     foreach ($data as $class => $datas) {
         // iterate through datas for this class
         // might have been empty just for force a table to be emptied on import
         if (!is_array($datas)) {
             continue;
         }
         $class = trim($class);
         if ('\\' == $class[0]) {
             $class = substr($class, 1);
         }
         $tableMap = $this->dbMap->getTable(constant(constant($class . '::TABLE_MAP') . '::TABLE_NAME'));
         $column_names = $tableMap->getFieldnames(TableMap::TYPE_PHPNAME);
         foreach ($datas as $key => $values) {
             // create a new entry in the database
             if (!class_exists($class)) {
                 throw new \InvalidArgumentException(sprintf('Unknown class "%s".', $class));
             }
             $obj = new $class();
             if (!$obj instanceof ActiveRecordInterface) {
                 throw new \RuntimeException(sprintf('The class "%s" is not a Propel class. There is probably another class named "%s" somewhere.', $class, $class));
             }
             if (!is_array($values)) {
                 throw new \InvalidArgumentException(sprintf('You must give a name for each fixture data entry (class %s).', $class));
             }
             foreach ($values as $name => $value) {
                 if (is_array($value) && 's' === substr($name, -1)) {
                     try {
                         // many to many relationship
                         $this->loadManyToMany($obj, substr($name, 0, -1), $value);
                         continue;
                     } catch (TableNotFoundException $e) {
                         // Check whether this is actually an array stored in the object.
                         if ('Cannot fetch TableMap for undefined table: ' . substr($name, 0, -1) === $e->getMessage()) {
                             if (PropelTypes::PHP_ARRAY !== $tableMap->getColumn($name)->getType() && PropelTypes::OBJECT !== $tableMap->getColumn($name)->getType()) {
                                 throw $e;
                             }
                         }
                     }
                 }
                 $isARealColumn = true;
                 if ($tableMap->hasColumn($name)) {
                     $column = $tableMap->getColumn($name);
                 } elseif ($tableMap->hasColumnByPhpName($name)) {
                     $column = $tableMap->getColumnByPhpName($name);
                 } else {
                     $isARealColumn = false;
                 }
                 // foreign key?
                 if ($isARealColumn) {
                     /*
                      * A column, which is a PrimaryKey (self referencing, e.g. versionable behavior),
                      * but which is not a ForeignKey (e.g. delegatable behavior on 1:1 relation).
                      */
                     if ($column->isPrimaryKey() && null !== $value && !$column->isForeignKey()) {
                         if (isset($this->object_references[$this->cleanObjectRef($class . '_' . $value)])) {
                             $obj = $this->object_references[$this->cleanObjectRef($class . '_' . $value)];
                             continue;
                         }
                     }
                     if ($column->isForeignKey() && null !== $value) {
                         $relatedTable = $this->dbMap->getTable($column->getRelatedTableName());
                         if (isset($this->object_references[$this->cleanObjectRef($relatedTable->getClassname() . '_' . $value)])) {
                             $value = $this->object_references[$this->cleanObjectRef($relatedTable->getClassname() . '_' . $value)]->getByName($column->getRelatedName(), TableMap::TYPE_COLNAME);
                         } else {
                             $relatedClass = $this->cleanObjectRef($relatedTable->getClassName());
                             if (isset($data[$relatedClass]) || isset($data['\\' . $relatedClass])) {
                                 throw new \InvalidArgumentException(sprintf('The object "%s" from class "%s" is not defined in your data file.', $value, $relatedTable->getClassname()));
                             }
                         }
                     }
                 }
                 if (false !== ($pos = array_search($name, $column_names))) {
                     $obj->setByPosition($pos, $value);
                 } elseif (is_callable(array($obj, $method = 'set' . ucfirst(PropelInflector::camelize($name))))) {
                     $obj->{$method}($value);
                 } else {
                     throw new \InvalidArgumentException(sprintf('Column "%s" does not exist for class "%s".', $name, $class));
                 }
             }
             $obj->save($this->con);
             $this->saveParentReference($class, $key, $obj);
         }
     }
 }