tags:

views:

148

answers:

2

I have two tables

big (id, bigs_name, smallid),

small(id, smallguys_name)

therefore two models - big and small

i have used the following relation in the big model, (I hope this relation is correct)

'has_small'  => array(self::HAS_ONE, 'small', 'smallid')

by default the actionIndex was created as

public function actionIndex()
{
  $dataProvider=new CActiveDataProvider('IphoneSubscription');
  $this->render('index',array(
   'dataProvider'=>$dataProvider,
  ));
}

In the _view.php I want to display 'smallguys_name' in place of 'smallid' which is displayed by default. That is in normal php I would have taken 'smallid' and selected the corrected row from 'small'. How do I do it in yii?

I have this in _view.php -

echo $data->smallid;

output - the id from 'big'

I tried this -

echo $data->has_small->smallguys_name;

and i get the following CDbException

Column not found: 1054 Unknown column 'has_small.smallid' in 'where clause'

thanks for the help

A: 

The problem is that with a "HAS_ONE" relationship, it is trying to query the SMALL table for SMALLID (has_small.smallid) instead of ID (has_small.id). If BIG really has a "HAS_ONE" relationship with SMALL, you need to put the foreign key for BIG in SMALL (flip flop them), like so:

big (id, bigs_name)

small(id, smallguys_name, bigid)

'has_small'  => array(self::HAS_ONE, 'small', 'bigid')

Otherwise, if you want to keep your DB structure the same, I would use the BELONGS_TO relation like you mentioned in your comment:

'has_small'  => array(self::BELONGS_TO, 'small', 'smallid')
thaddeusmt
thanks, i'll give it a try.
Anand
A: 

You should like this in big model for relation to small from big,

'has_small'  => array(self::BELONGS_TO, 'small', 'smallid');
Rahul