Case senstive in yii2 - yii2

I have this query
$query->orWhere(['order_delivery_address_city' => 'London']);
How to add parameter to the query, when city in database like 'london' (lower case), string not added in gridview?

If you are looking for an exact match for the string you can use BINARY
eg using the yii2 operator notation you could
$query->orWhere(['= BINARY', 'order_delivery_address_city', 'London'])
or LIKE
$query->orWhere(['LIKE BINARY', 'order_delivery_address_city', 'London'])

Try This :
$query->orWhere(['LIKE','order_delivery_address_city','London']);

Related

BOOLEAN and LIKE search together with Yii2?

I use YII2 Framework and I've built this search in BOOLEAN MODE:
if( $campi[$i] == "PossessoreElenco" ){
if(strpos($valor[$i], ' OR ') !== false) {
$titOR = str_replace(" OR ", ' ', $valor[$i]);
$query.= 'MATCH(PossessoreElenco) AGAINST("'.$titOR.'" IN BOOLEAN MODE)'; }
Now, if I write "Marc*" the result show both this : "Marco", "San Marco". This is right, but is not the result that I want. I would to take only the result that STARTS with the word that I write. So, at the end if I write Marc* OR Mich* in BOOLEAN MODE, I want to search for the result that STARTS with "Marc" or "Mich" (example 'Marco' or 'Michele') and not all results that CONTAINS the words (example, I don't want 'San Marco'). There is an opportunity to implement this option mantaining the boolean search?
I can use LIKE 'Marc%', but in this solution I lose the boolean search.
Thank you for the help!
The Boolean Full-Text Searches is based on word and don't care for the beginning of the string ..
if you need this you could enforce the query adding an having cluase fro filter the risulting rows
WHERE MATCH(PossessoreElenco) AGAINST( ? IN BOOLEAN MODE)
HAVING PossessoreElenco LIKE caoncat(?,'%')
NB The use of an having clause for filter the result without aggregation function is pretty improper
So you could use your main query as a subquery for apply the like eforcemnet
select *
from (
SELECT
....
WHERE MATCH(PossessoreElenco) AGAINST( :my_word IN BOOLEAN MODE)
) T
WHERE T.PossessoreElenco LIKE concat(:my_word,'%')
You should avoid the use of php var in sql because this can produce sqlijcetion for avoid tgis you could use named param and use the related binding function provided by Yii2

Symfony Doctrine: Search in simple_array

I got values stored in my database column field as value1,value2,value3,value4, so a simple_array column.
So i'm using Doctrine to make a search using this:
$searchQuery = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->andWhere("p.vals <= :value2")
->setParameter('value2', $request->query->get('value2'));
->orderBy("p.creationtime", 'DESC');
So expecting value2 is in the 2nd position of a simple array like value1,value2,value3, how can i ask QueryBuilder to select the second value in the string?
I think this query try to get all the values in p.vals, results are not right, shound select just one.
How can I select eg. the 2nd value in p.vals?
I believe you cannot access nth item of an array column using pure Mysql since the data is serialized, in order to do it I'd create a simple function
public function getItemFromArray(array $array, $index)
{
return isset($array[$index]) ? $array[$index] : null;
}
And if you want to find item with condition use
array_filter()

In Yii2 how to perform a sql function inside andFilterWhere() method? or What is the alternative method?

->andFilterWhere(['=>',"invoiceDate - dueDate",$this->overDueLimit])
i need this filter to extract overDue invoices in gridview. "invoiceDate - dueDate" should be a sql function which returns a number. but Yii2 takes this as a string(as a field name).
How to perform the same task in a correct way? Thanks
Thanks Scais Edge.I found the solution based on your suggestion. I added the following lines to the search method and it solved my problem.
if(!empty($this->overDue))
{
$terms=MyFunc::validate($this->overDue);// sanitize and get number
$query->andFilterWhere(['>=',"DATEDIFF(CURDATE(),`dueDate`)",$terms])
->andFilterWhere(['=', 'status', self::$pending]);
}
seems the filterWhere don't allow calculated value so you can use instead andWhere() that admit literal where condition. So you can create your where condition for the query you need .. in this case you must check if the value of $this->overDueLimit is setted ..
if ( isset($this->overDueLimit ) ) {
$query->andWhere("invoiceDate - dueDate => " . $this->overDueLimit);
}
check obviusly for a proper sanitaze of the var $this->overDueLimit

REGEX for selecting multiple value in string

I need an sql select statement to retrieve 04:30 and test.zip from this string:
{"TIME":"04:30","DATE":"11\/25\/2013","FILENAME":["test.zip"]}
use this \[(.*?)\]
it return value between [ and ]
and for 04:30 use TIME":(.*?),
it return value after "TIME":
Can't you just decode it and use PHP? (assuming you can't change the way it's stored in the db)
<?php
$str = '{"TIME":"04:30","DATE":"11/25/2013","FILENAME":["test.zip"]}';
$o = json_decode($str);
$time = $o->TIME;
$file = $o->FILENAME[0];
var_dump($time); //"04:30"
var_dump($file); //"test.zip"
Regex replaces etc in MySQL require a UDF (user-defined function) mysql-udf-regexp
If none of the above are viable solutions (change DB structure, do it with PHP, use a MySQL UDF), you'll need to get creative. It would require a known, static format of that string, but you could replace some parts and substring others. For example:
SELECT SUBSTRING(REPLACE(`column_name`,'{"TIME":"',''),1,5) AS `time` FROM `table_name`
File is more complex, this example assuming only one filename in the array
SELECT REPLACE(SUBSTRING(`column_name`,LOCATE('"FILENAME":["',`column_name`)+13),'"]}','') AS `file` FROM `table_name`
Those two field selections get 04:30 and test.zip respectively (you can of course use those functions in the same statement, rather than separately like I have, by comma separating them)

How to use MySQL FORMAT with CakePHP?

I cant figure out why when I try to use FORMAT function to limit number of decimal places in the results of MySQL query it doesn't work. Here is how my code looks like:
...some other options to join tables with some conditions....
$options['fields'] = array(
'MetricSim.sim_id',
'MetricSim.metric_id',
'FORMAT(MetricSim.value,3) AS value'
);
$metrics_sims = $this->Sim->find('all', $options);
If I don't use the FORMAT function I get all of the results as expected. But when I try to use it I just don't get value field in my results (the rest of the fields are in place).
Why do you want to use FORMAT in your query? You can use a Helper to format your data in your view.
For example, in your controller class you add:
var $helpers = array('Number');
and in your view, you can format the value like:
$number->format($metric_sims['MetricSim']['value']);
(See NumberHelper class)