In your Player model:

class Player extends AppModel
{
    var $name = 'Player';
    var $primaryKey = 'player_id';    // <-- Without this, Cake
assumes that the primary key of your model is named 'id'.
    var $hasOne = array('Playerstat' => array(
                    'className' =>'Playerstat',
                    'foreignKey' =>'player_id',
                    'dependent' => true
    ));

}

On May 22, 1:30 pm, cooked <[EMAIL PROTECTED]> wrote:
> I am stumped with a problem when using model associations. For the
> some reason the findAll generates an incorrect join query. Will great
> appreciate any help. Both my models and controllers along with the
> MySql output
> is shown below.
>
> class Playerstat extends AppModel
> {
>     var $name = 'Playerstat';
>     var $belongsTo = array('Player' => array(
>                                               'className' => 'Player',
>                                               'foreignKey' =>
> 'player_id'));
>
> }
>
> class Player extends AppModel
> {
>     var $name = 'Player';
>     var $hasOne = array('Playerstat' => array(
>                                               'className' =>
> 'Playerstat',
>                                               'foreignKey' =>
> 'player_id',
>                                               'dependent' => true));
>
> }
>
> Each Player record has a corresponding Playerstat record. player_id is
> the primary key in
> table players and player_id is the foreign key in playerstat.
>
> I have the following code in one of my controller function
>
>         $status = PLAYER_ACTIVE;
>         $results = $this->Player->findAll("Player.active = $status AND
> Player.level = 'A'",
>                                            array("Player.fname", 
> "Player.lname",
> "Playerstat.wins", "Playerstat.loss",
>                                                    "Playerstat.points", 
> "Playerstat.last_played"),
> 'Playerstat.points DESC');
>
> I get the following result from MySql
>
> Query: SELECT `Player`.`fname`, `Player`.`lname`, `Playerstat`.`wins`,
> `Playerstat`.`loss`, `Playerstat`.`points`, `Playerstat`.`last_played`
> FROM `players` AS `Player` LEFT JOIN `playerstats` AS `Playerstat` ON
> (`Playerstat`.`player_id` = `Player`.`id`) WHERE `Player`.`active` = 1
> AND `Player`.`level` = 'A' ORDER BY `Playerstat`.`points` DESC
>
> Warning (512): SQL Error: 1054: Unknown column 'Player.id' in 'on
> clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 440]
>
> The players table does not have an "id" field it is suppose to be
> player_id. What I am doing wrong? How can I make "id" into
> "player_id".
>
> Thanks,
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to