{$fieldIdentifier} = $object->id; $this->populateRelation($fieldObject, $object); } public function triggerEvent(string $event, array $attributes) { $event = new $event(); foreach($attributes as $key => $value) { $event->$key = $value; } $this->trigger($event::NAME, $event); } /** * Méthode générique de recherche utilisée pour tous les modèles. Elle a * pour but de construire la requête et de retourner le résultat. * * @param array $params * @param array $options * @return mixed * @throws NotFoundHttpException */ public static function searchBy($params = [], $options = []) { $class = get_called_class(); $repositoryClass = str_replace('\\Model\\', '\\Repository\\', $class).'Repository'; $repository = $repositoryClass::getInstance(); if(method_exists($repository , 'getDefaultOptionsSearch')) { $default_options = $repository->getDefaultOptionsSearch(); } else { throw new \ErrorException('La méthode "getDefaultOptionsSearch" n\'est ' . 'pas définie dans la classe "' . $repositoryClass . '"'); } $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 && !isset($options['ignore_id_producer']) ) { $params[$options['attribute_id_producer']] = GlobalParam::getCurrentProducerId(); } if (!isset($options['type_search'])) { $options['type_search'] = self::SEARCH_ALL; } if($class == 'common\models\PointSale' && !isset($options['status'])) { $params['status'] = 1; } $records = $class::find(); // With if (is_array($options['with']) && count($options['with'])) { $records = $records->with($options['with']); } // Join with if (is_array($options['join_with']) && count($options['join_with'])) { $records = $records->joinWith($options['join_with']); } // Conditions 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']); } } } // Params if (isset($options['params']) && is_array($options['params']) && count($options['params'])) { $records = $records->params($options['params']); } // Paramètres 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])) { // Orderby if (isset($options['orderby']) && strlen($options['orderby'])) { $records = $records->orderBy($options['orderby']); } // Limit if (isset($options['limit']) && is_numeric($options['limit']) && $options['limit'] > 0) { $records = $records->limit($options['limit']); } } if (isset($options['groupby'])) { $records = $records->groupBy($options['groupby']); } if (isset($options['as_array'])) { $records = $records->asArray(); } if ($options['type_search'] == self::SEARCH_QUERY) { self::groupByPrimaryKey($class, $records); return $records; } elseif ($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) { self::groupByPrimaryKey($class, $records); return $records->count(); } return null; } public static function groupByPrimaryKey($class, $records) { $primaryKey = static::primaryKey(); $records = $records->groupBy($class::tableName() . '.' . $primaryKey[0]); } public static function searchQuery($params = [], $options = []) { $options['type_search'] = self::SEARCH_QUERY; return self::searchDispatch($params, $options); } /** * Recherche un enregistrement. * * @param array $params * @param array $options * @return mixed */ public static function searchOne($params = [], $options = []) { $options['type_search'] = self::SEARCH_ONE; return self::searchDispatch($params, $options); } /** * Recherche tous les enregistrements. * * @param array $params * @param array $options * @return mixed */ public static function searchAll($params = [], $options = []) { $options['type_search'] = self::SEARCH_ALL; return self::searchDispatch($params, $options); } /** * Recherche et compte le nombre de résultats. * * @param array $params * @param array $options * @return integer */ public static function searchCount($params = [], $options = []) { $options['type_search'] = self::SEARCH_COUNT; return self::searchDispatch($params, $options); } /** * Appelle la méthode 'search' de la classe appellante. * * @param array $params * @param array $options * @return mixed */ public static function searchDispatch($params = [], $options = []) { $class = get_called_class(); return $class::searchBy($params, $options); } }