Exemplo n.º 1
0
 public function onBeforeUpdate()
 {
     /** @var $model \Mindy\Orm\TreeModel */
     $model = $this->getModel();
     // Случай когда обнулен slug, например из админки
     if (empty($model->{$this->name})) {
         $model->{$this->name} = Meta::cleanString($model->{$this->source});
     }
     // if remove parent (parent is null)
     if (!$model->parent) {
         if (strpos($model->{$this->name}, '/') === false) {
             $url = Meta::cleanString($model->{$this->name});
         } else {
             if ($model->{$this->name}) {
                 $slugs = explode('/', $model->{$this->name});
                 $url = end($slugs);
             } else {
                 $url = Meta::cleanString($model->{$this->source});
             }
         }
     } else {
         $parentUrl = $model->parent->{$this->name};
         $slugs = explode('/', $model->{$this->name});
         $url = $parentUrl . '/' . end($slugs);
     }
     $url = ltrim($url, '/');
     if ($this->unique) {
         $url = $this->uniqueUrl($url, 0, $model->pk);
     }
     $model->setAttribute($this->name, $url);
     $schema = ConnectionManager::getDb()->getSchema();
     $model->tree()->filter(['lft__gt' => $model->getOldAttribute('lft'), 'rgt__lt' => $model->getOldAttribute('rgt'), 'root' => $model->getOldAttribute('root')])->update([$this->name => new Expression("REPLACE(" . $schema->quoteColumnName($this->name) . ", :from, :to)", [':from' => $model->getOldAttribute($this->name), ':to' => $url])]);
 }
 public function testSimple()
 {
     $categoryToys = new Category(['name' => 'Toys']);
     $this->assertTrue($categoryToys->getIsNewRecord());
     $categoryToys->save();
     $this->assertFalse($categoryToys->getIsNewRecord());
     $category_animals = new Category();
     $category_animals->name = 'Animals';
     $category_animals->save();
     $db = ConnectionManager::getDb();
     $tableSql = $db->schema->quoteColumnName('tests_product');
     $tableAliasSql = $db->schema->quoteColumnName('tests_product_1');
     $categoryIdSql = $db->schema->quoteColumnName('category_id');
     $this->assertEquals("SELECT COUNT(*) FROM {$tableSql} {$tableAliasSql} WHERE ({$tableAliasSql}.{$categoryIdSql}='1')", $categoryToys->products->countSql());
     $this->assertEquals(0, $categoryToys->products->count());
     $product_bear = new Product(['category' => $categoryToys, 'name' => 'Bear', 'price' => 100, 'description' => 'Funny white bear']);
     $product_bear->save();
     $this->assertEquals(1, $categoryToys->products->count());
     $product_rabbit = new Product(['category' => $category_animals, 'name' => 'Rabbit', 'price' => 110, 'description' => 'Rabbit with carrot']);
     $product_rabbit->save();
     $this->assertEquals(1, $categoryToys->products->count());
     $product_rabbit->category = $categoryToys;
     $product_rabbit->save();
     $this->assertEquals(2, $categoryToys->products->count());
 }
Exemplo n.º 3
0
 public function __construct($models)
 {
     if (!is_array($models)) {
         $models = [$models];
     }
     $this->_models = $models;
     $this->db = ConnectionManager::getDb();
 }
Exemplo n.º 4
0
 public function getValue()
 {
     $db = ConnectionManager::getDb()->getQueryBuilder();
     if (is_numeric($this->value) || $this->autoNowAdd && $this->getModel()->getIsNewRecord() || $this->autoNow) {
         return $db->convertToDateTime($this->value);
     } else {
         return $this->value;
     }
 }
Exemplo n.º 5
0
 public function testOrExclude()
 {
     $qs = User::objects()->exclude(['username' => 'Max'])->orExclude(['username' => 'Anton']);
     $this->assertEquals(2, $qs->count());
     $db = ConnectionManager::getDb();
     $tableSql = $db->schema->quoteColumnName('tests_user');
     $tableAliasSql = $db->schema->quoteColumnName('tests_user_1');
     $usernameSql = $db->schema->quoteColumnName('username');
     $this->assertEquals("SELECT COUNT(*) FROM {$tableSql} {$tableAliasSql} WHERE (NOT (({$tableAliasSql}.{$usernameSql}='Max'))) OR (NOT (({$tableAliasSql}.{$usernameSql}='Anton')))", $qs->countSql());
 }
