public function search(array $params) { $query = Trip::find(); $query->with('number', 'employee'); $query->orderBy(['numberPossession.from' => SORT_DESC]); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => false]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } if (!empty($this->mobileNumber)) { $numberIds = array_map(function ($item) { return $item['_id']; }, Number::find()->select(['_id'])->asArray()->where(['like', 'number', $this->mobileNumber])->all()); $query->andWhere(['in', 'numberId', $numberIds]); } if (!empty($this->employeeName)) { $employeeIds = array_map(function ($item) { return (int) $item['id']; }, Employee::findByName($this->employeeName, true)->select('id')->asArray()->all()); $query->andWhere(['in', 'employeeId', $employeeIds]); } if ($this->complete === self::INCOMPLETE) { $query->andWhere(['complete' => false]); } if ($this->rentNumberFrom !== $this->rentNumberTo) { $query->andWhere(['numberPossession.from' => ['$gt' => new MongoDate(strtotime($this->rentNumberFrom)), '$lte' => new MongoDate(strtotime($this->rentNumberTo))]]); } $query->andFilterWhere(['like', 'destination', $this->destination]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Number::find(); $query->with('owner', 'documents'); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 30]]); $dataProvider->setSort(['attributes' => ['number', 'destination', 'comment'], 'defaultOrder' => ['number' => SORT_ASC]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } if (is_numeric($this->searchText)) { $query->andFilterWhere(['like', 'number', $this->searchText]); } elseif (!empty($this->searchText)) { $ownerId = []; foreach (Employee::findByName($this->searchText, true)->all() as $owner) { $ownerId[] = $owner->getPrimaryKey(); } $query->andWhere(['in', 'ownerId', $ownerId]); } if ($this->operatorId !== self::OPERATOR_ANY) { $query->andFilterWhere(['operatorId' => $this->operatorId]); } if ($this->destination !== self::DESTINATION_ANY) { $query->andFilterWhere(['destination' => $this->destination]); } $query->andFilterWhere(['like', 'comment', $this->comment]); return $dataProvider; }
public function actionAutoComplete($q = null) { $result = []; /** @var $item Employee */ foreach (Employee::findByName($q, true)->all() as $item) { $result[] = ['value' => $item->fullName, 'post' => $item->post]; } echo Json::encode($result); }
public function testFindByName() { $this->specify("Model must be found on accurate data (like is false). ", function ($name) { expect(Employee::findByName($name)->count())->greaterThan(0); }, ['examples' => [["Иванов Иван Иванович"]]]); $this->specify("Model must be not found on accurate data (like is true). ", function ($name) { expect(Employee::findByName($name, true)->count())->greaterThan(0); }, ['examples' => [["иванов иВан ИВАНОвич"], ["Ива"]]]); $this->specify("Model must be not found", function ($name) { expect(Employee::findByName($name)->count())->equals(0); }, ['examples' => [["Не существующий сотрдуник"], ["Сотрудник"]]]); }
public function validateAttribute($model, $attribute) { $model->{$attribute} = trim(preg_replace("/ +/", " ", $model->{$attribute})); $query = Employee::findByName($model->{$attribute}); $postAttribute = $this->postAttribute; if ($postAttribute !== null) { $model->{$postAttribute} = trim(preg_replace("/ +/", " ", $model->{$postAttribute})); $query->andWhere(['post' => $model->{$postAttribute}]); } if (!$query->count()) { $this->addError($model, $attribute, "Сотрудник «{$model->{$attribute}}»" . ($postAttribute !== null ? " с должностью «{$model->{$postAttribute}}»" : "") . " не найден."); } }
/** * @param $name * @param null $post * @param bool $nameLike * @return ActiveQuery */ public static function findByOwner($name, $post = null, $nameLike = false) { $ownerId = []; $subQuery = Employee::findByName($name, $nameLike)->andFilterWhere(['post' => $post]); foreach ($subQuery->all() as $owner) { $ownerId[] = $owner->getPrimaryKey(); } return self::find()->where(['in', 'ownerId', $ownerId]); }
public function beforeSave($insert) { if (parent::beforeSave($insert)) { switch ($this->getScenario()) { case 'edit': $this->numberId = Number::findOne(['number' => $this->mobileNumber])->getPrimaryKey(); $this->employeeId = Employee::findByName($this->employeeName)->andWhere(['post' => $this->employeePost])->one()->getPrimaryKey(); $this->duration = ['from' => new MongoDate(strtotime($this->beginDate)), 'to' => new MongoDate(strtotime($this->endDate))]; $this->numberPossession = ['from' => new MongoDate(strtotime($this->rentNumberDate))]; break; case 'complete': break; } return true; } else { return false; } }