Yii Framework Forum: [SOLVED] Foreign key problem - Yii Framework Forum

Jump to content

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

[SOLVED] Foreign key problem Need help with this piece of code. Rate Topic: -----

#1 User is offline   Jesper 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-May 10

Posted 08 May 2010 - 02:58 PM

Hello.
I am trying to create a new website. Currently i am very pleased with working with yii. Still having some Foreign key issues.

My problem is this:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'players.playerid' in 'where clause'

The database is from a game im trying to setup.
a "Base" allways has an owner but not allways an occupier.

Any ideas on how to handle it in the relations when i relate to all the bases the player has?

Data structures and relations:

Players model:
return array(
"bases" => array(self::HAS_MANY,"Bases","playerid",
'condition'=> "players.playerid = bases.ownerid"
),
"occupied_bases" => array(self::HAS_MANY,"Bases","playerid"),
"fleets" => array(self::HAS_MANY,"Fleet","ownerid")
);

Bases model:
return array(
'planet' => array(self::BELONGS_TO,"Planets", "baseid"),
'owner' => array(self::BELONGS_TO, "Players","ownerid"),
'occupier' => array(self::HAS_ONE,"Players","occupierid"),
'commander' => array(self::HAS_ONE,"Commanders","commanderid"),
'structures' => array(self::HAS_MANY,"Structures","baseid")
);

Players SQL:
CREATE TABLE `players` (
`playerid` int(11) NOT NULL,
... (more unnessesary coloums)
UNIQUE KEY `playerid` (`playerid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Bases SQL:
CREATE TABLE `bases` (
`baseid` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
...
`ownerid` int(11) DEFAULT '0',
`occupierid` int(11) DEFAULT '0',
`commanderid` int(11) DEFAULT '0',

UNIQUE KEY `baseid` (`baseid`),
KEY `ownerid` (`ownerid`),
KEY `occupierid` (`occupierid`),
KEY `commanderid` (`commanderid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `bases`
ADD CONSTRAINT `bases_ibfk_14` FOREIGN KEY (`commanderid`) REFERENCES `commanders` (`commanderid`),
ADD CONSTRAINT `bases_ibfk_11` FOREIGN KEY (`baseid`) REFERENCES `planets` (`baseid`),
ADD CONSTRAINT `bases_ibfk_12` FOREIGN KEY (`ownerid`) REFERENCES `players` (`playerid`),
ADD CONSTRAINT `bases_ibfk_13` FOREIGN KEY (`occupierid`) REFERENCES `players` (`playerid`);
0

#2 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 08 May 2010 - 03:34 PM

Yii creates alias 't' for main table, so try to use t.playerid.
0

#3 User is offline   Jesper 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-May 10

Posted 08 May 2010 - 04:07 PM

Giving same error:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't.playerid' in 'where clause'
0

#4 User is offline   Jesper 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-May 10

Posted 08 May 2010 - 04:43 PM

I found the problem. Still i had tried that before.

return array(
"bases" => array(self::HAS_MANY,"Bases","ownerid",
),
"occupied_bases" => array(self::HAS_MANY,"Bases","playerid"),
"fleets" => array(self::HAS_MANY,"Fleet","ownerid")
);

the only thing changed is playerid is set to ownerid...
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