Caner Caner - 11 days ago 5
PHP Question

Correct PHP syntax for relation between models?

I have 2 Models, Cars & Brands

When I want to create new car and set values everything works like

$car->setName('Golf V');
$car->setYear('2015'); // setYear on $car
$car->setNote('This car has electric problems'); // setNote on $car


But if I want to set Brand which has relation to other table then I get error: must be an instance of Salon\Models\Brand all id's exist, only insert is the problem, what I tried is:

$car->setBrand(\Salon\Models\Brand = $_['brand_id']);
$car->setBrand(\Salon\Models\Brand::getId()->$_['brand_id']);


It is of course not correct, but can anyone tell me the correct syntax?

Answer

I think you should understand the SOLID principles first, to be able to solve these kinds of problems.

You can take a look at S.O.L.I.D specially the Interface Segregation Principle