Yii Framework Forum: beginner: Relational Active Record - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

beginner: Relational Active Record Rate Topic: -----

#1 User is offline   odaxis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 28-November 08

Posted 28 November 2008 - 07:55 AM

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…

0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,901
  • Joined: 04-October 08
  • Location:DC, USA

Posted 28 November 2008 - 08:41 AM

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

#3 User is offline   rotschopf 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 01-December 08

Posted 01 December 2008 - 09:11 AM

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(
'up_account'=>array(self::HAS_ONE, 'up_account', 'plantID'),
);
}
---------------------------up_account-model------------------------
public function relations()
{
return array(
'plant'=>array(self::BELONGS_TO, 'plant', 'plantID'),
);
}

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

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,901
  • Joined: 04-October 08
  • Location:DC, USA

Posted 01 December 2008 - 11:06 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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