예제 #1
0
    protected static function putTestContent()
    {
        QC::executeSQL('DROP TABLE IF EXISTS products');
        $storagePath = __DIR__ . '/storage/';
        $mo = ModelOperator::getInstance($storagePath);
        $mo->generateBasicStructure('Product');
        $ms = new ModelStructure('Product');
        $ms->addColumn('id_brand', array('type' => 'int(11) unsigned'));
        $ms->saveStructure();
        $mo->generateAllModelClasses();
        $mo->updateDBForAllModels();
        require_once $storagePath . 'bases/BaseProduct.php';
        require_once $storagePath . 'classes/Product.php';
        QC::create('products')->insert(array('title' => 'Macbook air'))->execute();
        $testProductFileContent = <<<TEXT
<?php

class ProductCustom extends BaseProduct {
    private \$_internalManufacturer = 'solve';
    public function getManufacturer() { return \$this->_internalManufacturer; }
    public function setManufacturer(\$value) { \$this->_internalManufacturer = \$value; return \$this; }
}
TEXT;
        file_put_contents($storagePath . 'classes/ProductCustom.php', $testProductFileContent);
        require_once $storagePath . 'classes/ProductCustom.php';
        $mo->setStructureForModel('ProductCustom', $mo->generateBasicStructure('Product', false), false);
    }
예제 #2
0
 protected static function putTestContent()
 {
     QC::executeSQL('DROP TABLE IF EXISTS products');
     $storagePath = __DIR__ . '/storage/';
     $mo = ModelOperator::getInstance($storagePath);
     $mo->generateBasicStructure('Product');
     $ms = new ModelStructure('Product');
     $ms->addColumn('info', array('type' => 'array'));
     $ms->saveStructure();
     $mo->generateAllModelClasses();
     $mo->updateDBForAllModels();
     require_once $storagePath . 'bases/BaseProduct.php';
     require_once $storagePath . 'classes/Product.php';
     QC::create('products')->insert(array('title' => 'Macbook air'))->execute();
 }
예제 #3
0
파일: Model.php 프로젝트: solve/database
 public function &__get($key)
 {
     // if key is exists - return it
     if (array_key_exists($key, $this->_data)) {
         return $this->_data[$key];
     }
     $getterName = Inflector::camelize($key);
     $method = 'get' . $getterName;
     $default = null;
     // check for getter
     if (method_exists($this, $method) && !array_key_exists($getterName, $this->_invokedGetters)) {
         $this->_data[$key] = $this->_invokedGetters[$getterName] = $this->{$method}();
         return $this->_data[$key];
         // check for relation
     } elseif ($this->_structure->hasRelation($key) && !$this->_isNew) {
         $mr = ModelRelation::getInstanceForModel($this);
         $mr->_loadRelated($this, $key);
     } elseif ($methodInfo = ModelOperator::getInstanceAbilityMethod($this->_name, $method)) {
         if (empty($params)) {
             $params = array();
         }
         array_unshift($params, $this);
         call_user_func_array(array($methodInfo['ability'], $method), $params);
     }
     if (array_key_exists($key, $this->_data)) {
         return $this->_data[$key];
     } else {
         return $default;
     }
 }
예제 #4
0
 private function __construct($modelName)
 {
     $this->_modelName = $modelName;
     $this->_modelStructure = ModelStructure::getInstanceForModel($modelName);
     $this->_primaryKey = $this->_modelStructure->getPrimaryKey();
     $this->_tableName = $this->_modelStructure->getTableName();
     $this->reloadConfig();
 }
예제 #5
0
 protected static function putTestContent()
 {
     QC::executeSQL('SET FOREIGN_KEY_CHECKS = 0');
     QC::executeSQL('DROP TABLE IF EXISTS brands');
     $storagePath = __DIR__ . '/storage/';
     $mo = ModelOperator::getInstance($storagePath);
     $mo->generateBasicStructure('Brand');
     ModelStructure::getInstanceForModel('Brand')->addAbility('slug')->saveStructure();
     $mo->generateAllModelClasses();
     require_once $storagePath . 'bases/BaseBrand.php';
     require_once $storagePath . 'classes/Brand.php';
     $mo->updateDBForAllModels();
 }
예제 #6
0
 protected static function putTestContent()
 {
     QC::executeSQL('SET FOREIGN_KEY_CHECKS = 0');
     QC::executeSQL('DROP TABLE IF EXISTS brands');
     $storagePath = __DIR__ . '/storage/';
     $mo = ModelOperator::getInstance($storagePath);
     $mo->generateBasicStructure('User');
     ModelStructure::getInstanceForModel('User')->dropColumn('title')->addColumn('name', array('type' => 'varchar(255)', 'validation' => array('CustomName' => array('class' => 'Solve\\Database\\Tests\\CustomNameValidationRule', 'error' => 'invalid name')), 'process' => array('trim')))->addColumn('email', array('type' => 'varchar(255)'))->saveStructure();
     $mo->generateDataProcessorRules('User');
     $mo->generateAllModelClasses();
     require_once $storagePath . 'bases/BaseUser.php';
     require_once $storagePath . 'classes/User.php';
     $mo->updateDBForAllModels();
 }
