|
|
@ -7,27 +7,19 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace yii\sphinx; |
|
|
|
namespace yii\sphinx; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use yii\db\ActiveQueryInterface; |
|
|
|
|
|
|
|
use yii\db\ActiveQueryTrait; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Class ActiveQuery |
|
|
|
* Class ActiveQuery |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Paul Klimov <klimov.paul@gmail.com> |
|
|
|
* @author Paul Klimov <klimov.paul@gmail.com> |
|
|
|
* @since 2.0 |
|
|
|
* @since 2.0 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class ActiveQuery extends Query |
|
|
|
class ActiveQuery extends Query implements ActiveQueryInterface |
|
|
|
{ |
|
|
|
{ |
|
|
|
/** |
|
|
|
use ActiveQueryTrait; |
|
|
|
* @var string the name of the ActiveRecord class. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $modelClass; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var array list of relations that this query should be performed with |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $with; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var boolean whether to return each record as an array. If false (default), an object |
|
|
|
|
|
|
|
* of [[modelClass]] will be created to represent each record. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $asArray; |
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string the SQL statement to be executed for retrieving AR records. |
|
|
|
* @var string the SQL statement to be executed for retrieving AR records. |
|
|
|
* This is set by [[ActiveRecord::findBySql()]]. |
|
|
|
* This is set by [[ActiveRecord::findBySql()]]. |
|
|
@ -35,25 +27,6 @@ class ActiveQuery extends Query |
|
|
|
public $sql; |
|
|
|
public $sql; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* PHP magic method. |
|
|
|
|
|
|
|
* This method allows calling static method defined in [[modelClass]] via this query object. |
|
|
|
|
|
|
|
* It is mainly implemented for supporting the feature of scope. |
|
|
|
|
|
|
|
* @param string $name the method name to be called |
|
|
|
|
|
|
|
* @param array $params the parameters passed to the method |
|
|
|
|
|
|
|
* @return mixed the method return result |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function __call($name, $params) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (method_exists($this->modelClass, $name)) { |
|
|
|
|
|
|
|
array_unshift($params, $this); |
|
|
|
|
|
|
|
call_user_func_array([$this->modelClass, $name], $params); |
|
|
|
|
|
|
|
return $this; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return parent::__call($name, $params); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Executes query and returns all results as an array. |
|
|
|
* Executes query and returns all results as an array. |
|
|
|
* @param Connection $db the DB connection used to create the DB command. |
|
|
|
* @param Connection $db the DB connection used to create the DB command. |
|
|
|
* If null, the DB connection returned by [[modelClass]] will be used. |
|
|
|
* If null, the DB connection returned by [[modelClass]] will be used. |
|
|
@ -65,10 +38,9 @@ class ActiveQuery extends Query |
|
|
|
$rows = $command->queryAll(); |
|
|
|
$rows = $command->queryAll(); |
|
|
|
if (!empty($rows)) { |
|
|
|
if (!empty($rows)) { |
|
|
|
$models = $this->createModels($rows); |
|
|
|
$models = $this->createModels($rows); |
|
|
|
// TODO relations |
|
|
|
if (!empty($this->with)) { |
|
|
|
/*if (!empty($this->with)) { |
|
|
|
|
|
|
|
$this->populateRelations($models, $this->with); |
|
|
|
$this->populateRelations($models, $this->with); |
|
|
|
}*/ |
|
|
|
} |
|
|
|
return $models; |
|
|
|
return $models; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return []; |
|
|
|
return []; |
|
|
@ -95,12 +67,11 @@ class ActiveQuery extends Query |
|
|
|
$class = $this->modelClass; |
|
|
|
$class = $this->modelClass; |
|
|
|
$model = $class::create($row); |
|
|
|
$model = $class::create($row); |
|
|
|
} |
|
|
|
} |
|
|
|
// TODO relations |
|
|
|
if (!empty($this->with)) { |
|
|
|
/*if (!empty($this->with)) { |
|
|
|
|
|
|
|
$models = [$model]; |
|
|
|
$models = [$model]; |
|
|
|
$this->populateRelations($models, $this->with); |
|
|
|
$this->populateRelations($models, $this->with); |
|
|
|
$model = $models[0]; |
|
|
|
$model = $models[0]; |
|
|
|
}*/ |
|
|
|
} |
|
|
|
return $model; |
|
|
|
return $model; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return null; |
|
|
|
return null; |
|
|
@ -134,74 +105,4 @@ class ActiveQuery extends Query |
|
|
|
} |
|
|
|
} |
|
|
|
return $db->createCommand($this->sql, $params); |
|
|
|
return $db->createCommand($this->sql, $params); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Sets the [[asArray]] property. |
|
|
|
|
|
|
|
* @param boolean $value whether to return the query results in terms of arrays instead of Active Records. |
|
|
|
|
|
|
|
* @return static the query object itself |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function asArray($value = true) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->asArray = $value; |
|
|
|
|
|
|
|
return $this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Sets the [[indexBy]] property. |
|
|
|
|
|
|
|
* @param string|callable $column the name of the column by which the query results should be indexed by. |
|
|
|
|
|
|
|
* This can also be a callable (e.g. anonymous function) that returns the index value based on the given |
|
|
|
|
|
|
|
* row or model data. The signature of the callable should be: |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* ~~~ |
|
|
|
|
|
|
|
* // $model is an AR instance when `asArray` is false, |
|
|
|
|
|
|
|
* // or an array of column values when `asArray` is true. |
|
|
|
|
|
|
|
* function ($model) |
|
|
|
|
|
|
|
* { |
|
|
|
|
|
|
|
* // return the index value corresponding to $model |
|
|
|
|
|
|
|
* } |
|
|
|
|
|
|
|
* ~~~ |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return static the query object itself |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function indexBy($column) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return parent::indexBy($column); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function createModels($rows) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$models = []; |
|
|
|
|
|
|
|
if ($this->asArray) { |
|
|
|
|
|
|
|
if ($this->indexBy === null) { |
|
|
|
|
|
|
|
return $rows; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
foreach ($rows as $row) { |
|
|
|
|
|
|
|
if (is_string($this->indexBy)) { |
|
|
|
|
|
|
|
$key = $row[$this->indexBy]; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$key = call_user_func($this->indexBy, $row); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$models[$key] = $row; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
/** @var $class ActiveRecord */ |
|
|
|
|
|
|
|
$class = $this->modelClass; |
|
|
|
|
|
|
|
if ($this->indexBy === null) { |
|
|
|
|
|
|
|
foreach ($rows as $row) { |
|
|
|
|
|
|
|
$models[] = $class::create($row); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
foreach ($rows as $row) { |
|
|
|
|
|
|
|
$model = $class::create($row); |
|
|
|
|
|
|
|
if (is_string($this->indexBy)) { |
|
|
|
|
|
|
|
$key = $model->{$this->indexBy}; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$key = call_user_func($this->indexBy, $model); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$models[$key] = $model; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return $models; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |