0
votes

I have the following tables: cars, car_types and car_methods. My issues with this are the relations. The car has one type and one method.

My goal is to show cars and his attachment "car type" and "car_method".

Cars table:

id - type_id - method_id

Car_types table:

id - type

Car_methods table:

id - method

How can I set this up inside my Model tables so that I can do inside my controller something like:

$this->Cars->CarTypes->find('all');

I tried as follow but it will give a No associated error:

Cars model:

class CarsTable extends Table {
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('cars');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->hasOne('CarTypes', [
        'className' => 'Cars.CarTypes',
        'foreignKey' => 'type_id',
        'propertyName' => 'type'
    ]);

    $this->hasOne('CarMethods', [
        'className' => 'Cars.CarMethods',
        'foreignKey' => 'method_id',
        'propertyName' => 'method'
    ]);
}
}

CarTypes model:

class CarTypesTable extends Table {

public function initialize(array $config) {
    parent::initialize($config);

    $this->setTable('car_types');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->hasMany('Cars', [
        'foreignKey' => 'type_id'
    ]);
}
}

CarMethots model:

class CarMethodsTable extends Table {

public function initialize(array $config) {
    parent::initialize($config);

    $this->setTable('car_method');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->hasMany('Cars', [
        'foreignKey' => 'method_id'
    ]);
}
}
2
Have you tried to the documentation? book.cakephp.org/3.0/en/orm/associations.htmlfloriank
Yes i've tries the documentation, but I do not quite understand it. I've added the code for you.CodeWhisperer
It's has many cars for types and methods. You point to the table itself for types and methods as you do right now. Do the tutorials in the book or some generic SQL tutorial on how to associated date. :)floriank

2 Answers

2
votes

Your two last relations are wrong, here is the good ones

CarTypes model:

public function initialize(array $config) {
    $this->hasMany('Cars', [  //A CarType hasMany Cars
        'foreignKey' => 'type_id'
    ]);
}

CarMethods model:

public function initialize(array $config) {
    $this->hasMany('Cars', [  //A CarMethod hasMany Cars
        'foreignKey' => 'method_id'
    ]);
}

Controller action

And for your query, add this two relations to the contain option :

//Get all cars with type and method
$this->Cars->find('all',
     [
        'contain' => ['CarTypes', 'CarMethods'] 
     ]
);
0
votes

The belongsTo association is a natural complement to the hasOne and hasMany associations: it allows us to see the data from the other direction.

belongsTo: the current model contains the foreign key.

If you have foreign key in your table, then that table will belong to the associated table. So, in your case :

public function initialize(array $config) {
     $this->belongsTo('CarTypes', [
        'foreignKey' => 'type_id',
        'joinType' => 'LEFT'
     ]);

     $this->belongsTo('CarMethods', [
         'foreignKey' => 'method_id',
         'joinType' => 'LEFT'
     ]);
}

After this you can use $this->Cars->CarTypes->find('all');