예제 #7
0
 protected static function putTestContent()
 {
     QC::executeSQL('SET FOREIGN_KEY_CHECKS = 0');
     QC::executeSQL('DROP TABLE IF EXISTS brands');
     $storagePath = __DIR__ . '/storage/';
     $mo = ModelOperator::getInstance($storagePath);
     $mo->generateBasicStructure('Brand');
     ModelStructure::getInstanceForModel('Brand')->addAbility('files', array('logo' => array('name' => 'original', 'multiple' => true), 'info' => array(), 'avatar' => array('sizes' => array('small' => array('size' => '100x100', 'method' => 'fitOut')))))->saveStructure();
     $mo->generateAllModelClasses();
     require_once $storagePath . 'bases/BaseBrand.php';
     require_once $storagePath . 'classes/Brand.php';
     $webRoot = __DIR__ . '/upload/';
     FilesAbility::setBaseStoreLocation($webRoot);
     FSService::setWebRoot($webRoot);
     FSService::unlinkRecursive($webRoot);
     $mo->updateDBForAllModels();
 }
예제 #8
0
 protected static function putTestContent()
 {
     QC::executeSQL('SET FOREIGN_KEY_CHECKS = 0');
     QC::executeSQL('DROP TABLE IF EXISTS products');
     QC::executeSQL('DROP TABLE IF EXISTS brands');
     QC::executeSQL('DROP TABLE IF EXISTS brands_translate');
     $storagePath = __DIR__ . '/storage/';
     $mo = ModelOperator::getInstance($storagePath);
     $mo->generateBasicStructure('Product');
     $mo->generateBasicStructure('Brand');
     ModelStructure::getInstanceForModel('Product')->addColumn('id_brand', array('type' => 'int(11) unsigned'))->addRelation('brand')->saveStructure();
     ModelStructure::getInstanceForModel('Brand')->addRelation('products')->addAbility('translate', array('columns' => array('title')))->saveStructure();
     $mo->generateAllModelClasses();
     require_once $storagePath . 'bases/BaseProduct.php';
     require_once $storagePath . 'bases/BaseBrand.php';
     require_once $storagePath . 'classes/Product.php';
     require_once $storagePath . 'classes/Brand.php';
     ModelOperator::getAbilityInstanceForModel('Brand', 'Translate')->cleanup();
     $mo->updateDBForAllModels();
 }
예제 #9
0
 private function _postLoad()
 {
     $abilities = $this->_structure->getAbilities();
     if (empty($abilities)) {
         $abilities = array();
     }
     foreach ($abilities as $abilityName => $abilityInfo) {
         ModelOperator::getAbilityInstanceForModel($this->_modelClass, $abilityName)->postLoad($this);
     }
     $relations = $this->_structure->getRelations();
     if (empty($relations)) {
         $relations = array();
     }
     foreach ($relations as $relationName => $relationInfo) {
         if (!empty($relationInfo['autoload'])) {
             $this->loadRelated($relationName);
         }
     }
     $this->postLoad();
 }
예제 #10
0
    protected static function putTestContent()
    {
        QC::executeSQL('DROP TABLE IF EXISTS products');
        QC::executeSQL('DROP TABLE IF EXISTS categories');
        $storagePath = __DIR__ . '/storage/';
        $mo = ModelOperator::getInstance($storagePath);
        $mo->generateBasicStructure('Product');
        $mo->generateBasicStructure('Category');
        ModelStructure::getInstanceForModel('Product')->addColumn('id_category', array('type' => 'int(11) unsigned'))->addRelation('category')->addRelation('category_title', array('table' => 'categories', 'fields' => array('title'), 'use' => 'title', 'type' => 'many_to_one', 'local_field' => 'id_category'))->saveStructure();
        ModelStructure::getInstanceForModel('Category')->addRelation('products')->saveStructure();
        $mo->generateAllModelClasses();
        require_once $storagePath . 'bases/BaseProduct.php';
        require_once $storagePath . 'bases/BaseCategory.php';
        require_once $storagePath . 'classes/Product.php';
        require_once $storagePath . 'classes/Category.php';
        $mo->updateDBForAllModels();
        QC::create('products')->insert(array('title' => 'Macbook Air', 'id_category' => 1))->execute();
        QC::create('products')->insert(array('title' => 'Macbook Pro', 'id_category' => 1))->execute();
        QC::create('products')->insert(array('title' => 'iMac 27"', 'id_category' => 2))->execute();
        QC::create('categories')->insert(array('title' => 'Notebooks'))->execute();
        QC::create('categories')->insert(array('title' => 'Computers'))->execute();
        $categoriesCollectionText = <<<TEXT
<?php
use Solve\\Database\\Models\\ModelCollection;

class CategoriesCollection extends ModelCollection {

    public function getProductsCount() {
        return 12;
    }

}
TEXT;
        file_put_contents($storagePath . 'classes/CategoriesCollection.php', $categoriesCollectionText);
        require_once $storagePath . 'classes/CategoriesCollection.php';
    }
