public function actionTestManyToMany() { $items = Item::model()->findAll(); $ingredients = Ingredient::model()->findAll(); foreach ($items as $item) { echo $item->name . " has " . count($item->ingredients) . " ingredients. They are:<br />"; foreach ($item->ingredients as $ingredient) { echo $ingredient->name . "<br />"; } echo "<br />"; } echo "<hr />"; foreach ($ingredients as $ingredient) { echo $ingredient->name . " is associated with " . count($ingredient->items) . " items. They are:<br />"; foreach ($ingredient->items as $item) { echo $item->name . "<br />"; } echo "<br />"; } }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = Ingredient::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }