|
- <?php
-
-
-
- namespace common\components;
-
- use common\helpers\GlobalParam;
-
- class ActiveRecordCommon extends \yii\db\ActiveRecord
- {
- const SEARCH_ALL = 'all';
- const SEARCH_ONE = 'one';
- const SEARCH_COUNT = 'count';
-
-
-
- public static function searchBy($params = [], $options = [])
- {
- $class = get_called_class();
-
- if (is_callable([$class, 'defaultOptionsSearch'])) {
- $default_options = $class::defaultOptionsSearch();
- } else {
- throw new \ErrorException('La méthode "defaultOptionsSearch" n\'est '
- . 'pas définie dans la classe "' . $class . '"');
- }
-
- $options = array_merge($default_options, $options);
-
- $pk = $class::primaryKey();
- $pk = $class::tableName() . '.' . $pk[0];
-
- if (isset($options['attribute_id_producer']) && strlen($options['attribute_id_producer'])
- && !isset($params[$options['attribute_id_producer']]) && !Yii::$app->user->isGuest) {
- $params[$options['attribute_id_producer']] = GlobalParam::getCurrentProducerId();
- }
-
- if (!isset($options['type_search'])) {
- $options['type_search'] = self::SEARCH_ALL;
- }
-
- $records = $class::find();
-
-
- if (is_array($options['with']) && count($options['with'])) {
- $records = $records->with($options['with']);
- }
-
-
- if (is_array($options['join_with']) && count($options['join_with'])) {
- $records = $records->joinWith($options['join_with']);
- }
-
-
- if (isset($options['conditions'])) {
- if (is_array($options['conditions'])) {
- if (count($options['conditions'])) {
- foreach ($options['conditions'] as $condition) {
- $records = $records->andWhere($condition);
- }
- }
- } else {
- if (strlen($options['conditions'])) {
- $records = $records->andWhere($options['conditions']);
- }
- }
- }
-
-
- if (isset($options['params']) && is_array($options['params']) && count($options['params'])) {
- $records = $records->params($options['params']);
- }
-
-
- if (is_array($params) && count($params)) {
- foreach ($params as $key => $val) {
- if (strpos($key, '.') === false) {
- unset($params[$key]);
- $key = $class::tableName() . '.' . $key;
- $params[$key] = $val;
- }
- $records = $records->andWhere([$key => $val]);
- }
- }
-
- if (!isset($params[$pk])) {
-
- if (isset($options['orderby']) && strlen($options['orderby'])) {
- $records = $records->orderBy($options['orderby']);
- }
-
- if (isset($options['limit']) && is_numeric($options['limit'])
- && $options['limit'] > 0) {
- $records = $records->limit($options['limit']);
- }
- }
-
- if (isset($options['as_array'])) {
- $records = $records->asArray();
- }
-
- if ($options['type_search'] == self::SEARCH_ALL) {
- return $records->all();
- } elseif ($options['type_search'] == self::SEARCH_ONE) {
- $record = $records->one();
- if ($record) {
- return $record;
- }
- } elseif ($options['type_search'] == self::SEARCH_COUNT) {
- return $records->count();
- }
-
- return false;
- }
-
-
-
- public static function searchOne($params = [], $options = [])
- {
- $options['type_search'] = self::SEARCH_ONE;
- return self::searchDispatch($params, $options);
- }
-
-
-
- public static function searchAll($params = [], $options = [])
- {
- $options['type_search'] = self::SEARCH_ALL;
- return self::searchDispatch($params, $options);
- }
-
-
-
- public static function searchCount($params = [], $options = [])
- {
- $options['type_search'] = self::SEARCH_COUNT;
- return self::searchDispatch($params, $options);
- }
-
-
-
- public static function searchDispatch($params = [], $options = [])
- {
- $class = get_called_class();
- return $class::searchBy($params, $options);
- }
-
- }
|