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

Commit

Permalink
Update query and schema
Browse files Browse the repository at this point in the history
  • Loading branch information
panlatent committed Feb 26, 2019
1 parent 3d687e8 commit 40d669e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 38 deletions.
27 changes: 27 additions & 0 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace panlatent\odoo;

use Yii;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
use yii\db\BaseActiveRecord;
use yii\helpers\Inflector;
Expand Down Expand Up @@ -54,11 +55,37 @@ public static function tableName(): string
return Inflector::camel2id(StringHelper::basename(get_called_class()), '.');
}

/**
* Returns the schema information of the Odoo orm table associated with this AR class.
* @return TableSchema the schema information of the Odoo orm table associated with this AR class.
* @throws InvalidConfigException if the table for the AR class does not exist.
*/
public static function getTableSchema()
{
$tableSchema = static::getDb()
->getSchema()
->getTableSchema(static::tableName());

if ($tableSchema === null) {
throw new InvalidConfigException('The table does not exist: ' . static::tableName());
}

return $tableSchema;
}

/**
* @inheritdoc
*/
public function insert($runValidation = true, $attributes = null)
{
throw new NotSupportedException();
}

/**
* @inheritdoc
*/
public function attributes()
{
return array_keys(static::getTableSchema()->columns);
}
}
13 changes: 13 additions & 0 deletions src/BaseActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public function __construct(string $modelClass, array $config = [])
parent::__construct($config);
}

/**
* @param Connection|null $odoo
* @return ActiveRecord|null
*/
public function one($odoo = null)
{
$result = parent::one($odoo);

$results = $this->populate([$result]);

return $results ? reset($results) : null;
}

/**
* @param QueryBuilder $builder
* @return Query
Expand Down
3 changes: 3 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ class Connection extends Component
* Common service.
*/
const COMMON = 'common';

/**
* Object service
*/
const OBJECT = 'object';

/**
* Db service.
*/
const DB = 'db';

/**
* Report service.
*/
Expand Down
97 changes: 59 additions & 38 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class Schema extends Component
*/
public $defaultSchema;

/**
* @var bool
*/
private $_fetchedAllTableSchemas = false;

/**
* @var array
*/
private $_tableSchemas = [];

/**
* @return string[]
*/
Expand All @@ -42,18 +52,21 @@ public function getTableNames(): array
*/
public function getTableSchemas()
{
$tables = [];
if ($this->_fetchedAllTableSchemas) {
return array_values($this->_tableSchemas);
}

$this->_tableSchemas = [];

$results = $this->odoo->searchRead('ir.model', [], ['fields' => ['model']]);
foreach ($results as $result) {
$tables[] = Yii::createObject([
'class' => TableSchema::class
] + [
'name' => $result['model'],
]);
$tableSchema = $this->_createTableSchema($result);
$tables[$tableSchema->name] = $tableSchema;
}

return $tables;
$this->_fetchedAllTableSchemas = true;

return array_values($this->_tableSchemas);
}

/**
Expand All @@ -62,29 +75,56 @@ public function getTableSchemas()
*/
public function getTableSchema(string $tableName = null)
{
$config = [
'class' => TableSchema::class
];
if ($this->_tableSchemas && array_key_exists($tableName, $this->_tableSchemas)) {
return $this->_tableSchemas[$tableName];
}

$results = $this->odoo->searchRead('ir.model', [['model', '=', $tableName]], ['fields' => [
'name',
'model'
]]);

if (!$results) {
return null;
return $this->_tableSchemas[$tableName] = null;
}

$result = reset($results);

$config = array_merge($config, [
'name' => $result['model'],
'fullName' => $result['model'],
return $this->_tableSchemas[$tableName] = $this->_createTableSchema($result);
}

/**
* @return array
*/
public function findUniqueIndexes()
{
return [['id']];
}

/**
* @param string $tableName
* @return bool
*/
public function hasTable(string $tableName): bool
{
return count($this->odoo->search('ir.model', [['model', '=', $tableName]])) > 0;
}

/**
* @param array $config
* @return TableSchema
*/
private function _createTableSchema(array $config)
{
$model = $config['model'];

$config = [
'name' => $model,
'fullName' => $model,
'primaryKey' => ['id'],
]);
];

// Foreign Keys
$fields = $this->odoo->fieldsGet($tableName);
$fields = $this->odoo->fieldsGet($model);

$foreignKeys = [];
$relationFields = array_filter($fields, function($meta) {
Expand All @@ -111,32 +151,13 @@ public function getTableSchema(string $tableName = null)
'comment' => $meta['string']
]);

$columns[] = $column;
$columns[$field] = $column;
}
unset($column);

$config['columns'] = $columns;

/** @noinspection PhpIncompatibleReturnTypeInspection */
return Yii::createObject($config);
return Yii::createObject(['class' => TableSchema::class] + $config);
}

/**
* @return array
*/
public function findUniqueIndexes()
{
return [['id']];
}

/**
* @param string $tableName
* @return bool
*/
public function hasTable(string $tableName): bool
{
return count($this->odoo->search('ir.model', [['model', '=', $tableName]])) > 0;
}


}

0 comments on commit 40d669e

Please sign in to comment.