user->isGuest) { $params[$options['attribute_id_producer']] = Producer::getId(); } if(!isset($options['type_search'])) { $options['type_search'] = self::SEARCH_ALL ; } $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']); } } } // 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($options['type_search'] == self::SEARCH_ALL) { return $records->all(); } elseif($options['type_search'] == self::SEARCH_ONE) { $record = $records->one(); if($record) { return $record ; } else { throw new NotFoundHttpException( 'Le modèle '.$class.' #'.((int) $params[$pk]).' n\'a pas été trouvé.'); } } elseif($options['type_search'] == self::SEARCH_COUNT) { return $records->count() ; } } /** * 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::search($params, $options) ; } }