Hello, EveryBody
I have Db connection
'db'=>array(
'connectionString'=>'mysql:host=localhost;dbname=siteconst_yiicore',
'tablePrefix' => 'siteconst_',
//'class'=>'CDbConnection',
'username'=>'root',
'password'=>'',
'emulatePrepare'=>true, // needed by some MySQL installations
'charset'=>'utf8',
),
I Have db structure for Mysql 5.1
CREATE TABLE siteconst_Structure (
id int(11) NOT NULL auto_increment,
title varchar(255) NOT NULL,
...
PRIMARY KEY (id),
...
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE siteconst_Widget (
id int(11) NOT NULL auto_increment,
title varchar(255) NOT NULL,
...
PRIMARY KEY (id),
...
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE siteconst_WidgetStructure (
object_id int(11) NOT NULL,
structure_id int(11) NOT NULL,
...
PRIMARY KEY (object_id,structure_id),
...
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `siteconst_WidgetStructure`
ADD CONSTRAINT siteconst_WidgetStructure_ibfk_1 FOREIGN KEY (object_id) REFERENCES siteconst_Widget (id),
ADD CONSTRAINT siteconst_WidgetStructure_ibfk_2 FOREIGN KEY (structure_id) REFERENCES siteconst_Structure (id);
I have apropriate models
<?php
class Structure extends CActiveRecord
{
public function tableName()
{
return '{{Structure}}';
}
...
public function relations()
{
return array(
'widgets'=>array(self::MANY_MANY,'WidgetModel','{{WidgetStructure}}(object_id,structure_id)')
);
}
}
class WidgetModel extends CActiveRecord
{
public function tableName()
{
return '{{Widget}}';
}
public function relations()
{
return array(
'structures'=>array(self::MANY_MANY,'WidgetStructure','{{WidgetStructure}}(structure_id object_id)')
);
}
}
class WidgetStructure extends CActiveRecord
{
public function tableName()
{
return '{{WidgetStructure}}';
}
public function relations()
{
}
}
?>
Now I m trying get Widgets for appropriate structure
$model=Structure::model()->findbyPk($structure_id);
$model->widgets;
And i get error
CDbException
Description
The relation "widgets" in active record class "Structure" is specified with an invalid foreign key "object_id". The foreign key does not point to either joining table.
What iv done wrong?
Thank you