Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
Add BaseActiveQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
panlatent committed Feb 26, 2019
1 parent c97332e commit 3d687e8
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 79 deletions.
89 changes: 11 additions & 78 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,98 +12,31 @@
* @package panlatent\odoo
* @author Panlatent <panlatent@gmail.com>
*/
class ActiveQuery extends Query
class ActiveQuery extends BaseActiveQuery
{
/**
* @var bool|null
* @var int[]|int|null
*/
public $asArray;
public $id;

/**
* @var ActiveRecord|string
* @param int[]|int|null $value
* @return $this
*/
protected $modelClass;

/**
* ActiveQuery constructor.
*
* @param string $modelClass
* @param array $config
*/
public function __construct(string $modelClass, array $config = [])
{
$this->modelClass = $modelClass;

parent::__construct($config);
}

/**
* @param QueryBuilder $builder
* @return Query
*/
public function prepare($builder)
public function id($value)
{
if (empty($this->from)) {
$this->from = [$this->getPrimaryTableName()];
}
$this->id = $value;

return parent::prepare($builder);
return $this;
}

/**
* @inheritdoc
*/
public function populate(array $rows): array
protected function beforePrepare()
{
if (empty($rows)) {
return [];
}

$models = $this->createModels($rows);

if (!$this->asArray) {
foreach ($models as $model) {
$model->afterFind();
}
if ($this->id) {
$this->andWhere(['id' => $this->id]);
}

return parent::populate($models);
}

/**
* Converts found rows into model instances.
*
* @param array $rows
* @return ActiveRecord[]|array
*/
protected function createModels($rows)
{
if ($this->asArray) {
return $rows;
}

$models = [];

$class = $this->modelClass;
foreach ($rows as $row) {
$model = $class::instantiate($row);

/** @var ActiveRecord $modelClass */
$modelClass = get_class($model);
$modelClass::populateRecord($model, $row);
$models[] = $model;
}

return $models;
}

/**
* @return string primary table name
*/
protected function getPrimaryTableName()
{
$modelClass = $this->modelClass;

return $modelClass::tableName();
}
}
2 changes: 1 addition & 1 deletion src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function primaryKey()
}

/**
* @inheritdoc
* @return ActiveQuery|object
*/
public static function find()
{
Expand Down
129 changes: 129 additions & 0 deletions src/BaseActiveQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* @link https://github.com/panlatent/yii2-odoo
* @license http://opensource.org/licenses/MIT
*/

namespace panlatent\odoo;

/**
* Class BaseActiveQuery
*
* @package panlatent\odoo
* @author Panlatent <panlatent@gmail.com>
*/
abstract class BaseActiveQuery extends Query
{
/**
* @var bool|null
*/
public $asArray;

/**
* @var ActiveRecord|string
*/
protected $modelClass;

/**
* ActiveQuery constructor.
*
* @param string $modelClass
* @param array $config
*/
public function __construct(string $modelClass, array $config = [])
{
$this->modelClass = $modelClass;

parent::__construct($config);
}

/**
* @param QueryBuilder $builder
* @return Query
*/
public function prepare($builder)
{
$this->beforePrepare();

if (empty($this->from)) {
$this->from = $this->getPrimaryTableName();
}

$this->afterPrepare();

return parent::prepare($builder);
}

/**
* @inheritdoc
*/
public function populate(array $rows): array
{
if (empty($rows)) {
return [];
}

$models = $this->createModels($rows);

if (!$this->asArray) {
foreach ($models as $model) {
$model->afterFind();
}
}

return parent::populate($models);
}

/**
* Before prepare
*/
protected function beforePrepare()
{

}

/**
* After prepare
*/
protected function afterPrepare()
{

}

/**
* Converts found rows into model instances.
*
* @param array $rows
* @return ActiveRecord[]|array
*/
protected function createModels($rows)
{
if ($this->asArray) {
return $rows;
}

$models = [];

$class = $this->modelClass;
foreach ($rows as $row) {
$model = $class::instantiate($row);

/** @var ActiveRecord $modelClass */
$modelClass = get_class($model);
$modelClass::populateRecord($model, $row);
$models[] = $model;
}

return $models;
}

/**
* @return string primary table name
*/
protected function getPrimaryTableName()
{
$modelClass = $this->modelClass;

return $modelClass::tableName();
}
}

0 comments on commit 3d687e8

Please sign in to comment.