Using the findAll AR function that returns only one object when using the with statement

I am having a problem with my Yii installation where I am trying to get a fairly simple request, but I am not getting results that online tutorials say what I should get. I have two models that look something like this:

Rates:

class Pricing extends CActiveRecord { /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return Pricing the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * @return string the associated database table name */ public function tableName() { return 'pricing'; } /** * @return string the primary key */ public function primaryKey(){ return 'ID'; } ... /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'xpricing_routes' => array(self::HAS_MANY, 'PricingRoutes', 'ID_pricing'), ); } 

and PricingRoutes:

 class PricingRoutes extends CActiveRecord { /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return PricingRoutes the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * @return string the associated database table name */ public function tableName() { return 'pricing_routes'; } /** * @return string the primary key */ public function primaryKey(){ return 'ID'; } ... /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'xpricing' => array(self::BELONGS_TO, 'Pricing', 'ID_pricing'), ); } 

Then in the controller we have:

 $criteria = new CDbCriteria; $criteria->with = array('xpricing_routes'); $criteria->together=true; $pricing_records = Pricing::model()->findAll($criteria); $pricing_records_arr = CHtml::listData($pricing_records, 'id', 'name'); echo '<pre>'; print_r($pricing_records); print_r($pricing_record_arr); echo '</pre>'; 

As you probably already know, we have 2 tables called prices and prices. The pricing route table has a foreign key called ID_pricing, which is sent to the ID field in the price table. The pricing table has one record, and the pricing_routes table has 4 records, each of which has a primary key for one element in the pricing table in the ID_pricing field. Therefore, we should get 4 results in the query that we run, and when I run the query that Yii generates using AR, this is what I get.

The problem we are facing is that the $ pricing_records variable is an array that has only one pricing object. This object contains the data that we need, but are not really applicable. The $ pricing_records_arr variable is just an empty array. The documentation I found seems to suggest that we should get an array of pricing objects, each of which contains information from the pricing_routes table. We know that using AR may not be the best way to get this data, but we have reasons for it to work, so any ideas on how to do this will be greatly appreciated.

EDIT:

It turns out this turned out to be a misunderstanding of what I am returning. Comments on this issue gave me the information I need.

+4
source share
2 answers

If you call

 $pricing_records = Pricing::model()->findAll($criteria); 

you will receive only one active record with properties filled with values ​​from the Pricing table. If you want to get all the entries from "pricing_routes" belonging to that particular "price", you need to call

 $pricing_records->xpricing_routes 

where "xpricing_routes" is the name of the relationship that you defined correctly in your model. It returns an array of PricingRoutes or null if there is no corresponding entry.

+1
source

if you use findall it will return an array even if there is one entry

 $pricing_records = Pricing::model()->findAll($criteria); foreach($pricing_records as $var){ $var->xpricing_routes->ID_pricing;//any variable } 

or if there is only one field, you can use

 $pricing_records = Pricing::model()->find($criteria); $pricing_records->xpricing_routes->ID_pricing;//any variable
$pricing_records = Pricing::model()->find($criteria); $pricing_records->xpricing_routes->ID_pricing;//any variable 
0
source

All Articles