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…