findByPk() with

I’m using CDbCriteria->with to get additional data from other tables:




$criteria = new CDbCriteria;

$criteria->with = array('userData', 'orderedProducts');

return new CActiveDataProvider('AR_Orders', array(

	'criteria'=>$criteria,

	...

	),

));



It’s not clear to me, how to use ‘with’ when loading data via findByPk:

$order = AR_Orders::model()->findByPk($id, ‘’, [color="#FF0000"]???what to write here???[/color]));

Thanks!

$order = AR_Orders::model()->findByPk($id, $criteria);

Yes, my question wasn’t clear enough… Sorry about that.

I wanted to ask, it there a shorter version instead of creating and initializing a CDbCriteria object?

A kind of array or string?

http://www.yiiframework.com/doc/api/1.1/CActiveRecord/#with-detail




$order = AR_Orders::model()->with('userData', 'orderedProducts')->findByPk($id);



But if you don’t need to do the loading eagerly, you won’t need to do it.




$order = AR_Orders::model()->findByPk($id);

$userData = $order->userData;  // lazy loading of userData

$orderedProducts = $order->orderedProduscts; // lazy loading of orderedProducts;



what is wrong at this, because doesn’t work

$Criteria = new CDbCriteria();

$Criteria->condition = "price > 30";

$model=Posts::model()->findByPk($id, $Criteria);

Hi redjohn,

This is totally another story.

While we were talking about findByPk() and ‘with’, your problem is findByPk() and ‘condition’ (or ‘criteria’).

Usually we don’t need to supply any extra condition (criteria) when we call findByPk, because there is only one record that matches the primary key. When you supply some condition like ‘price > 30’ as the extra condition, you may get one record, or you may get NULL as the result, because the record with the given primary key may or may not have the price larger than 30. This is the expected behavior and findByPk is just working fine.

I guess what you really want is something like this:




$Criteria = new CDbCriteria();

$Criteria->condition = "price > 30";

$models = Posts::model()->findAll($Criteria);