0

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)

  1. Have I defined well the relations ?
  2. How to express it in cakePHP, special events-refuels and events-repairs?
1
  • Can you explain the problem or question you have? You've provided a lot of good data, but - not sure what you're asking. Commented Oct 20, 2014 at 21:20

2 Answers 2

0

You missed

class User extends AppModel {
    public $hasMany = array('Car');
}
Sign up to request clarification or add additional context in comments.

Comments

-1

I've found that array('recursive' => 2) solve the problem!

But what is this and why ?

2 Comments

Recursive 2 is bad. Don't use it. Use Containable instead. Good news though, if setting recursive to 2 gets you the data you want, that means your associations are likely correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.