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?
I’ve done this using Form builder. For views/alt/_form.php:
'showErrorSummary' => true,
'elements' => array(
'name' => array(
'alt1' => 'text',
'alt2' => 'text',
'altEntity' => array(
'type' => 'dropdownlist',
// specify label in controller action
'buttons' => array(
‘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
'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'
Thanks for your assitance, helped me out a lot.