This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
141 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |