Yii Framework Forum: Comparison check in CActiveFinder->Tablematch() method - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Comparison check in CActiveFinder->Tablematch() method Rate Topic: -----

#1 User is offline   jefftulsa 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 168
  • Joined: 06-October 08
  • Location:Austin, TX

Posted 26 November 2008 - 12:42 AM

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


0

#2 User is offline   qiang 

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

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

#3 User is offline   jefftulsa 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 168
  • Joined: 06-October 08
  • Location:Austin, TX

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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