コード例 #1
0
ファイル: ModelTest.php プロジェクト: solve/database
 public function testCreatingLoading()
 {
     $m = new Model();
     $m->_setName('Product');
     $this->assertEquals('Product', $m->_getName(), 'Internal change name ok');
     $object = $m->getModel('Product');
     $this->assertInstanceOf('Product', $object, 'getModel returns model Instance');
     $object = \Product::loadOne(1);
     $this->assertInstanceOf('Product', $object, 'loadOne returns model Instance');
     $data = $object->getArray();
     $this->assertEquals(array('id' => 1, 'title' => 'Macbook air', 'id_brand' => null), $data, 'User get array returns actual array');
     $this->assertEquals(1, $object->id, 'Getter of model object is works');
     $object->title = 'Test';
     $this->assertTrue($object->isChanged(), 'isChanged is true after changing');
     $this->assertEquals('Test', $object->title, 'Property changed after setter');
     $this->assertEquals('Test', $object['title'], 'Array accessor works');
     $res = $object->save();
     $this->assertTrue($res, 'Saved without errors');
     $this->assertFalse($object->isNew(), 'Object is not new after saving');
     $this->assertFalse($object->isChanged(), 'Object is not changed after saving');
     $object->setTitle('new title');
     $this->assertEquals('new title', $object->getTitle(), 'setter and getter works fine');
     $product = \Product::loadOne(QC::create()->where('id = :d', 1));
     $this->assertEquals('1', $product->id, 'loaded by QC');
     $product = \Product::loadOne(QC::create()->rawSelect('select * from products where id = 1'));
     $this->assertEquals('1', $product->id, 'loaded by QC raw select');
 }
コード例 #2
0
ファイル: TableViewController.php プロジェクト: solve/admin
 /**
  * @param Model|ModelCollection $caller
  */
 protected function loadAbilitiesForCaller($caller)
 {
     $files = $caller->_getStructure()->getAbilityInfo('files');
     if (!empty($files)) {
         foreach (array_keys($files) as $fileAlias) {
             $getterName = 'get' . Inflector::classify($fileAlias);
             if ($caller instanceof Model) {
                 $caller = array($caller);
             }
             foreach ($caller as $object) {
                 $object->{$getterName}();
             }
         }
     }
 }
コード例 #3
0
ファイル: SlugAbility.php プロジェクト: solve/database
 /**
  * @param Model $caller
  */
 public function preSave($caller)
 {
     if (array_key_exists($this->_sourceField, $caller->getChangedData())) {
         $caller->{$this->_valueField} = Inflector::slugify($caller->getChangedData($this->_sourceField));
     }
 }
コード例 #4
0
ファイル: FilesAbility.php プロジェクト: solve/database
 /**
  * @param Model $caller
  * @param $alias
  */
 public function skipFileAliasForSave($caller, $alias)
 {
     $hash = $caller->getInternalObjectHash();
     if (empty($this->_aliasedToSkip[$hash])) {
         $this->_aliasedToSkip[$hash] = array();
     }
     $this->_aliasedToSkip[$hash][$alias] = true;
 }
コード例 #5
0
ファイル: ModelOperator.php プロジェクト: solve/database
 /**
  * @param Model|ModelCollection $caller
  * @return array|null
  */
 public static function getIDs($caller)
 {
     return $caller instanceof Model ? array($caller->getID()) : $caller->getIDs();
 }
コード例 #6
0
ファイル: ModelCollection.php プロジェクト: solve/database
 public function _getName()
 {
     return $this->_model->_getName();
 }
コード例 #7
0
ファイル: ModelRelation.php プロジェクト: solve/database
 /**
  * @param Model|ModelCollection $caller
  * @param $relationName
  * @return $this
  * @throws \Exception
  */
 public function _loadRelated($caller, $relationName)
 {
     /**
      * @var $localKey
      * @var $localField
      * @var $foreignKey
      * @var $foreignField
      * @var $localTable
      * @var $foreignTable
      * @var $manyTable
      * @var $relationType
      * @var $relatedModelName
      * @var $hydration
      * @var $fieldsToRetrieve
      * @var $relationToMany
      */
     $varsToExport = ModelOperator::calculateRelationVariables($caller->_getName(), $relationName);
     extract($varsToExport);
     $foreignIds = array();
     $idsMap = array();
     $originalCaller = null;
     if ($caller instanceof Model && $caller->_hasCollectionReference()) {
         $caller = $caller->_getCollectionReference();
     }
     if ($relationType == 'many_to_one') {
         $foreignIds = ModelOperator::getFieldArray($caller, $localField);
     } elseif ($relationToMany) {
         $localIds = ModelOperator::getIDs($caller);
         $tableToSelect = $foreignTable;
         $fieldToSelect = $foreignKey;
         if ($relationType == 'many_to_many') {
             $tableToSelect = $manyTable;
             $fieldToSelect = $localField;
         }
         $idsMap = QC::create($tableToSelect)->where(array($tableToSelect . '.' . $foreignField => $localIds))->select($fieldToSelect . ', ' . $foreignField)->foldBy($foreignField)->use($fieldToSelect)->execute();
         foreach ($idsMap as $value) {
             $foreignIds = array_merge($foreignIds, $value);
         }
     }
     $foreignIds = array_unique($foreignIds);
     if (empty($foreignIds)) {
         return false;
     } elseif ($foreignKey == 'id') {
         $ids = array();
         foreach ($foreignIds as $key => $value) {
             $ids[] = intval($value);
         }
         $foreignIds = $ids;
     }
     /**
      * @var ModelCollection $relatedCollection
      */
     $relatedCollection = call_user_func(array($relatedModelName, 'loadList'), $foreignIds);
     $callerObjects = $caller instanceof ModelCollection ? $caller : array($caller);
     /**
      * @var Model $object
      */
     foreach ($callerObjects as $object) {
         if (substr($relationType, '-6') == 'to_one') {
             $relationValue = $relatedCollection->getOneByPK($object[$localField]);
         } else {
             $relationValue = $relatedCollection->getSubCollectionByPKs($idsMap[$object->getID()]);
         }
         $object->_setRawFieldValue($relationName, $relationValue);
     }
     return $this;
 }