简体   繁体   English

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

[英]How to get join data if using columns?

I have two tables, ProductGroup and ProductGroupTranslation which are joined together. 我有两个表,ProductGroup和ProductGroupTranslation结合在一起。

In ProductGroup I have id and in translation table I have title. 在ProductGroup中,我有ID;在翻译表中,我有标题。

Now I want to get all ProductGroups: 现在,我想获取所有产品组:

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

This works and I can access translation via $categories->productgrouptranslation->getTitle(); 这有效,我可以通过$categories->productgrouptranslation->getTitle();访问翻译$categories->productgrouptranslation->getTitle();

But how do I access joined table if I fetch only columns out: 但是,如果我只取出列,如何访问联接表:

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

This is a limitation of the technology you are using. 这是您使用的技术的限制。 If you specify the columns you want to retrieve data from, then those are the only columns you are going to have access to. 如果您指定要从中检索数据的列,那么这些是您将只能访问的列。

If you are asking "what columns do I need to specify to only get the id and title out of a joined result set", then I suspect the answer is to pass an array of columns into columns with the column name preceded with the table name: 如果你问“哪些列,我需要指定只获取ID和标题了加入结果集”,然后我怀疑答案是列的一个阵列通入columns与前面加上表名的列名:

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

Replace the above table names with the actual table names. 将上面的表名替换为实际的表名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM