Yii Framework Forum: Need an example of how to access a joined table - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Need an example of how to access a joined table Rate Topic: -----

#1 User is offline   Didier 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 02-February 09

Posted 10 March 2010 - 04:30 PM

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?
0

#2 User is offline   jsoo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 01-March 10
  • Location:Durham, NC, USA

Posted 10 March 2010 - 05:48 PM

View PostDidier, 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'
            ),
        ),
    ),
));

1

#3 User is offline   Didier 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 02-February 09

Posted 14 March 2010 - 03:24 PM

Thanks for your assitance, helped me out a lot.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users