Example #1
0
 public static function getFields()
 {
     return ['name' => ['class' => CharField::className(), 'default' => 'Product', 'validators' => [function ($value) {
         if (mb_strlen($value, 'UTF-8') < 3) {
             return "Minimal length < 3";
         }
         return true;
     }]], 'price' => ['class' => CharField::className(), 'default' => 0], 'description' => ['class' => TextField::className(), 'null' => true], 'category' => ['class' => ForeignField::className(), 'modelClass' => Category::className(), 'null' => true, 'relatedName' => 'products'], 'lists' => ['class' => ManyToManyField::className(), 'modelClass' => ProductList::className()]];
 }
 public function testIssue64()
 {
     $this->assertEquals(1, Category::objects()->count());
     $this->assertEquals(2, Product::objects()->count());
     $this->assertEquals(2, ProductList::objects()->count());
     $category = Category::objects()->get();
     $this->assertEquals(2, $category->products->count());
     $this->assertEquals(1, $category->products->filter(['name' => 'bar'])->count());
     $this->assertEquals(2, $category->products->filter(['lists__name' => 'test'])->count());
     $this->assertEquals(1, $category->products->filter(['lists__name' => 'asd'])->count());
     $this->assertEquals("SELECT `tests_product_1`.* FROM `tests_product` `tests_product_1` LEFT OUTER JOIN `tests_product_tests_product_list` `tests_product_tests_product_list_2` ON `tests_product_1`.`id` = `tests_product_tests_product_list_2`.`product_id` LEFT OUTER JOIN `tests_product_list` `tests_product_list_3` ON `tests_product_tests_product_list_2`.`product_list_id` = `tests_product_list_3`.`id` WHERE ((`tests_product_1`.`category_id`='1')) AND ((`tests_product_list_3`.`name`='asd')) GROUP BY `tests_product_1`.`id`", $category->products->filter(['lists__name' => 'asd'])->allSql());
 }
 public function testIregex()
 {
     $qs = ProductList::objects()->filter(['name__iregex' => '[P-Z]']);
     $this->assertInstanceOf('\\Mindy\\Orm\\Manager', $qs);
     $this->assertEquals("SELECT COUNT(*) FROM `tests_product_list` `tests_product_list_1` WHERE ((`tests_product_list_1`.`name` REGEXP '[P-Z]'))", $qs->countSql());
     $this->assertEquals(1, $qs->count());
     $qs = ProductList::objects()->filter(['name__iregex' => '[0-9]']);
     $this->assertInstanceOf('\\Mindy\\Orm\\Manager', $qs);
     $this->assertEquals("SELECT COUNT(*) FROM `tests_product_list` `tests_product_list_1` WHERE ((`tests_product_list_1`.`name` REGEXP '[0-9]'))", $qs->countSql());
     $this->assertEquals(0, $qs->count());
 }
Example #4
0
 public function testLongTableName()
 {
     $this->assertEquals('{{%tests_product_list}}', ProductList::tableName());
 }
 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));
 }
 public function testIregex()
 {
     $qs = ProductList::objects()->filter(['name__iregex' => '[a-z]']);
     $this->assertEquals(1, $qs->count());
     $this->assertEquals("SELECT COUNT(*) FROM \"tests_product_list\" \"tests_product_list_1\" WHERE ((\"tests_product_list_1\".\"name\" ~* '[a-z]'))", $qs->countSql());
     $qs = ProductList::objects()->filter(['name__iregex' => '[0-9]']);
     $this->assertEquals(0, $qs->count());
     $this->assertEquals("SELECT COUNT(*) FROM \"tests_product_list\" \"tests_product_list_1\" WHERE ((\"tests_product_list_1\".\"name\" ~* '[0-9]'))", $qs->countSql());
 }