/**
  * Adds a join for a belongs to relation to the statement
  * @param zibo\library\database\manipulation\expression\TableExpression $table
  * @param zibo\library\database\manipulation\expression\TableExpression $relationTable
  * @param string $fieldName
  * @return null
  */
 private function addBelongsToJoin(TableExpression $table, TableExpression $relationTable, $fieldName)
 {
     $expressionForeignKey = new FieldExpression($fieldName, $table, self::ALIAS_SELF . self::ALIAS_SEPARATOR . $fieldName);
     $expressionPrimaryKey = new FieldExpression(ModelTable::PRIMARY_KEY, $relationTable, $fieldName . self::ALIAS_SEPARATOR . ModelTable::PRIMARY_KEY);
     $joinCondition = new SimpleCondition($expressionForeignKey, $expressionPrimaryKey, Condition::OPERATOR_EQUALS);
     $join = new JoinExpression(JoinExpression::TYPE_LEFT, $relationTable, $joinCondition);
     $this->tables[self::ALIAS_SELF]->addJoin($join);
     $relationLocalizedTable = $this->meta->getRelationLocalizedTable($fieldName);
     if ($relationLocalizedTable === null) {
         return;
     }
     $joinCondition = $this->createLocalizeCondition($relationTable, $relationLocalizedTable);
     $join = new JoinExpression(JoinExpression::TYPE_LEFT, $relationLocalizedTable, $joinCondition);
     $this->tables[self::ALIAS_SELF]->addJoin($join);
     if ($this->recursiveDepth == 1) {
         $localeField = new FieldExpression(LocalizedModel::FIELD_LOCALE, $relationLocalizedTable, $fieldName . self::ALIAS_SEPARATOR . LocalizedModel::FIELD_LOCALE);
         $this->statement->addField($localeField);
     }
 }
 public function testSelectStatementWithOrder()
 {
     $table = new TableExpression('table', 't');
     $field1 = new FieldExpression('field1', $table, 'f1');
     $field2 = new FieldExpression('field2', $table);
     $field3 = new FieldExpression('field3');
     $statement = new SelectStatement();
     $statement->addField($field1);
     $statement->addField($field2);
     $statement->addField($field3);
     $statement->addTable($table);
     $statement->addOrderBy(new OrderExpression($field1));
     $statement->addOrderBy(new OrderExpression($field2, OrderExpression::DIRECTION_DESC));
     $statement->addOrderBy(new OrderExpression($field3));
     $sql = $this->parser->parseStatement($statement);
     $this->assertNotNull($sql);
     $this->assertEquals('SELECT `t`.`field1` AS `f1`, `t`.`field2`, `field3` FROM `table` AS `t` ORDER BY `f1` ASC, `t`.`field2` DESC, `field3` ASC', $sql);
 }
 /**
  * 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;
 }