I have two tables, entity and alt. An entity can have many alt's, but any alt belongs to only one entity.
Yii regonizes this properly in the model.
What I am stuck on is how to proceed from here. How do I now access the data so that I can show int the alt view/update pages the data from entity AND during create populate a dropdown with all entity's for selection?
Does anyone have an example of a similar situation that they are willing to share?
Page 1 of 1
Need an example of how to access a joined table
#2
Posted 10 March 2010 - 05:48 PM
Didier, on 10 March 2010 - 04:30 PM, said:
What I am stuck on is how to proceed from here. How do I now access the data so that I can show int the alt view/update pages the data from entity AND during create populate a dropdown with all entity's for selection?
I've done this using Form builder. For views/alt/_form.php:
return array(
'showErrorSummary' => true,
'elements' => array(
'name' => array(
'alt1' => 'text',
'alt2' => 'text',
),
'altEntity' => array(
'type' => 'dropdownlist',
// specify label in controller action
),
),
'buttons' => array(
// etc.
),
);'altEntity' is the name of Alt's relation to Entity.
Then in AltController.php::actionAdd() or actionUpdate():
$form['altEntity']->label = $model->getAttributeLabel('entityName');
$form['altEntity']->items = CHtml::listData(Entity::model()->findAll(), 'id', 'name');rendering the form with
$this->render($viewFile, array(
'form' => $form,
'model' => $model,
));'entityName' is defined in Alt::attributeLabels(); $model is from $this->loadModel() of course, and gets you the data for that alt.
To include relational data in a CGridView, as you might use with an index action, pass the CGridView the following dataProvider:
new CActiveDataProvider('alt', array(
'criteria' => array(
'with' => array(
'entity' => array(
'select' => 'foo, bar'
),
),
),
));
Share this topic:
Page 1 of 1

Help













