beginner: Relational Active Record

Dear all,

I try to setup a small test based on the Relational Active Record but I’m not able to achieve it.

I get this error at runtime:

“The relation "organizer" in active record class "Event" is specified with an invalid foreign key "organizer_oid". The foreign key does not point to either joining table.”

I’ve created 2 tables in MySQL:

create table organizer (

  oid int(11) NOT NULL auto_increment,

  shortName varchar(24) not null,

  longName varchar(64),

  PRIMARY KEY  (oid)

);

CREATE TABLE  event (

  oid int(11) NOT NULL auto_increment,

  code varchar(24) not null,

  startDate date not null,

  endDate date not null,

  status varchar(12) not null,

  organizer_oid int not null,

  description varchar(250),

  htmldescription text,

  PRIMARY KEY  (oid),

  FOREIGN KEY FK_Event_Organizer (organizer_oid) REFERENCES organizer (oid)

);

insert into organizer values (1, 'EMMA', null);

insert into organizer values (2, 'AFAC', null);

insert into event values (1, 'EVT1', '20/12/2008', '24/12/2008', 'TRMT', 1, null, null);

Then I generated via yiic the model for the 2 classes, and I modified the relations() function on the Event class that way:

public function relations()

{

return array(

'organizer'=>array(self::BELONGS_TO, 'Organizer', 'organizer_oid'),

);

}

The error is output when I try to access $model->organizer

<h2>Event List</h2>

<div class="actionBar">

[<?php echo CHtml::link('New Event',array('create')); ?>]

</div>

<table class="dataGrid">

  <tr>

    <th><?php echo $this->generateColumnHeader('oid'); ?></th>

    <th><?php echo $this->generateColumnHeader('code'); ?></th>

    <th><?php echo $this->generateColumnHeader('startDate'); ?></th>

    <th><?php echo $this->generateColumnHeader('endDate'); ?></th>

    <th><?php echo $this->generateColumnHeader('status'); ?></th>

    <th><?php echo $this->generateColumnHeader('organizer_oid'); ?></th>

    <th><?php echo $this->generateColumnHeader('description'); ?></th>

        <th>Actions</th>

  </tr>

<?php foreach($eventList as $n=>$model): ?>

  <tr class="<?php echo $n%2?'even':'odd';?>">

    <td><?php echo CHtml::link($model->oid,array('show','id'=>$model->oid)); ?></td>

    <td><?php echo CHtml::encode($model->code); ?></td>

    <td><?php echo CHtml::encode($model->startDate); ?></td>

    <td><?php echo CHtml::encode($model->endDate); ?></td>

    <td><?php echo CHtml::encode($model->status); ?></td>

    <td><?php echo CHtml::encode($model->organizer->shortName); ?></td>

    <td><?php echo CHtml::encode($model->description); ?></td>

    <td>

      <?php echo CHtml::link('Update',array('update','id'=>$model->oid)); ?>

      <?php echo CHtml::linkButton('Delete',array('submit'=>array('delete','id'=>$model->oid),'confirm'=>'Are you sure?')); ?>

        </td>

  </tr>

<?php endforeach; ?>

</table>

<?php $this->widget('CLinkPager',array('pages'=>$pages)); ?>

Any idea would be really appreciated!

Many thanks in advance…

Please update to the latest SVN. There is a bug that was fixed yesterday.

I think there is still a problem…

---------------------------------DB_DUMP-------------------------

– Table structure for table plants

CREATE TABLE IF NOT EXISTS plants (

  id int(10) unsigned NOT NULL,

  name varchar(40) NOT NULL,

  PRIMARY KEY  (id)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;


– Table structure for table up_accounts

CREATE TABLE IF NOT EXISTS up_accounts (

  plantID int(10) unsigned NOT NULL COMMENT 'Foreign Key pointing to the Plant',

  login varchar(40) NOT NULL,

  pass varchar(40) NOT NULL,

  PRIMARY KEY  (plantID),

  UNIQUE KEY login_uniq (login)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

----------------------------plant-model-------------------------------

public function relations()


{


	return array(


		&#039;up_account&#039;=&gt;array(self::HAS_ONE, &#039;up_account&#039;, &#039;plantID&#039;), 


	);


}

---------------------------up_account-model------------------------

public function relations()


{


	return array(


		&#039;plant&#039;=&gt;array(self::BELONGS_TO, &#039;plant&#039;, &#039;plantID&#039;), 


	);


}

Same problem as above occurss when accessing plant::model()->findByPK(1)->up_account or up_account::model()->findByPK(1)->plant

The bug I fixed is irrelevant to your problem.

Please refer to the first ‘Info’ block in http://www.yiiframew…de/database.arr to solve your problem.