Query.php 5.55 KB
<?php
    
    namespace artbox\odoo\components;
    
    use yii\base\Component;
    use yii\db\QueryInterface;
    use yii\db\QueryTrait;
    
    class Query extends Component implements QueryInterface
    {
        use QueryTrait;
        
        /**
         * @var string the model to be selected from.
         * @see from()
         */
        public $from;
        
        /**
         * @var array the mapping of query
         */
        public $mapping = [];
        
        public $options = [ [] ];
        
        /**
         * Executes the query and returns all results as an array.
         *
         * @param Connection $db the database connection used to execute the query.
         *                       If this parameter is not given, the `db` application component will be used.
         *
         * @return array the query results. If the query results in nothing, an empty array will be returned.
         */
        public function all($db = null)
        {
            /**
             * @var Connection $db
             */
            if (empty($db)) {
                $db = \Yii::$app->get('odoo');
            }
            return $db->createCommand($this->from, 'search_read', $this->options, $this->mapping)
                      ->execute();
        }
        /**
         * Executes the query and returns a single row of result.
         *
         * @param Connection $db the database connection used to execute the query.
         *                       If this parameter is not given, the `db` application component will be used.
         *
         * @return array|bool the first row (in terms of an array) of the query result. False is returned if the query
         * results in nothing.
         */
        public function one($db = null)
        {
            $result = $this->all($db);
            if (empty($result)) {
                return null;
            } else {
                return $result[ 0 ];
            }
        }
        /**
         * Returns the number of records.
         *
         * @param string     $q  the COUNT expression. Defaults to '*'.
         * @param Connection $db the database connection used to execute the query.
         *                       If this parameter is not given, the `db` application component will be used.
         *
         * @return int number of records.
         */
        public function count($q = '*', $db = null)
        {
            /**
             * @var Connection $db
             */
            if (empty($db)) {
                $db = \Yii::$app->get('odoo');
            }
            return ( new Command(
                [
                    'db' => $db,
                ]
            ) )->count('product.product', $this->options, $this->mapping);
        }
        /**
         * Returns a value indicating whether the query result contains any row of data.
         *
         * @param Connection $db the database connection used to execute the query.
         *                       If this parameter is not given, the `db` application component will be used.
         *
         * @return bool whether the query result contains any row of data.
         */
        public function exists($db = null)
        {
            if ($this->count('*', $db)) {
                return true;
            } else {
                return false;
            }
        }
        /**
         * @param array $fields
         *
         * @return \artbox\odoo\components\Query
         */
        public function select(array $fields)
        {
            $this->mapping[ 'fields' ] = $fields;
            
            return $this;
        }
        
        /**
         * @param $model
         *
         * @return \artbox\odoo\components\Query
         */
        public function from($model)
        {
            $this->from = $model;
            
            return $this;
        }
        
        /**
         * @param array $mapping
         *
         * @return \artbox\odoo\components\Query
         */
        public function mapping(array $mapping)
        {
            $this->mapping = $mapping;
            
            return $this;
        }
        
        /**
         * @param array $mapping
         *
         * @return \artbox\odoo\components\Query
         */
        public function addMapping(array $mapping)
        {
            if (is_array($this->mapping)) {
                $this->mapping = array_merge($this->mapping, $mapping);
            } else {
                $this->mapping = $mapping;
            }
            return $this;
        }
        
        /**
         * @param int|null $offset
         *
         * @return \artbox\odoo\components\Query
         */
        public function offset($offset)
        {
            $this->mapping[ 'offset' ] = $offset;
            
            return $this;
        }
        
        /**
         * @param int|null $limit
         *
         * @return \artbox\odoo\components\Query
         */
        public function limit($limit)
        {
            $this->mapping[ 'limit' ] = $limit;
            
            return $this;
        }
        
        /**
         * @param array|string $condition
         *
         * @return \artbox\odoo\components\Query
         */
        public function where($condition)
        {
            $this->options[ 0 ][] = $condition;
            
            return $this;
        }
        
        /**
         * @param array|string $condition
         *
         * @return \artbox\odoo\components\Query
         */
        public function andWhere($condition)
        {
            return $this->where($condition);
        }
    }