作者:asinfotrac
项目:yii2-toolbo
/**
* Configures the query as such, that you can filter by a model, its id or an array of both. It is also
* possible to invert the query. This means all but the one(s) provided.
*
* @param \yii\db\ActiveQuery $query the query to modify
* @param integer|integer[]|\yii\db\ActiveRecord|\yii\db\ActiveRecord[] $param the id(s) or the model(s). If
* an array is provided, it can be a mix of both
* @param string $attribute the attribute name to compare (defaults to `id`)
* @param bool $invert if true t, the query will be inverted (NOT LIKE, NOT, ...)
*/
public function oneOrManyModelComparison(&$query, $param, $attribute = 'id', $invert = false)
{
//get data from array
if (is_array($param)) {
$data = [];
foreach ($param as $p) {
if ($p instanceof \yii\db\ActiveRecord) {
$data[] = $p->{$attribute};
} else {
$data[] = $p;
}
}
$param = $data;
} else {
if ($param instanceof \yii\db\ActiveRecord) {
$param = $param->{$attribute};
}
}
//modify query
if (!$invert) {
$query->andWhere([$attribute => $param]);
} else {
$query->andWhere(['not', [$attribute => $param]]);
}
}
作者:netis-p
项目:yii2-cru
/**
* Use a distinct compare value for each column. Primary and foreign keys support multiple values.
* @param \yii\db\ActiveQuery $query
* @return \yii\db\ActiveQuery
*/
protected function addAttributesSearchConditions(\yii\db\ActiveQuery $query)
{
$tablePrefix = $this->getDb()->getSchema()->quoteSimpleTableName('t');
$conditions = ['and'];
$formats = $this->attributeFormats();
$attributes = $this->attributes();
$relations = $this->relations();
$validAttributes = array_diff($attributes, array_keys($this->getErrors()));
$attributeValues = $this->getAttributes($validAttributes);
$formatter = Yii::$app->formatter;
/** @var EnumCollection $enums */
$enums = $formatter instanceof Formatter ? $formatter->getEnums() : null;
foreach ($validAttributes as $attribute) {
$value = $attributeValues[$attribute];
if ($value === null || !isset($formats[$attribute]) || $enums !== null && !is_array($formats[$attribute]) && $enums->has($formats[$attribute])) {
continue;
}
if (in_array($attribute, $relations)) {
// only hasMany relations should be ever marked as valid attributes
$conditions[] = $this->getRelationCondition($this->getRelation($attribute), $value);
} else {
$conditions[] = $this->getAttributeCondition($attribute, $value, $formats, $tablePrefix, $this->getDb());
}
}
// don't clear attributes to allow rendering filled search form
//$this->setAttributes(array_fill_keys($attributes, null));
if ($conditions !== ['and']) {
$query->andWhere($conditions);
}
return $query;
}
作者:portalsway
项目:APEDevice
public static function getActiveToken($token)
{
$activeQuery = new ActiveQuery(self::className());
$activeQuery->where('access_token = :token', [':token' => $token]);
$activeQuery->andWhere('expires > now()');
$token = $activeQuery->one();
return $token;
}
作者:Bochozka
项目:stat.in
public function filterQuery(ActiveQuery $query)
{
if ($this->type) {
$query->innerJoinWith('type');
$query->andWhere(['{{death_reason_type}}.[[key]]' => $this->type]);
}
return $query;
}
作者:bth200
项目:rfer
public function getDataprovider()
{
$query = new ActiveQuery($this::className());
if ($this->airport_id) {
$query->andWhere(['airport_id' => $this->airport_id]);
}
$query->andWhere(['isarrival' => $this->isarrival]);
$query->orderBy($this->isarrival == 1 ? "timeto" : "timefrom");
return new ActiveDataProvider(['query' => $query]);
}
作者:netis-p
项目:yii2-cru
/**
* Creates a Sort object configuration using query default order.
* @param \yii\db\ActiveQuery $query
* @param array $attributes
* @return array
*/
public function getSortConfig(\yii\db\ActiveQuery $query, array $attributes)
{
$defaults = $query instanceof ActiveQuery ? $query->getDefaultOrderColumns() : [];
$sort = ['enableMultiSort' => true, 'attributes' => [], 'defaultOrder' => $defaults];
/** @var TableSchema $tableSchema */
$tableSchema = $this->getTableSchema();
foreach ($attributes as $attribute) {
if ($tableSchema->getColumn($attribute) === null) {
continue;
}
$sort['attributes'][$attribute] = ['asc' => array_merge([$attribute => SORT_ASC], $defaults), 'desc' => array_merge([$attribute => SORT_DESC], $defaults)];
}
return $sort;
}
作者:tqsq200
项目:dotplant
public function filter(ActiveQuery $query, &$cacheKeyAppend)
{
$get = Yii::$app->request->post();
if (isset($get['changeValue']) && is_array($get['changeValue'])) {
foreach ($get['changeValue'] as $propertyId => $isActive) {
if ($isActive && isset($get[$this->minValueAttribute][$propertyId]) && isset($get[$this->maxValueAttribute][$propertyId]) && is_numeric($get[$this->minValueAttribute][$propertyId]) && is_numeric($get[$this->maxValueAttribute][$propertyId])) {
$query->innerJoin('object_static_values as osvf' . $propertyId, 'product.id=osvf' . $propertyId . '.object_model_id');
$query->innerJoin('property_static_values as psvf' . $propertyId, 'psvf' . $propertyId . '.id=osvf' . $propertyId . '.property_static_value_id');
$query->andWhere('psvf' . $propertyId . '.value >= :minData ')->andWhere('psvf' . $propertyId . '.value <= :maxData ')->andWhere(['psvf' . $propertyId . '.property_id' => $propertyId])->addParams([':minData' => (int) $get[$this->minValueAttribute][$propertyId], ':maxData' => (int) $get[$this->maxValueAttribute][$propertyId]]);
$cacheKeyAppend .= 'FilterRangeProperty[min:' . (int) $get[$this->minValueAttribute][$propertyId] . ':max' . (int) $get[$this->maxValueAttribute][$propertyId] . ']';
}
}
}
return $query;
}
作者:Liv102
项目:cm
/**
* Конфигурирование объекта запроса поиска по элементам.
*
* @param \yii\db\ActiveQuery $activeQuery
* @param null $modelClassName
* @return $this
*/
public function buildElementsQuery(\yii\db\ActiveQuery $activeQuery)
{
$where = [];
//Нужно учитывать связанные дополнительные данные
if ($this->enabledElementProperties == Cms::BOOL_Y) {
$activeQuery->joinWith('cmsContentElementProperties');
//Нужно учитывать настройки связанные дополнительные данных
if ($this->enabledElementPropertiesSearchable == Cms::BOOL_Y) {
$activeQuery->joinWith('cmsContentElementProperties.property');
$where[] = ['and', ['like', CmsContentElementProperty::tableName() . ".value", '%' . $this->searchQuery . '%', false], [CmsContentProperty::tableName() . ".searchable" => Cms::BOOL_Y]];
} else {
$where[] = ['like', CmsContentElementProperty::tableName() . ".value", '%' . $this->searchQuery . '%', false];
}
}
//Поиск по основному набору полей
if ($this->searchElementFields) {
foreach ($this->searchElementFields as $fieldName) {
$where[] = ['like', CmsContentElement::tableName() . "." . $fieldName, '%' . $this->searchQuery . '%', false];
}
}
if ($where) {
$where = array_merge(['or'], $where);
$activeQuery->andWhere($where);
}
//Отфильтровать только конкретный тип
if ($this->searchElementContentIds) {
$activeQuery->andWhere([CmsContentElement::tableName() . ".content_id" => (array) $this->searchElementContentIds]);
}
return $this;
}
作者:VampireM
项目:admin-9939-co
/**
* @param $table
* @return object
* @throws InvalidConfigException
*/
public static function findx($table)
{
if (self::$table != $table) {
self::$table = $table;
}
return Yii::createObject(ActiveQuery::className(), [get_called_class(), ['from' => [static::tableName()]]]);
}
作者:netis-p
项目:yii2-cru
/**
* Adds a condition to search in relations using subquery.
* @todo this should be called for each token, to group their conditions with OR and group token groups with AND
*
* @param \yii\db\ActiveQuery $query
* @param array $tokens all search tokens extracted from term
* @param array $relationAttributes array of string(relation name) => array(
* 'model' => netis\crud\db\ActiveRecord,
* 'searchModel' => netis\crud\db\ActiveSearchTrait,
* 'attributes' => array
* )
* @return array conditions to add to $query
*/
protected function processSearchRelated(\yii\db\ActiveQuery $query, array $tokens, array $relationAttributes)
{
$allConditions = ['or'];
foreach ($relationAttributes as $relationName => $relation) {
/**
* @todo optimize this (check first, don't want to loose another battle with PostgreSQL query planner):
* - for BELONGS_TO check fk against subquery
* - for HAS_MANY and HAS_ONE check pk against subquery
* - for MANY_MANY join only to pivot table and check its fk agains subquery
*/
$query->joinWith([$relationName => function ($query) use($relationName) {
/** @var \yii\db\ActiveQuery $query */
/** @var \yii\db\ActiveRecord $class */
$class = $query->modelClass;
return $query->select(false)->from([$relationName => $class::tableName()]);
}]);
$conditions = ['and'];
/** @var ActiveSearchInterface $searchModel */
$searchModel = $relation['searchModel'];
if (!$searchModel instanceof ActiveSearchInterface) {
continue;
}
foreach ($tokens as $token) {
$condition = $searchModel->processSearchToken($token, $relation['attributes'], $relationName);
if ($condition !== null) {
$conditions[] = $condition;
}
}
if ($conditions !== ['and']) {
$allConditions[] = $conditions;
}
}
return $allConditions !== ['or'] ? $allConditions : null;
}
作者:fanin
项目:gtq
/**
* Apply possible answers order to query
* @param ActiveQuery $query
* @param $order
* @return string
*/
public static function applyOrder(ActiveQuery $query, $order)
{
switch ($order) {
case 'oldest':
$query->orderBy('created_at DESC');
break;
case 'active':
$query->orderBy('created_at ASC');
break;
case 'votes':
default:
$query->orderBy('votes DESC');
break;
}
return $order;
}
作者:mervic
项目:yii2-adminlte-gi
/**
* @inheritdoc
*/
public function rules()
{
return array_merge(parent::rules(), [[['db', 'ns', 'tableName', 'modelClass', 'baseClass', 'queryNs', 'queryClass', 'queryBaseClass'], 'filter', 'filter' => 'trim'], [['ns', 'queryNs'], 'filter', 'filter' => function ($value) {
return trim($value, '\\');
}], [['db', 'ns', 'tableName', 'baseClass', 'queryNs', 'queryBaseClass'], 'required'], [['db', 'modelClass', 'queryClass'], 'match', 'pattern' => '/^\\w+$/', 'message' => 'Only word characters are allowed.'], [['ns', 'baseClass', 'queryNs', 'queryBaseClass'], 'match', 'pattern' => '/^[\\w\\\\]+$/', 'message' => 'Only word characters and backslashes are allowed.'], [['tableName'], 'match', 'pattern' => '/^(\\w+\\.)?([\\w\\*]+)$/', 'message' => 'Only word characters, and optionally an asterisk and/or a dot are allowed.'], [['db'], 'validateDb'], [['ns', 'queryNs'], 'validateNamespace'], [['tableName'], 'validateTableName'], [['modelClass'], 'validateModelClass', 'skipOnEmpty' => false], [['baseClass'], 'validateClass', 'params' => ['extends' => ActiveRecord::className()]], [['queryBaseClass'], 'validateClass', 'params' => ['extends' => ActiveQuery::className()]], [['generateRelations', 'generateLabelsFromComments', 'useTablePrefix', 'useSchemaName', 'generateQuery'], 'boolean'], [['enableI18N'], 'boolean'], [['messageCategory'], 'validateMessageCategory', 'skipOnEmpty' => false], [['imagesDomain', 'imagesPath'], 'filter', 'filter' => 'trim'], [['imagesPath'], 'filter', 'filter' => function ($value) {
return trim($value, '/');
}], [['imagesDomain', 'imagesPath'], 'required'], [['addingI18NStrings'], 'boolean'], [['imagesDomain'], 'match', 'pattern' => '/^(?:[\\w](?:[\\w-]+[\\w])?\\.(?:{\\$domain})|(?:[\\w](?:[0-9\\w\\-\\.]+)?[\\w]\\.[\\w]+))|(?:@[\\w_-]+)$/', 'message' => 'No valid images domain.'], [['messagesPaths'], 'validateMessagesPaths']]);
}
作者:tqsq200
项目:dotplant
/**
* @param ActiveQuery $query
* @param string $attribute
* @param bool $partialMath
*/
private function addCondition($query, $attribute, $partialMath = false)
{
if (isset($this->relationAttributes[$attribute])) {
$attributeName = $this->relationAttributes[$attribute];
} else {
$attributeName = call_user_func([$this->modelClassName, 'tableName']) . '.' . $attribute;
}
$value = $this->{$attribute};
if ($value === '') {
return;
}
if ($partialMath) {
$query->andWhere(['like', $attributeName, $value]);
} else {
$query->andWhere([$attributeName => $value]);
}
}
作者:kocap
项目:librar
/**
* @param ActiveQuery $query
* @param $attribute
* @param bool|false $partialMatch
*/
protected function addCondition(ActiveQuery $query, $attribute, $partialMatch = false)
{
if (($pos = strrpos($attribute, '.')) !== false) {
$modelAttribute = substr($attribute, $pos + 1);
} else {
$modelAttribute = $attribute;
}
$value = $this->{$modelAttribute};
if (trim($value) === '') {
return;
}
$attribute = "books.{$attribute}";
if ($partialMatch) {
$query->andWhere(['like', $attribute, $value]);
} else {
$query->andWhere([$attribute => $value]);
}
}
作者:VasileGabrie
项目:humhu
/**
* @inheritdoc
*/
public function run()
{
$countQuery = clone $this->query;
$pagination = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'pageSize' => $this->pageSize]);
$this->query->offset($pagination->offset)->limit($pagination->limit);
return $this->render("userListBox", ['title' => $this->title, 'users' => $this->query->all(), 'pagination' => $pagination]);
}
作者:skeeks-cm
项目:cm
protected function _run()
{
$key = $this->getCacheKey() . 'run';
$dependency = new TagDependency(['tags' => [$this->className() . (string) $this->namespace, (new CmsSite())->getTableCacheTag()]]);
$result = \Yii::$app->cache->get($key);
if ($result === false || $this->enabledRunCache == Cms::BOOL_N) {
$this->activeQuery = CmsSite::find();
if ($this->active == Cms::BOOL_Y) {
$this->activeQuery->active();
} else {
if ($this->active == Cms::BOOL_N) {
$this->activeQuery->active(false);
}
}
if ($this->limit) {
$this->activeQuery->limit($limit);
}
if ($this->orderBy) {
$this->activeQuery->orderBy([$this->orderBy => (int) $this->order]);
}
$result = parent::_run();
\Yii::$app->cache->set($key, $result, (int) $this->runCacheDuration, $dependency);
}
return $result;
}
作者:nanodesu8
项目:yii2-activerecord-sor
/**
* @param int $sort
* @return $this
*/
public function sort($sort = SORT_DESC)
{
/** @var ISortableActiveRecord $model */
$model = new $this->owner->modelClass();
$behavior = $model->getSortBehavior();
$this->owner->orderBy([$behavior->attributeName => $sort]);
return $this;
}
作者:pbabila
项目:bcod
/**
* @return Pagination
*/
public function getPaginator()
{
$paginator = new Pagination(['totalCount' => $this->countQuery->count()]);
$paginator->pageParam = 'page';
$paginator->pageSizeParam = false;
$paginator->setPageSize($this->limit);
return $paginator;
}
作者:barie
项目:yii2-tool
/**
* @inheritdoc
*/
public function run()
{
$result = [];
$class = $this->relation->modelClass;
$form = $this->form;
$template = '<div class="template">' . $this->render($this->viewName, ['model' => new $class(), 'form' => $this->form, 'index' => 'myindex']) . '</div>';
$items = $this->items ?: $this->relation->all();
foreach ($items as $index => $model) {
$result[] = $this->render($this->viewName, compact('index', 'model', 'form'));
}
$result[] = Html::a(\Yii::t('app', ' Add'), '#', ['class' => 'btn btn-success glyphicon glyphicon-plus', 'template' => $template, 'onclick' => '
$(this).before($(this).attr("template").replace(/myindex/g, "new-"+Date.now())); return false;']);
return implode('', $result);
}
作者:yii2-tool
项目:yii2-bas
/**
* @inheritdoc
*/
public function populate($rows)
{
$event = new PopulateEvent($this, $rows);
$this->trigger(static::EVENT_BEFORE_POPULATE, $event);
$rows = $event->rows;
return parent::populate($rows);
}