I am running into a small problem when trying to get a simple relational AR query to work. The error I am getting is:
'The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key "{key}". The foreign key does not point to either joining table.'
I am curious if the following method should compare case-insensitive $table->name to $name?:
private function tableMatch($schema,$table,$name)
{
if(strpos($name,'.')===false)
return $table->name===$name;
else
{
$table2=$schema->getTable($name);
return $table->rawName===$table2->rawName;
}
}
In my case, it seems to be returning false based on comparing Tablename===tablename
(if this is not the issue, I will post more about the specifics of my tables, relations, etc...)
Page 1 of 1
Comparison check in CActiveFinder->Tablematch() method
#2
Posted 26 November 2008 - 07:38 AM
The comparison should be case-sensitive. Would you please show me the DBMS, table name and relation information?
#3
Posted 26 November 2008 - 08:23 AM
Sure. Here are the two related parent and child tables:
The Parent:
CREATE TABLE `Post` (
`id` int(11) NOT NULL auto_increment,
`userId` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`blurb` text,
`content` text NOT NULL,
`createTime` int(11) NOT NULL,
`updateTime` int(11) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (userId) REFERENCES User(id)
ON DELETE CASCADE
) ENGINE=INNODB;
The Child:
CREATE TABLE `Comment` (
`id` int(11) NOT NULL auto_increment,
`postId` int(11),
`content` text NOT NULL,
`author` varchar(150) NOT NULL,
`createTime` int(11) NOT NULL,
`updateTime` int(11) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (postId) REFERENCES Post(id)
ON DELETE CASCADE
) ENGINE=INNODB;
Post has one entry with id = 1, with two related comments in Comment.
The relation defined in Post AR:
public function relations()
{
return array(
'comments'=>array(self::HAS_MANY, 'Comment', 'postId', 'order'=>'Comment.createTime DESC'),
);
}
And the query that generates the error:
$postList=Post::model()->with('comments')->findByPk(1);
Thanks.
The Parent:
CREATE TABLE `Post` (
`id` int(11) NOT NULL auto_increment,
`userId` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`blurb` text,
`content` text NOT NULL,
`createTime` int(11) NOT NULL,
`updateTime` int(11) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (userId) REFERENCES User(id)
ON DELETE CASCADE
) ENGINE=INNODB;
The Child:
CREATE TABLE `Comment` (
`id` int(11) NOT NULL auto_increment,
`postId` int(11),
`content` text NOT NULL,
`author` varchar(150) NOT NULL,
`createTime` int(11) NOT NULL,
`updateTime` int(11) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (postId) REFERENCES Post(id)
ON DELETE CASCADE
) ENGINE=INNODB;
Post has one entry with id = 1, with two related comments in Comment.
The relation defined in Post AR:
public function relations()
{
return array(
'comments'=>array(self::HAS_MANY, 'Comment', 'postId', 'order'=>'Comment.createTime DESC'),
);
}
And the query that generates the error:
$postList=Post::model()->with('comments')->findByPk(1);
Thanks.
Share this topic:
Page 1 of 1

Help













