CakePHP 3:如何从 leftJoin 自动获取字段?

CakePHP 3: How to automatically get fields from leftJoin?

我有两个 table。汽车和轮胎。轮胎可以(!)属于汽车。我的 table 看起来像:

轮胎:

id  |  car_id
-------------
1   |  17
2   |  NULL

汽车:

id  |  name
-------------
17  |  BMW
18  |  Mercedes

根据我的理解,如果我想获得所有(!)轮胎(包括它们所属的汽车,如果有的话)我无法创建内部连接(所以我不能使用包含)。我需要使用左连接。但是这样我就不知道如何自动 select table 汽车上的所有字段。

我做的查询:

$query = $this->Tires->find('all');
$query->leftJoin(
    ['Cars' => 'cars'],
    ['Cars.id = Tires.car_id']
);

// brings this SQL query
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

但是我如何自动获取汽车的所有字段?

更新 burzum 实际上给了我解决方案,我很快想详细说明,因为我认为 cake 不能很好地解决它...

为了实现我尝试做的事情,我需要添加以下代码:

$query
  // you need to pass each model you want to get fields for
  ->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'
    }
...

所以,我可以使用它。不过,我还是不明白不同的输出...

http://book.cakephp.org/3.0/en/orm/query-builder.html#selecting-all-fields-from-a-table

上面的例子URL:

// Only all fields from the articles table including
// a calculated slug field.
$query = $articlesTable->find();
$query
    ->select(['slug' => $query->func()->concat(['title', '-', 'id'])])
    ->select($articlesTable); // Select all fields from articles

因此,在您的情况下,您需要将汽车 table 的实例传递给 select() 调用。