如果使用列,如何获取连接数据


How to get join data if using columns?

我有两个表,ProductGroup和ProductGroupTranslation,它们连接在一起。

在ProductGroup中我有id,在翻译表中我有title。

现在我想获得所有ProductGroups:

$categories = ProductGroup::find(array(
    "product_group_id IS NULL",
    "order" => "id DESC"
) );

这可以工作,我可以通过$categories->productgrouptranslation->getTitle();访问翻译

但是如果我只取出列,我如何访问连接表呢?

$categories = ProductGroup::find(array(
   "columns" => "id",
    "product_group_id IS NULL",
    "order" => "id DESC"
) );

这是您所使用的技术的限制。如果您指定要从中检索数据的列,那么您将只能访问这些列。

如果您问"我需要指定哪些列才能仅从连接结果集中获取id和标题",那么我怀疑答案是将列数组传递到columns,列名前面有表名:

$categories = ProductGroup::find(array(
   "columns" => "ProductGroup.id,ProductGroupTranslation.title",
   "product_group_id IS NULL",
   "order" => "id DESC"
) );

将上面的表名替换为实际的表名