Reputation: 1382
Im not sure if I need to use class inheritance but I'll explain what I'm trying to accomplish. Let's say I have two classes:
class Car
{
protected $id;
protected $make;
protected $model;
protected $price;
public function getPrice()
{
return $this->price;
}
/* more getters and setters */
}
and another class:
class Order
{
protected $id;
protected $carId;
protected $quantity;
protected $price;
/* getters and setters */
}
In my controller I have $carId and $quantity and I need to calculate the price of that order. To look from database perspective single order can have only one car but single car can have many orders. I know I can accomplish this by making another query to the database to get the price but I think there should be a better way to do it. Maybe with class inheritance I don't know. so I would really appreciate if anyone could explain me some other way on how to accomplish this.
Upvotes: 0
Views: 57
Reputation: 13300
You don't understand what you need. The case that you described is not about inheritance it is about relationship.
You need to add relation between Car
and Order
. If you use Doctrine as your ORM you can just follow instructions in the official documentation: http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations
Then you can call $order->getCar()
to get Car
entity that is related to your order.
Upvotes: 1