I have two tables. Cars and tires. Tires can (!) Belong to the car. My tables look like this:
Tires:
id | car_id
-------------
1 | 17
2 | NULL
Cars:
id | name
-------------
17 | BMW
18 | Mercedes
From my point of view, if I want to get all (!) Tires (including the car to which they belong, if it is available), I cannot create an internal connection (therefore I cannot use the one containing). I need to use a left join. But I do not know how to automatically select all the fields on the table machines.
Inquiry:
$query = $this->Tires->find('all');
$query->leftJoin(
['Cars' => 'cars'],
['Cars.id = Tires.car_id']
);
SELECT Tires.id AS `Tires__id`, Tires.car_id AS `Tires__car_id`
FROM tires Tires
LEFT JOIN cars Cars ON Cars.id = Tires.car_id
But how do I automatically get all the fields from cars?
UPDATE
burzum actually gave me a solution that I quickly want to clarify, since I do not think it is a well-resolved cake ...
To achieve what I was trying to do, I need to add the following code:
$query
->select($this->Tires)
->select($this->Tires->Cars);
:
...
'Cars' => [
'id' => '17',
'name' => 'BMW'
]
...
, :
...
'car' => object(App\Model\Entity\Car) {
'id' => (int) 17,
'name' => 'BMW',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Cars'
}
...
, . , ...