Thursday, 15 August 2013

php - Yii relation in relation and join table attributes -



php - Yii relation in relation and join table attributes -

i have next models: products, addons, addontypes, productaddons.

the schema following:

product id name addons id name type_id addontypes id name productaddons id product_id addon_id cost

the question: how can bring together these tables in relation products simple that

productaddons::model()->with('addons')->findall() ?

q1: related addon cant figure out, how can addon type? q2: how can attribute bring together table. cost productaddons?

i hope work you. using "with()" homecoming array. on 1:1 relations... yii ;).

/* @var $productswithaddonly productaddons */ $productswithaddonly = productaddons::model()->with('addons')->findall(); if (!empty($productswithaddonly->addons) && is_array($productswithaddonly->addons)) { /* @var $addon addons */ foreach ($productswithaddonly->addons $addon) { if (isset($addon->type)) { var_dump($addon->type); } } }

btw. should maintain model names singulary ...

get related models products by:

$criteria=new cdbcriteria; $criteria->with=array( 'addons.addons', ); $prdocutswithaddons = product::model()->findall($criteria);

php mysql activerecord yii

No comments:

Post a Comment