Exemplo n.º 6
0
 /**
  * @return null|string
  * @throws \Mindy\Query\Exception\UnknownDatabase
  */
 public function getDbPrepValue()
 {
     $db = ConnectionManager::getDb();
     if ($db->getSchema() instanceof \Mindy\Query\Pgsql\Schema) {
         /*
          * Primary key всегда передается по логике Query, а для корректной работы pk в pgsql
          * необходимо передать curval($seq) или nextval($seq) или не экранированный DEFAULT.
          */
         return new Expression("DEFAULT");
     } else {
         return parent::getDbPrepValue();
     }
 }
 public function getQuerySet()
 {
     $db = ConnectionManager::getDb();
     if ($this->_qs === null) {
         $qs = parent::getQuerySet();
         $this->relatedTableAlias = $qs->makeAliasKey($this->relatedTable);
         $qs->join('JOIN', $this->relatedTable . ' ' . $this->relatedTableAlias, $this->makeOnJoin($qs));
         $this->_qs = $qs->filter([$this->relatedTableAlias . '.' . $db->schema->quoteColumnName($this->primaryModelColumn) => $this->primaryModel->pk]);
         if (!empty($this->extra)) {
             $this->_qs->filter($this->extra);
         }
     }
     return $this->_qs;
 }
Exemplo n.º 8
0
 public function getRelatedTable($clean = true)
 {
     $tableName = $this->getRelatedModel()->tableName();
     $schema = ConnectionManager::getDb()->getSchema();
     return $clean ? $schema->getRawTableName($tableName) : $tableName;
 }
Exemplo n.º 9
0
 /**
  * https://github.com/studio107/Mindy_Query/issues/11
  * Issue #11
  */
 public function testIssue11()
 {
     // Fix hhvm test
     date_default_timezone_set('UTC');
     $this->initModels([new Solution()]);
     list($modelOne, $created) = Solution::objects()->getOrCreate(['status' => 1, 'name' => 'test', 'court' => 'qwe', 'question' => 'qwe', 'result' => 'qwe', 'content' => 'qwe']);
     $this->assertEquals(1, $modelOne->pk);
     $sql = Solution::objects()->filter(['id' => '1'])->updateSql(['status' => 2]);
     $db = ConnectionManager::getDb();
     $tableSql = $db->schema->quoteColumnName('tests_solution');
     $statusSql = $db->schema->quoteColumnName('status');
     $idSql = $db->schema->quoteColumnName('id');
     $this->assertEquals("UPDATE {$tableSql} SET {$statusSql}=2 WHERE ({$idSql}='1')", $sql);
     $this->dropmodels([new Solution()]);
 }
Exemplo n.º 10
0
 /**
  * Returns the database connection used by this AR class.
  * By default, the "db" application component is used as the database connection.
  * You may override this method if you want to use a different database connection.
  * @return \Mindy\Query\Connection the database connection used by this AR class.
  */
 public static function getDb()
 {
     return ConnectionManager::getDb();
 }
 public function testExtraLinkRecords()
 {
     $product = new Product();
     $product->name = 'Bear';
     $product->price = 100;
     $product->description = 'Funny white bear';
     $product->save();
     $list1 = new ProductList();
     $list1->name = 'Toys';
     $list1->save();
     $list2 = new ProductList();
     $list2->name = 'Trash';
     $list2->save();
     $this->assertEquals(1, Product::objects()->count());
     $this->assertEquals(2, ProductList::objects()->count());
     $tableName = $product->getField('lists')->getTableName();
     $cmd = ConnectionManager::getDb()->createCommand("SELECT * FROM {$tableName}");
     $all = $cmd->queryAll();
     $this->assertEquals([], $all);
     $this->assertEquals(0, count($all));
     $product->lists = [$list1];
     $product->save();
     $cmd = ConnectionManager::getDb()->createCommand("SELECT * FROM {$tableName}");
     $all = $cmd->queryAll();
     $this->assertEquals([['product_id' => 1, 'product_list_id' => 1]], $all);
     $this->assertEquals(1, count($all));
     $product->lists = [$list2];
     $product->save();
     $cmd = ConnectionManager::getDb()->createCommand("SELECT * FROM {$tableName}");
     $all = $cmd->queryAll();
     $this->assertEquals([['product_id' => 1, 'product_list_id' => 2]], $all);
     $this->assertEquals(1, count($all));
     $product->lists = [$list1];
     $product->save();
     $cmd = ConnectionManager::getDb()->createCommand("SELECT * FROM {$tableName}");
     $all = $cmd->queryAll();
     $this->assertEquals([['product_id' => 1, 'product_list_id' => 1]], $all);
     $this->assertEquals(1, count($all));
     $product->lists = [$list1, $list1, $list1];
     $product->save();
     $cmd = ConnectionManager::getDb()->createCommand("SELECT * FROM {$tableName}");
     $all = $cmd->queryAll();
     $this->assertEquals([['product_id' => 1, 'product_list_id' => 1], ['product_id' => 1, 'product_list_id' => 1], ['product_id' => 1, 'product_list_id' => 1]], $all);
     $this->assertEquals(3, count($all));
 }
Exemplo n.º 12
0
 public function getConnectionType()
 {
     $params = explode(':', ConnectionManager::getDb()->dsn);
     return array_pop($params);
 }
Exemplo n.º 13
0
 public function sqlDefault()
 {
     $queryBuilder = ConnectionManager::getDb()->getQueryBuilder();
     $default = $queryBuilder->convertToBoolean($this->default);
     return $this->default === null ? '' : "DEFAULT {$default}";
 }