Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii findAllByAttributes with BETWEEN DATES and ORDER BY

Tags:

php

yii

Im tring to do a search by attributes, on a given date range and ordered by date created, but no luck.

$user = array('user_country'=>1 ,'user_gender'='M');
    $rows = User::model()->findAllByAttributes($user,array("user_date_created BETWEEN '2012' AND '2013' " ,'order'=> 'user_date_created') );

Thank you in advance.

like image 703
chulian Avatar asked Feb 19 '13 18:02

chulian


2 Answers

Had to use a CDbCriteria, like this:

$attribs = array('user_country'=>1 ,'user_gender'=>'M');
$criteria = new CDbCriteria(array('order'=>'user_date_created DESC','limit'=>10));
$criteria->addBetweenCondition('user_date_created', $date['date_start'], $date['date_end']);
$rows = user::model()->findAllByAttributes($attribs, $criteria);

Hope it helps somebody in the future

like image 92
chulian Avatar answered Nov 18 '22 02:11

chulian


You can use to:

$criteria = new CDbCriteria;
$criteria->condition = "date_start >= '$date_start' AND date_end <= '$date_end'";
$model = TemporadaAlta::model()->find($criteria);
like image 42
Victor Azevedo Avatar answered Nov 18 '22 03:11

Victor Azevedo