/** * Sets the model query to this parser and initializes the parser for this model query. * @param zibo\library\orm\query\ModelQuery $modelQuery * @return null */ private function setModelQuery(ModelQuery $modelQuery) { $this->meta = $modelQuery->getModel()->getMeta(); $this->recursiveDepth = $modelQuery->getRecursiveDepth(); $this->localize = false; $this->locale = $modelQuery->getLocale(); $this->includeUnlocalizedData = $modelQuery->willIncludeUnlocalizedData(); $this->addIsLocalizedOrder = $modelQuery->willAddIsLocalizedOrder(); $this->fields = array(); $this->recursiveBelongsToFields = array(); $this->recursiveHasFields = array(); $this->tables = array(); $this->fieldJoins = array(); $this->conditionJoins = array(); $this->addTable($this->meta->getName(), self::ALIAS_SELF); $this->addTable($this->meta->getName() . LocalizedModel::MODEL_SUFFIX, self::ALIAS_SELF_LOCALIZED); $this->statement = new SelectStatement(); $this->statement->addTable($this->tables[self::ALIAS_SELF]); $joins = $modelQuery->getJoins(); foreach ($joins as $join) { $table = $join->getTable(); $this->addTable($table->getModelName(), $table->getAlias()); } }
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; }