Yii2 Add math in where condition - yii2

In users table I have a column call "month".
I want to list all users that meet the condition: current month - user month <=2
Here my code
$time = new \DateTime('now');
$today = $time->format('m');
$users = Users::find()->where(['<=', 'month' - $today, 2])->all();
But this code is wrong. Please help me with this.
Hope this all make sense.
Thank you!

In Yii2 you can use different format for buil where condition
for this kind of situatio is useful use string format with param
in string format you can pass the literal string and param this way
$users = Users::find()->where('(month - :today ) <= 2' , [':today'=>$today])->all();
see this for more http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail

Related

i have two fields named as created_at and updated_at(timestamps) and i want to search between these dates in my where clause

i want to take input date as a parameter and search between the dates that are given in input.
Theses are the timestamps() provided by laravel to create the two fields as created_at and upated_at.I want to search between these dates that are given in created_at and updated_at but unable to find the data between these dates.
Following is the code that i have create
` $startDate = $request->input('created_at');
$endDate = $request->input('updated_at');
->select()
->whereBetween('created_at','updated_at',[$startDate,$endDate])
->get();`
I think whereBetween only accepts 2 parameters. The first being the column name and the second the array with values to test. Check: https://laravel.com/docs/5.5/queries#where-clauses
You would want something like:
->whereBetween('created_at', [$startDate,$endDate])
->whereBetween('updated_at', [$startDate,$endDate])
The error:
Argument 2 passed to Illuminate\Database\Query\Builder::whereBetween()
must be of the type array
is because the second parameter of whereBetween() is an array, and you pass a string 'updated_at' here. So change it to like:
->whereBetween('created_at', [$startDate,$endDate])
->whereBetween('updated_at', [$startDate,$endDate])
Reference

Array - StripTags

I have cities in my mysql table. I am getting those through an mysql query. Then I want to display in json format. However, json doesn't display due to not valid characters in the city names. Can anyone help on this? here is my code. I want to get json format somehow by using strip_tags or anything with this array. please help on this guys.
$zone = $mcon->query("SELECT name from tbl_cities ORDER BY name ASC");
$data = array();
while ($value = $zone->fetch_assoc()) {
$data[] = $value;
}
echo json_encode(array("text" => $data));
SELECT Replace( name , '-', ' ' ) from tbl_cities ORDER BY name ASC
This will replace all occurances of hyphens with spaces. There are many functions built into SQL to format the data before you pass it json.
I hope this helps.

Laravel eloquent how to parse data inside the query where attribute

I have following issue.
I need to get rows from database where price is lower than given number.
I can do it this way:
DB::where('price', '<=', $given_number)->get();
but the problem is that inside price cell I have number with added signs like $ for dollars or Euro sign. I know how to parse the string in PHP to extract only numbers - filter_var($string, FILTER_SANITIZE_NUMBER_INT) , but how can I do this inside the query from Laravel? How can i pre-parse this 'price' attribute?
Is this what you mean?
$price = filter_var($given_number, FILTER_SANITIZE_NUMBER_INT);
$products = Product::where('price', '<=', $price)->get();
Is there more information that you left out?

ZF2 Query WHERE clause with DATE() for datetime column

I have a column with datetime data type and I want to build a SQL query in Zend Framework2 which compare date part with user input date.
Need to build similar part as DATE(datetime column) = '2014-09-16' with;
$select->where();
would be very grateful if someone could help on this.
Use like this:
$date = '2014.05.24';
$select->where('date(expecting_date) = "'.$date.'"');
You should use predicate expression for these kind of conditions, like :
$select = new \Zend\Db\Sql\Select(table name);
$select->where(new \Zend\Db\Sql\Predicate\Expression('DATE(datetime) = ?', '2014-09-16'));

zend framework automatically alter queries

My database (mysql) tables use TIMESTAMP columns, and whenever I want them returned in a query, I want them to be queried as "UNIX_TIMESTAMP(columnname)".
How do you easily modify queries in zend framework to achieve this?
For example, the current code is:
select = $this->select();
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);
This eventually becomes:
select * from tablename where user_id = 42;
I want something that automatically finds the TIMESTAMP column and changes the resulting query to:
select user_id,name,unix_timestamp(created) where user_id = 42;
I know I can use a MySQL view to achieve this, but I'd rather avoid that.
Thanks.
RR
You should be able to specify the fields you want in the select using the $select->from() object.
Zend_Db_Select
You should end up with something like this.
$select = $this->select();
$select->from(
array('t' => 'tablename'),
array('user_id', 'name', 'UNIX_TIMESTAMP(created)')
);
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);
If you wanted to run an expression that doesn't have parenthese in the function, Use the Zend_Db_Expr() method to escape the query properly.