I'm just writing my first phalcon application and have a question for filtering a query using Phalcon\Mvc\Model\Criteria.
Finally I want a query like this
SELECT *
FROM table
WHERE
status = 'A' AND
(
title LIKE 'combining%' OR
title LIKE 'phalcon%' OR
(
title LIKE 'criteria%' AND
title LIKE '%phalcon'
)
)
For me it seems there is no way for parenthesis in phalcons model criteria. The only way to achieve this is by writing phql.
Instead of writing a complete phql I maybe can write something like this, but that is getting the same complexity
<?php
$query = Table::query();
$query->where('status = :status:', array('status' => 'A'));
$query->andWhere('(
title LIKE :title1: OR
title LIKE :title2: OR (
title LIKE :title3: AND
title LIKE :title4:
)
)', array(
'title1' => 'combining%',
'title2' => 'phalcon%',
'title3' => 'criteria%',
'title4' => '%phalcon'
));
It looks like Criteria's "where", "andWhere" and "orWhere" methods will add the outer parenthesis for you, then you can manually write the inner parenthesis.
$query = Model::query()
->where('status = "A"')
->andWhere('title LIKE "combining%" OR
title LIKE "phalcon%" OR
(
title LIKE "criteria%" AND
title LIKE "%phalcon"
)
');
$models = $query->execute();
You can test that the where clause is correct.
var_dump($query->getWhere());
Related
I've made an API with the Yii2 framework.
But I don't know how to use the OR condition in my statement.
For example:
I want to get all cars with brand BMW or DODGE.
I've tried the following:
$query = Car::getCar($lang)
->where(['or', ['m.brand' => 'BMW'], ['m.brand' => 'DODGE']])
->all();
But this doesn't work.
I only get it to work with one value for m.brand.
So:
$query = Car::getCar($lang)
->where(['m.brand' => 'BMW'])
->all();
Works just fine.
Tried to put in a few other ways, but I don't get this to work.
Does anyone know what I'm doing wrong?
EDIT
The getCar method returns something like:
(new Query())->select(['a.auto_id'])->from('auto_new a')
EDIT 2
Got it to work with:
$query->andWhere(['or', ['m.brand' => 'BMW'], ['m.brand' => 'DODGE']])
You can actually simplify it a lot by using an array with the values you need:
$query = Car::getCar($lang)
->where(['m.brand' => ['BMW', 'DODGE']])
->all();
This will execute with something like WHERE m.brand IN ('BMW', 'DODGE') which returns the result you are looking for.
If I understand you well, you could use something like this:
Model::find()
->orWhere(['brand' => 'brand1'])
->orWhere(['id' => 'brand2'])
->all();
where() can take an array to create sql along the lines of
SELECT * FROM car WHERE brand in ('brand1', 'brand2');
using this construct you can generate an array of brands you wish to return then use the following ActiveQuery.
$brands = ['BMW', 'DODGE'];
$query = Car::find()->where(['brand' => $brands])->all();
First of all, hello there !=)
I was wondering if anyone knew how to write a select in a where such as
SELECT my_column
FROM my_table
WHERE field_1 = something
AND field_2 = ( SELECT field_2
FROM my_table_2
WHERE my_field = $myParam );
I know how to write this query without the select :
//This part shouldn't matter, it's just my way of connecting to table database
$select = $mytable1->getTableGateway()->getSQL()->select();
//This is where the fun begin
$select->colunms(array('my_column'))
->where(array('field_1' => 'something', 'field_2' => ??);
TRY 1 :
Using another variable that allows me to connect to the other base doesn't seem to work because of course it's not a array or anything...
$select_2 = $myTable2->tableGateway->getSQL()->select();
$select_2->colunms(array('field_2'))
->where(array('my_field' => $myParam);
$select->colunms(array('my_column'))
->where(array('field_1' => 'something', 'field_2' => $select_2);
I tried adding something like :
$var= $myTable2->getTableGateway()->selectWith($select_2);
//and then
$select->colunms(array('my_column'))
->where(array('field_1' => 'something', 'field_2' => $var->toArray());
It doesn't work either, because of course it's not one line...
I don't think a foreach is really necessary
Any thought before i go too far in my thoughts ?
Thank in advance
In ZF1 it works like this:
$select_2 = $myTable2->tableGateway->getSQL()->select();
$select_2->columns(array('field_2'))
->where(array('my_field = ?' => $myParam);
$select->columns(array('my_column'))
->where(array('field_1 = ?' => 'something', 'field_2 = ?' => $select_2)
Honestly, I'm not sure if it'll work in ZF2, but try to add the wildcard '?' to your where clause.
Best,
I essentially want to search the database using an array of barcodes. Here is my query if I only have one barcode:
$q = new CDbCriteria(array(
'condition' => '"barcode" = :barcode',
'params' => array(':barcode' => $this->barcode),
));
I am trying to modify this query so that I query an array of barcodes. It would be a fairly standard array, something like ['Barcode1','Barcode2', 'Barcode3'].
How can I modify this query I have to instead return the results for Barcode1 OR Barcode2 OR Barcode3?
You need to add an inCondition
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#addInCondition-detail
something like this
$q = new CDbCriteria();
$q->addInCondition("barcode",array("value1","value2"...),"AND");
I want to calculate the avrage of a mysql result in PHP (I'm using CodeIgniter as framework).
My (model) code:
$query = $this->db->query('SELECT stars FROM feedback');
$res = $query->result_array();
$avrage = array_sum($res); // The impossible part
$avrage = round($avrage,0);
The result of the query looks like this: (print_r) Array ( [0] => Array ( [stars] => 5 ) [1] => Array ( [stars] => 3 ) )
I just want the '5' and '3' in a separate array, so array_sum() can do it's job. How do I do this?
Thanks.
You can do it in SQL directly using AVG()
SELECT avg(stars) as avg_stars
FROM feedback
Why not use AVG directly in your query?
'SELECT AVG(stars) as average FROM feedback'
The best way to do this would be to modify your SQL query, like this: SELECT AVG(stars) as stars_average FROM feedback. Then, after fetching the result set, you can do the following in your PHP code: $average = $res[0]['stars_average'];.
In my database, I will have college building rooms (such as B100, A200, TLAB100) and I want it so that when people search for a specific room (such as B101, B102) it will return the building name without the ending number, so: B100.
My controller so far looks like this:
$results = $this->Building->find('all', array(
'conditions' => array(
'Building.building LIKE' => $this->data['Food']['q']
)
)
);
But I believe LIKE isn't the right command for it. Because it's not working that way
Why Like is not right?
$likeSmth = substr($this->data['Food']['q'], 0, -1);
'SELECT * FROM tbl WHERE roomnumber LIKE '.$likeSmth.'%'
and etc.