I've read about convections and I have my application/database straight.
I've read too about $hasOne, $hasMany, $belongsTo, etc
But, I'm facing a problem/issue that I can't solve for my self.
My models: Car, Event, Refuel, Repair
My controller: CarsController
My relations:
class Car extends AppModel {
public $BelongsTo = array('User');
public $hasMany = array('Event');
}
.
class Event extends AppModel {
public $BelongsTo = array('Car');
public $hasOne = array('Refuel');
}
.
class Refuel extends AppModel {
public $BelongsTo = array('Event');
}
Output of find() excecuted from CarsController
array(
'Car' => array(
'id' => '1',
'user_id' => '1',
'make' => 'Make',
'model' => 'Model',
),
'Event' => array(
(int) 0 => array(
'id' => '1',
'car_id' => '1',
'dateEvent' => '20-10-2014',
'description' => '1'
),
(int) 1 => array(
'id' => '2',
'car_id' => '1',
'dateEvent' => '20-10-2014',
'description' => '2'
),
(int) 2 => array(
'id' => '3',
'car_id' => '1',
'dateEvent' => '20-10-2014',
'description' => '3'
)
)
)
You need to know this:
Car belongs to a User. User may have many cars
Car has many events. An event just have one car.
To each Event should be associated one refuel or one repair. A refuel or repair cant have more than one event associated.
Tables:
users: id
cars: id, user_id
events: id, car_id
refuels: id, event_id (unique)
repairs: id, event_id (unique)
- Have I defined well the relations ?
- How to express it in cakePHP, special events-refuels and events-repairs?