Exemple #1
0
 private function addJoin(ModelJoin $modelJoin)
 {
     if (!$this->isAlreadyJoined($modelJoin)) {
         $this->_query->addJoin($modelJoin->asJoinClause());
         $this->_joinedModels[] = $modelJoin;
         if ($modelJoin->storeField()) {
             $this->selectModelColumns($modelJoin->getModelObject(), $modelJoin->alias());
         }
     }
 }
Exemple #2
0
 /**
  * @test
  */
 public function shouldHandleSubQueries()
 {
     //given
     Product::create(array('name' => 'prod1', 'description' => 'd'));
     Product::create(array('name' => 'prod1', 'description' => 'd'));
     Product::create(array('name' => 'prod2', 'description' => 'd'));
     $query = Query::select(array('count(*) AS c'))->from(Query::select(array('name', 'count(*) c'))->from('products')->groupBy('name')->where(array('description' => 'd')), 'sub')->where(array('c' => 2));
     $executor = QueryExecutor::prepare(Db::getInstance(), $query);
     //when
     $result = $executor->fetch();
     //then
     $this->assertEquals(array('c' => 1), $result);
 }
Exemple #3
0
 public static function update($attributes)
 {
     return Query::newInstance(QueryType::$UPDATE)->attributes($attributes);
 }
Exemple #4
0
 public function update()
 {
     $this->_callBeforeSaveCallbacks();
     $attributes = $this->getAttributesForUpdate();
     if ($attributes) {
         $query = Query::update($attributes)->table($this->_modelDefinition->table)->where(array($this->_modelDefinition->primaryKey => $this->getId()));
         QueryExecutor::prepare($this->_modelDefinition->db, $query)->execute();
     }
     $this->_callAfterSaveCallbacks();
 }
Exemple #5
0
 /**
  * @group non-sqlite3
  * @test
  */
 public function selectShouldNotLockForUpdateByDefault()
 {
     // when
     $query = Query::select();
     // then
     $this->assertFalse($query->lockForUpdate);
 }
Exemple #6
0
 /**
  * @test
  */
 public function shouldReturnSelectWithMultipleJoinsWithAliases()
 {
     //given
     $query = new Query();
     $query->table = 'products';
     $query->join('categories', 'id_category', 'id_category', 'c')->join('orders', 'id', 'id_product', 'o')->where('id = ?', 1);
     //when
     $sql = $this->_dialect->buildQuery($query);
     //then
     $expected = 'SELECT * FROM products LEFT JOIN categories AS c ON c.id_category = products.id_category LEFT JOIN orders AS o ON o.id = products.id_product WHERE id = ?';
     $this->assertEquals($expected, $sql);
 }
 /**
  * @test
  */
 public function shouldBuildUpdateQuery()
 {
     //given
     $query = new Query();
     $query->table = 'products';
     $query->type = QueryType::$UPDATE;
     $query->updateAttributes = array('col1' => 'val1', 'col2' => 'val2');
     $query->where(array('col1' => 'prev1', 'col2' => 'prev2'));
     //when
     $sql = $this->dialect->buildQuery($query);
     //then
     $this->assertEquals("UPDATE products SET col1 = ?, col2 = ? WHERE col1 = ? AND col2 = ?", $sql);
 }