tomas3man
tomas3man

Reputation: 29

CakePHP, table $belongsTo two table: error Error: Cannot redeclare table::$belongsTo

I have problem like that - have 3 tables, for example Clients, Hats, Tshirts. Each Client have one Hat and one Tshirt relation. I made this like that:

 `
  ClientsModel:
  var $name = 'Client';
  var $belongsTo = 'Hat';
  var $belongsTo = 'Tshirt';

  HatModel:
  var $name = 'Hat';
  var $hasMany = 'Client';

  TshirtModel:
  var $name = 'Tshirt';
  var $hasMany = 'Client';
  `

I cant display indext.ctp of Clients View, the error is: ` Fatal Error

   Error: Cannot redeclare Cleint::$belongsTo
   File: /var/www/uat/app/Model/Client.php 
   `

How to deal with that relation in my database?

Upvotes: 1

Views: 606

Answers (1)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

To declare a belongsTo relation you need to create an array of object not a single var for each belongsTo relation.
In your you client Model the belongsTo relation needs to be like this:

public $belongsTo = array(
    'Hat' => array(
        'className'     => 'Hat',
        'conditions'    => '',
        'order'         => '',
        'foreignKey'    => 'hat_id' //or your external key
    ),
    'Tshirt' => array(
        'className'     => 'Tshirt',
        'conditions'    => '',
        'order'         => '',
        'foreignKey'    => 'tshirt_id' //or your external key
    )
);

MANUAL

Upvotes: 1

Related Questions