public function testForTest() { TranslateAbility::setLanguageId(1); $brand = new \Brand(array('title' => 'Apple')); $this->assertEquals('Apple', $brand->title, 'Accessor after creating'); $brand->save(); $this->assertEquals('Apple', $brand->title, 'Accessor after saving'); $brand = \Brand::loadOne(1); $this->assertEquals('Apple', $brand->title, 'Accessor after loading'); $this->assertEquals(array('id_language' => 1, 'id_object' => 1, 'title' => 'Apple'), QC::create('brands_translate')->executeOne(), 'Data stored in DB after saving'); TranslateAbility::setLanguageId(2); $brand->title = 'Яблоко'; $brand->save(); $this->assertEquals('Яблоко', $brand->title, 'Accessor after loading'); $this->assertEquals(array('id_language' => 2, 'id_object' => 1, 'title' => 'Яблоко'), QC::create('brands_translate')->where('id_language = :d', 2)->executeOne(), 'Data stored in DB after saving'); $brand->loadTranslation(1); $this->assertEquals('Apple', $brand->title, 'loadTranslation works'); $this->assertEquals(array('id' => 1, 'id_object' => 1, 'id_language' => 2, 'title' => 'Яблоко'), $brand->getTranslationForLanguage(2), 'getTranslationForLanguage'); TranslateAbility::setLanguageId(1); $brand2 = new \Brand(); $brand2->title = 'Samsung'; $brand2->save(); $brand2->loadTranslation(2); $this->assertEmpty($brand2->title, 'Empty data for not translated item'); $brands = \Brand::loadList(); $this->assertEquals(array('Apple', 'Samsung'), $brands->getFieldArray('title'), 'Loaded two translated items'); $brands->loadTranslation(2); $this->assertEquals(array('Яблоко', null), $brands->getFieldArray('title'), 'Loaded two not fully translated items'); $this->assertEquals(array(1 => array('id' => 1, 'id_object' => 1, 'id_language' => 1, 'title' => 'Apple'), 2 => array('id' => 1, 'id_object' => 1, 'id_language' => 2, 'title' => 'Яблоко')), $brand->getAllTranslations(), 'getAllTranslations()'); }
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); }
public function testComplexConditions() { $qc = new QC('users'); $qc->and('age < :d', 10)->use('name')->indexBy('id')->orderBy('id DESC')->limit(1, 2)->groupBy('age'); $this->assertEquals('SELECT * FROM `users` WHERE ((age < 10)) GROUP BY `age` ORDER BY id DESC LIMIT 1, 2', $qc->getSQL(), ''); $sql = QC::create('users')->where('age > :d', 12)->update('title = :s and id = :d', 'new title', 1)->getSQL(); $this->assertEquals('UPDATE `users` SET title = \'new title\' and id = 1 WHERE ((age > 12))', $sql, 'multiple update with parameters'); }
public function defaultAction() { $this->view->name = 'Alexandr'; $this->view->city = 'Kiev'; var_dump(QC::create('users')->execute()); // $this->view->setVar('name', 'AlexandrHTML', View::FORMAT_HTML); // $this->view->setNoLayout(); // $this->view->setStandaloneTemplate('index/default'); // echo "index default \n"; // $this->forwardToRoute('test'); // $this->redirectToUri('about/'); // $this->redirectSelf(); // var_dump($this->view->fetchTemplate('index/default'));die(); }
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(); }
public function testBasicOperations() { $data = QC::create('users')->execute(); $this->assertEmpty($data, 'Empty select'); $id = QC::create('users')->insert(array('age' => 10, 'name' => 'Alexandr', 'position' => 'developer'))->execute(); $this->assertEquals(1, $id, 'Insert returns id 1'); QC::create('users')->insert(array('age' => 11, 'name' => 'Sergey', 'position' => 'developer'))->execute(); $data = QC::create('users')->select('id, name')->execute(); $this->assertEquals(array(array('id' => '1', 'name' => 'Alexandr'), array('id' => '2', 'name' => 'Sergey')), $data, 'select fields of two simple rows'); $data = QC::create('users')->select('name')->where('id = 1')->executeOne(); $this->assertEquals(array('name' => 'Alexandr'), $data, 'Select one value'); $data = QC::create('users')->select('id, name, position')->indexBy('id')->foldBy('position')->use('name')->execute(); $this->assertEquals(array('developer' => array('1' => 'Alexandr', '2' => 'Sergey')), $data, 'Select with foldBy, indexBy and use modifiers'); $data = QC::create()->rawSelect('select id,name from users')->indexBy('id')->use('name')->execute(); $this->assertEquals(array(1 => 'Alexandr', 2 => 'Sergey'), $data, 'Custom select with modifiers'); $countDeleted = QC::create('users')->delete('id < :d', 3)->execute(); $this->assertEquals(2, $countDeleted, 'Deleted 2 items'); }
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'; }
protected function processRequestFilters(QC $qc) { // paging $page = $this->request->getVar('page', 1); $count = $this->request->getVar('count', 10); $qc->limit(($page - 1) * $count, $count); // sorting $sorting = $this->request->getVar('sorting', null); if (is_array($sorting)) { foreach ($sorting as $key => $type) { $qc->orderBy($key . ' ' . ($type == 'asc' ? 'ASC' : 'DESC')); } } $this->setData(array('total' => QC::create('users')->select('count(*) cnt')->use('cnt')->executeOne(), 'page' => $page, 'count' => $count), 'paging'); $this->setData($sorting, 'sorting'); }
public function delete() { if ($this->isNew()) { return false; } $this->_preDelete(); unset($this); return QC::create($this->_tableName)->delete(array($this->_primaryKey => $this->getID()))->execute(); }
/** * Load data from YML files into database with replacing * @param string|array $models */ public function dataLoad($models = null) { if (!empty($models)) { if (!is_array($models)) { $models = array($models); } } else { $models = array_keys($this->_structures); } $data_dir = $this->_storagePath . 'data/'; foreach ($models as $model) { $model = ucfirst($model); $file_name = $data_dir . strtolower($model) . '.yml'; if (!isset($this->_structures[$model]) || !is_file($file_name)) { continue; } $data = Yaml::parse(file_get_contents($file_name)); if (!is_array($data)) { continue; } QC::executeSQL('SET FOREIGN_KEY_CHECKS=0'); QC::executeSQL('TRUNCATE `' . $this->_structures[$model]['table'] . '`'); QC::create($this->_structures[$model]['table'])->insert($data)->execute(); } }
public function delete() { QC::create($this->_tableName)->delete(array($this->_primaryKey => $this->getIDs()))->execute(); }
/** * @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; }
public function preDelete($caller) { $ids = ModelOperator::getIDs($caller); QC::create($this->_tableName)->delete(array('id_object' => $ids))->execute(); }