public function testSelectStatementWithCondition()
 {
     $table1 = new TableExpression('table1');
     $table2 = new TableExpression('table2_with_long_name', 'table2');
     $field = new FieldExpression('field1', $table2, 'f2');
     $condition = new SimpleCondition($field, new ScalarExpression('test'), '=');
     $statement = new SelectStatement();
     $statement->addField(new FieldExpression('field1', $table1, 'f1'));
     $statement->addField($field);
     $statement->addTable($table1);
     $statement->addTable($table2);
     $statement->addCondition($condition);
     $sql = $this->parser->parseStatement($statement);
     $this->assertNotNull($sql);
     $this->assertEquals('SELECT `table1`.`field1` AS `f1`, `table2`.`field1` AS `f2` FROM `table1`, `table2_with_long_name` AS `table2` WHERE `table2`.`field1` = \'test\'', $sql);
 }
예제 #2
0
 /**
  * Adds the condition expressions to the statement
  * @param array $condition Array with database condition objects
  * @param string $operator Operator for the nested conditions
  * @return null
  */
 private function addConditions(array $conditions, $operator)
 {
     $numConditions = count($conditions);
     if ($numConditions === 0) {
         return;
     }
     if ($numConditions === 1) {
         $this->statement->addCondition(array_shift($conditions));
         return;
     }
     $nestedCondition = new NestedCondition();
     foreach ($conditions as $condition) {
         $nestedCondition->addCondition($condition, $operator);
     }
     $this->statement->addCondition($nestedCondition);
 }
예제 #3
0
 /**
  * Gets the primary keys of the has many values
  * @param Model $model Model of the has many field
  * @param string $foreignKey Name of the foreign key to this model
  * @param integer $id Value for the foreign key
  * @return array Array with the primary key of the has many value as key and value
  */
 private function findOldHasManyAndBelongsTo($model, $foreignKey, $id)
 {
     $condition = new SimpleCondition(new FieldExpression($foreignKey), new ScalarExpression($id), Condition::OPERATOR_EQUALS);
     $statement = new SelectStatement();
     $statement->addTable(new TableExpression($model->getName()));
     $statement->addField(new FieldExpression(ModelTable::PRIMARY_KEY));
     $statement->addCondition($condition);
     $result = $this->executeStatement($statement);
     $model->clearCache();
     $oldHasMany = array();
     foreach ($result as $record) {
         $oldHasMany[$record[ModelTable::PRIMARY_KEY]] = $record[ModelTable::PRIMARY_KEY];
     }
     return $oldHasMany;
 }