本文整理汇总了PHP中yii\db\ActiveRecord::findOne方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::findOne方法的具体用法?PHP ActiveRecord::findOne怎么用?PHP ActiveRecord::findOne使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveRecord
的用法示例。
在下文中一共展示了ActiveRecord::findOne方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: init
public function init()
{
parent::init();
$this->target = Yii::createObject($this->targetClass);
if (!$this->condition) {
$this->target = $this->target->findOne($this->condition);
}
}
示例2: _delete
protected function _delete(ActiveRecord $model)
{
$id = Yii::$app->request->post('id');
$model->findOne($id)->delete();
// $model::deleteAll($id);
return $this->_success();
}
示例3: getIdByName
/**
* @param $name
* @return null|static
*/
public static function getIdByName($name)
{
$branch = parent::findOne(['branch_title' => $name]);
if ($branch) {
return $branch->id;
} else {
return 0;
}
}
示例4: findByUsername
public static function findByUsername($username)
{
return parent::findOne(['username' => $username]);
}
示例5: info
public static function info()
{
return parent::findOne(\Yii::$app->user->getId());
}
示例6: findByLabel
/**
* 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]);
}
示例7: findIdentity
/**
* @param string $id
* @return User
*/
public static function findIdentity($id)
{
return parent::findOne(['id' => $id]);
}
示例8: randomName
/**
* @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;
}
示例9: findByName
public static function findByName($name)
{
return parent::findOne(['name' => $name, 'uid' => \Yii::$app->config->superId]);
}
示例10: findByCache
/**
* 根据主键获取记录(支持缓存)
* @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');
}
}