/** * @inheritdoc */ public function __construct($expression, $params = [], $config = []) { parent::__construct($expression, $params, $config); if ($this->query instanceof ActiveQuery) { $this->query->on(ActiveQuery::EVENT_ALIAS, function (Event $event) use($expression) { /* @var $query ActiveQuery */ $query = $event->sender; $this->expression = str_replace('{a}', $query->getAlias(), $expression); }); $this->expression = str_replace('{a}', $this->query->getAlias(), $expression); } }
/** * Возвращает выражение для сортировки результата * @param array $ids массив идентификаторов найденных сфинксом моделей * @return \yii\db\Expression * @throws \yii\base\InvalidConfigException */ protected function getOrderByExpression($ids) { $orderStr = "CASE id\n"; $i = 1; foreach ($ids as $id) { $orderStr .= "WHEN {$id} THEN {$i}\n"; $i++; } $orderStr .= "ELSE {$i}\n"; $orderStr .= "END\n"; return \Yii::createObject(Expression::className(), [$orderStr]); }
/** * Поиск. Возвращает провайдер данных * @param string $term фраза для поиска * @param array $attrs массив значений атрибутов (key=>value) * @return \common\sphinx\SphinxDataProvider * @throws \yii\base\InvalidConfigException */ public function search($term, $attrs = []) { $query = $this->getQuery(); $sphinxQuery = $this->getSphinxQuery(); if ($term) { $sphinxQuery->match(\Yii::createObject(Expression::className(), [$this->prepareTerm($term)])); } if ($attrs) { $sphinxQuery->andWhere($attrs); } $dataProvider = \Yii::createObject(["class" => \common\sphinx\SphinxDataProvider::className(), "query" => $query, "sphinxQuery" => $sphinxQuery]); return $dataProvider; }
/** * Поведения * @return array */ public function behaviors() { $fields = $this->getMetaFields()->getFields(); $behaviors = ['timestamp' => ['class' => \yii\behaviors\TimestampBehavior::className(), 'attributes' => [ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']], 'value' => Yii::createObject(["class" => Expression::className()], ['NOW()'])], 'tagCache' => ['class' => \common\behaviors\TagCache::className()]]; foreach ($fields as $field) { if ($field->behaviors()) { $behaviors = array_merge($behaviors, $field->behaviors()); } } return $behaviors; }
public function __construct() { $time = isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(true); $strtime = DateTimeFormatter::unixTimeToString($time, new DateTimeZone('Etc/UTC')); parent::__construct(Yii::$app->db->quoteValue($strtime)); }
/** * @depends testNewRecord */ public function testUpdateRecordExpression() { ActiveRecordTimestamp::$tableName = 'test_auto_timestamp_string'; ActiveRecordTimestamp::$behaviors = ['timestamp' => ['class' => TimestampBehavior::className(), 'value' => new Expression("strftime('%Y')")]]; $model = new ActiveRecordTimestamp(); $model->save(false); $enforcedTime = date('Y') - 1; $model->created_at = $enforcedTime; $model->updated_at = $enforcedTime; $model->save(false); $this->assertEquals($enforcedTime, $model->created_at, 'Create time has been set on update!'); $this->assertInstanceOf(Expression::className(), $model->updated_at); $model->refresh(); $this->assertEquals($enforcedTime, $model->created_at, 'Create time has been set on update!'); $this->assertEquals(date('Y'), $model->updated_at); }