本文整理匯總了PHP中yii\db\Query::scalar方法的典型用法代碼示例。如果您正苦於以下問題:PHP Query::scalar方法的具體用法?PHP Query::scalar怎麽用?PHP Query::scalar使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類yii\db\Query
的用法示例。
在下文中一共展示了Query::scalar方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: encodePassword
public function encodePassword($password)
{
$query_pass = new Query();
$exppass = new Expression('password(:password)', [':password' => $password]);
$query_pass->select(['password' => $exppass]);
$hash = $query_pass->scalar();
// var_dump($hash);die;
return $hash;
}
示例2: getList
public static function getList($page = 1, $rows = 10, $condition = '', $conditionParams = [])
{
$return = [];
$query = new Query();
$query->select('COUNT(1)');
$query->from(self::tableName() . ' t0');
$query->where($condition, $conditionParams);
$result['total'] = $query->scalar();
$query->select(['t0.id', 't0.username', 't0.email', 't1.firstname', 't1.lastname']);
$query->join('LEFT JOIN', 'user_profile t1', 't0.id = t1.user_id');
$query->offset($page * $rows - $rows);
$query->limit($rows);
$result['rows'] = $query->All();
foreach ($result['rows'] as $k => $v) {
$myRoles = Yii::$app->authManager->getRolesByUser($v['id']);
if (count($myRoles)) {
foreach ($myRoles as $kk => $vv) {
$result['rows'][$k]['role'] = $kk;
break;
}
}
}
return $result;
}
示例3: getThreadCount
public function getThreadCount()
{
$query = new Query();
$query->select('count(*) as num')->from('{{%forum_thread}}' . ' p')->join('JOIN', '{{%forum_board}}' . ' b', 'b.id=p.board_id')->where('b.forum_id=:id', array(':id' => $this->id));
return $query->scalar();
}
示例4: beforeSave
public function beforeSave($insert)
{
if (!parent::beforeSave($insert)) {
return false;
}
$query = new Query();
$query->select('COUNT(*)')->where(['post_id' => $this->post_id])->from(static::tableName());
$count = $query->scalar(static::getDb());
if ($count == 0) {
$this->left = 1;
$this->right = 2;
$this->level = 1;
} else {
if (empty($this->parent_id)) {
$query = new Query();
$query->select('MAX({{right}})')->where(['post_id' => $this->post_id])->from(static::tableName());
$maxRight = $query->scalar(static::getDb());
$this->left = $maxRight + 1;
$this->right = $maxRight + 2;
$this->level = 1;
} else {
/*$query = new Query();
$query
->select(['{{left}} left', '{{level}} level'])
->where(['post_id' => $this->post_id, 'id' => $this->parent_id])
->from(static::tableName());
$data1 = $query->all(static::getDb());
static::getDb()
->createCommand('UPDATE ' . static::tableName() . ' SET {{right}} = {{right}} + 2 WHERE {{right}} > :left')
->bindValue(':left', $data1[0]['left'])
->execute();
static::getDb()
->createCommand('UPDATE ' . static::tableName() . ' SET {{left}} = {{left}} + 2 WHERE {{left}} > :left')
->bindValue(':left', $data1[0]['left'])
->execute();
$this->left = $data1[0]['left'] + 1;
$this->right = $data1[0]['left'] + 2;
$this->level = $data1[0]['level'] + 1;*/
$query = new Query();
$query->select(['id', '{{left}} left', '{{right}} right', '{{level}} level'])->where(['post_id' => $this->post_id, 'id' => $this->parent_id])->from(static::tableName());
$data1 = $query->all(static::getDb());
if ($data1[0]['left'] != $data1[0]['right'] - 1) {
$query = new Query();
$query->select(['id', '{{left}} left', '{{right}} right', '{{level}} level', '{{text}}'])->where(['post_id' => $this->post_id, 'level' => $data1[0]['level']])->andWhere('{{left}} > :r AND {{id}} != :id', [':r' => $data1[0]['right'], ':id' => $data1[0]['id']])->from(static::tableName());
$data2 = $query->one(static::getDb());
static::getDb()->createCommand('UPDATE ' . static::tableName() . ' SET {{right}} = {{right}} + 2 WHERE {{right}} > :left')->bindValue(':left', $data2['left'] - 2)->execute();
static::getDb()->createCommand('UPDATE ' . static::tableName() . ' SET {{left}} = {{left}} + 2 WHERE {{left}} > :left')->bindValue(':left', $data2['left'] - 2)->execute();
$this->left = $data2['right'] - 2;
$this->right = $data2['right'] - 1;
$this->level = $data1[0]['level'] + 1;
} else {
static::getDb()->createCommand('UPDATE ' . static::tableName() . ' SET {{right}} = {{right}} + 2 WHERE {{right}} > :left')->bindValue(':left', $data1[0]['left'])->execute();
static::getDb()->createCommand('UPDATE ' . static::tableName() . ' SET {{left}} = {{left}} + 2 WHERE {{left}} > :left')->bindValue(':left', $data1[0]['left'])->execute();
$this->left = $data1[0]['left'] + 1;
$this->right = $data1[0]['left'] + 2;
$this->level = $data1[0]['level'] + 1;
}
}
}
return true;
}
示例5: run
/**
* Runs the action.
*/
public function run()
{
if (!Yii::$app->request->isAjax) {
return;
}
Yii::$app->response->format = Response::FORMAT_JSON;
$languages = Yii::$app->i18n->getLanguages();
$category = Yii::$app->request->post('category');
$message = rawurldecode(Yii::$app->request->post('message'));
$translations = Yii::$app->request->post('translation');
$is_empty = Yii::$app->request->post('is_empty');
if ($translations) {
$query = new Query();
$query->select('id')->from($this->sourceMessageTable)->where(['category' => $category, 'message' => $message]);
$id = $query->scalar();
if ($id === false) {
Yii::$app->db->createCommand()->insert($this->sourceMessageTable, ['category' => $category, 'message' => $message])->execute();
$id = Yii::$app->db->getLastInsertID();
}
$json['message'] = false;
foreach ($translations as $id_language => $value) {
if (!isset($languages[$id_language])) {
continue;
}
if ($is_empty[$id_language]) {
$value = null;
$json['message'] = null;
} else {
if ($this->htmlEncode) {
$value = Html::encode($value);
}
if (Yii::$app->i18n->getId() == $id_language) {
if (Yii::$app->i18n->nl2br) {
$json['message'] = nl2br($value);
} else {
$json['message'] = $value;
}
}
}
$query = new Query();
$res = $query->from($this->messageTable)->where(['id' => $id, 'language_id' => $id_language])->exists();
if ($res) {
Yii::$app->db->createCommand()->update($this->messageTable, ['translation' => $value], ['id' => $id, 'language_id' => $id_language])->execute();
} else {
Yii::$app->db->createCommand()->insert($this->messageTable, ['id' => $id, 'language_id' => $id_language, 'translation' => $value])->execute();
}
}
$json['r'] = 1;
return $json;
} else {
$json['fields'] = [];
$json['adminLink'] = $this->getAdminLink();
foreach ($languages as $id_language => $language) {
$query = new Query();
$query->select("m.translation")->from($this->sourceMessageTable . ' AS s')->innerJoin($this->messageTable . ' AS m', 'm.id = s.id')->where(['m.language_id' => $language['id'], 's.category' => $category, 's.message' => $message]);
$value = $query->scalar();
if ($value === null) {
$translation = $value;
} else {
$translation = Html::decode($value);
}
if ($translation === false) {
$translation = '';
}
$json['fields']['#dot-translation-' . $id_language] = $translation;
}
return $json;
}
}