예제 #11
0
 public function addAbilityAction()
 {
     $modelName = $this->getFirstParamOrAsk('Enter model name');
     $abilityName = $this->ask('Enter ability to add');
     $structure = ModelStructure::getInstanceForModel($modelName);
     $structure->addAbility($abilityName);
     $structure->saveStructure();
 }
예제 #12
0
 /**
  * @param string $modelName
  * @param string $relationName
  * @return array
  * @throws \Exception if not found relation info
  */
 public static function calculateRelationVariables($modelName, $relationName)
 {
     $relationName = Inflector::underscore($relationName);
     if (!empty(self::$_variablesCache[$modelName . $relationName])) {
         return self::$_variablesCache[$modelName . $relationName];
     }
     $modelStructure = ModelStructure::getInstanceForModel($modelName);
     $relationInfo = $modelStructure->getRelationInfo($relationName);
     if (is_null($relationInfo)) {
         throw new \Exception('Relation ' . $relationName . ' is not found, probably capitalization');
     }
     if (!is_array($relationInfo)) {
         $relationInfo = array();
     }
     $info = array();
     $info['localTable'] = $modelStructure->getTableName();
     foreach ($relationInfo as $key => $value) {
         $info[lcfirst(Inflector::camelize($key))] = $value;
     }
     if (!isset($info['model']) && !isset($info['table'])) {
         $info['model'] = ucfirst(Inflector::singularize($relationName));
     }
     if (isset($info['model'])) {
         $relatedStructure = ModelStructure::getInstanceForModel($info['model']);
         $info['foreignTable'] = $relatedStructure->getTableName();
         $info['relatedModelName'] = $info['model'];
         $info['hydration'] = 'model';
         unset($info['model']);
     } else {
         $info['relatedModelName'] = ucfirst(Inflector::singularize($info['table']));
         $info['foreignTable'] = $info['table'];
         unset($info['table']);
         $info['hydration'] = 'simple';
         if (empty($info['fields'])) {
             $info['fields'] = '*';
         }
         $info['fieldsToRetrieve'] = $info['fields'];
         unset($info['fields']);
         $relatedStructure = null;
     }
     if (!isset($info['localKey'])) {
         $info['localKey'] = $modelStructure->getPrimaryKey();
     }
     if (!isset($info['foreignKey'])) {
         $info['foreignKey'] = empty($relatedStructure) ? 'id' : $relatedStructure->getPrimaryKey();
     }
     $foreignTable = $relatedStructure ? $relatedStructure->getTableName() : (isset($info['table']) ? $info['table'] : $relationName);
     $autoLocalField = 'id_' . Inflector::underscore($info['relatedModelName']);
     $autoForeignField = 'id_' . Inflector::underscore($modelName);
     $hasLocalField = false;
     $hasForeignField = false;
     if ($info['hydration'] == 'model') {
         if ($relatedStructure->hasColumn($autoForeignField)) {
             if (empty($info['type'])) {
                 $info['type'] = 'one_to_many';
             }
             if (empty($info['foreignField'])) {
                 $info['foreignField'] = $autoForeignField;
             }
             $hasForeignField = true;
         }
         if ($modelStructure->hasColumn($autoLocalField)) {
             if (empty($info['type'])) {
                 $info['type'] = 'many_to_one';
             }
             if (empty($info['localField'])) {
                 $info['localField'] = $autoLocalField;
             }
             $hasLocalField = true;
         }
         if (!$hasForeignField && !$hasLocalField) {
             if (empty($info['type'])) {
                 $info['type'] = 'many_to_many';
             }
             if (empty($info['foreignField'])) {
                 $info['foreignField'] = $autoForeignField;
             }
             if (empty($info['localField'])) {
                 $info['localField'] = $autoLocalField;
             }
         }
     }
     $info['relationToMany'] = substr($info['type'], -4) == 'many' ? true : false;
     $info['relationType'] = $info['type'];
     unset($info['type']);
     if (strpos($info['relatedModelName'], '\\') === false) {
         $info['relatedModelName'] = 'Entities\\' . $info['relatedModelName'];
     }
     if (empty($info['manyTable'])) {
         $info['manyTable'] = $info['localTable'] > $foreignTable ? $info['localTable'] . '_' . $foreignTable : $foreignTable . '_' . $info['localTable'];
     }
     self::$_variablesCache[$modelName . $relationName] = $info;
     return $info;
 }