public function init() { parent::init(); $this->target = Yii::createObject($this->targetClass); if (!$this->condition) { $this->target = $this->target->findOne($this->condition); } }
protected function _delete(ActiveRecord $model) { $id = Yii::$app->request->post('id'); $model->findOne($id)->delete(); // $model::deleteAll($id); return $this->_success(); }
/** * @param $name * @return null|static */ public static function getIdByName($name) { $branch = parent::findOne(['branch_title' => $name]); if ($branch) { return $branch->id; } else { return 0; } }
public static function findByUsername($username) { return parent::findOne(['username' => $username]); }
public static function info() { return parent::findOne(\Yii::$app->user->getId()); }
/** * Find model by label value * @param ActiveRecord $model * @param string $label * @return null|static */ public static function findByLabel(ActiveRecord $model, $label) { /* @var ActiveRecord $this */ return $model->findOne([self::getLabelColumnName($model) => $label]); }
/** * @param string $id * @return User */ public static function findIdentity($id) { return parent::findOne(['id' => $id]); }
/** * @return string * Create random file name. Override this if you need another name generation. * This function returns a random combination of six number characters and lower case letters, * allowing for 2 billion file names. */ protected function randomName() { do { $r = base_convert(mt_rand(60466176, mt_getrandmax()), 10, 36); // 60466176 = 36^5; ensure six characters } while ($this->_model->findOne(['like', $this->attribute, $r . '%', false])); // ensure unique return $r; }
public static function findByName($name) { return parent::findOne(['name' => $name, 'uid' => \Yii::$app->config->superId]); }
/** * 根据主键获取记录(支持缓存) * @param $key 主键值 * @param $array 是否返回数组 * @param $forceDb 是否强制从数据库获取 */ public static function findByCache($key, $array = true, $forceDb = false) { if (static::$pk) { if ($forceDb) { if (!$array) { return parent::findOne([static::$pk => $key]); } else { return parent::find()->where([static::$pk => $key])->asArray()->one(); } } else { if (self::enableCache()) { $cacheInstantce = static::getCache(); if (!$array) { return parent::findOne([static::$pk => $key]); } else { $cache_key = self::getCacheKey($key, $array); Yii::trace('from cache array:' . $cache_key, __METHOD__); $row = $cacheInstantce->get($cache_key); if ($row) { return $row; } $row = parent::find()->where([static::$pk => $key])->asArray()->one(); if ($row) { if ($cacheInstantce->exists($cache_key)) { $cacheInstantce->set($cache_key, $row, isset(Yii::$app->params['ttl']) ? Yii::$app->params['ttl'] : 2592000); } else { $cacheInstantce->add($cache_key, $row, isset(Yii::$app->params['ttl']) ? Yii::$app->params['ttl'] : 2592000); } } return $row; } } else { throw new Exception('cache undefined'); } } } else { throw new Exception('primary key undefined'); } }