Answer the question
In order to leave comments, you need to log in
Prepared queries in yii2?
Hello! There is this code:
$where = '';
if(!empty($filter_time)) $where =" and date_created > now()-Interval <b>$filter_time </b> minute ";
if(!empty($filter_category))
$where.= "and path LIKE '<b>$filter_category</b>%' ";
$sql = Yii::$app->db->createCommand("SELECT COUNT(*) FROM Pages
JOIN categories ON categories = id_categories WHERE status = 1 $where")->queryScalar();
bindValues()
.
Answer the question
In order to leave comments, you need to log in
Try like this:
$where = '';
if(!empty($filter_time)) $where =" and date_created > now()-Interval :filter_time minute ";
if(!empty($filter_category))
$where.= "and path LIKE ':filter_category%' ";
$sql = Yii::$app->db->createCommand("SELECT COUNT(*) FROM Pages
JOIN categories ON categories = id_categories WHERE status = 1 $where")
->bindValue(':filter_time', $filter_time)
->bindValue(':filter_category', $filter_category)
->queryScalar();
Why are you working with the Command class? by doing this, you only create difficulties for yourself ... to create complex queries in yii there is a Query class that perfectly solves your problem, for example like this:
$query = new \yii\db\Query();
$query->from('Pages')
->leftJoin('categories', ['categories' => 'id_categories'])
->where(['status' => 1])
->andFilterWhere(['like', 'path', $filter_category]);
if (! empty($filter_time)) {
$expression = new \yii\db\Expression('now() - interval :filter_time minute', [
':filter_time' => $filter_time
]);
$query->andWhere(['>', 'date_created', $expression]);
}
Есть такой код
kill whoever wrote it.
Why use a framework if you don't use AR and its other goodies?
Example from the docs:
$post = Yii::$app->db->createCommand('SELECT * FROM post WHERE id=:id AND status=:status')
->bindValue(':id', $_GET['id'])
->bindValue(':status', 1)
->queryOne();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question