Yii Framework Forum: Count(Criteria) In Yii - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Count(Criteria) In Yii Rate Topic: -----

#1 User is offline   channasmcs 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 06-March 13
  • Location:Srilanka

Posted 28 October 2013 - 06:09 AM

hi i'm using yii restful api

normally use this Criteria to select offerdetail model data related with place_id

//http://localhost/coupon3.0/index.php/api/OfferDetail?place=1
if (isset($_GET['place']))
{
$Criteria = new CDbCriteria();
$Criteria->condition = sprintf("place_id = %s", $_GET['place']);
$models = OfferDetail::model()->findAll($Criteria);

}

Now i want to get num of offerdetail row related with place_id

Query SELECT count( * )FROM offer_detailWHERE place_id = '3' ;


how select this using criteria on web service help

[u]HINT :-[/u]

i try like this

http://localhost/cou...rDetail?count=1
if (isset($_GET['count'])){
$Criteria = new CDbCriteria();
$Criteria->condition = sprintf("place_id = %s", $_GET['count']); $count=OfferDetail::model()->count($Criteria);
$models = OfferDetail::model()->findAll($Criteria);
}
But Not Working
0

#2 User is offline   cynosure 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 25-October 13

Posted 28 October 2013 - 07:42 AM

Hi,

$place_id = Yii::app()->request->getParam('place');
if($place_id) {
 //build criteria
 $criteria = new CDbCriteria();
 $criteria->condition = 'place_id = :place_id';
 $criteria->params = array(':place_id' => $place_id);

 //query items
 $models = OfferDetail::model()->findAll($criteria);
 //count items
 $count = OfferDetail::model()->count($criteria);
}



Some words about the code:
First, try to avoid using the globals, Yii have it's own parameter handler.
Second, sprintf validates the variable type, but not gives as much protection as the Yii parameter binding over PDO.
Third, but it lacks in my example also, I better prefer building setter/getter methods in the model, not in the controller/view. But this is just my opinion, not a requirement.
2

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users