I am trying to do a full outer join of several tables in Drupal 8. MySQL doesn't support full outer join so I'm using a left join union right join.
It gets most of the data but in the materials that I am querying for, I know there are multiple materials but I am only getting one. I can't see what I am doing wrong.
I am directly accessing the db and not using helper functions because this is actually for a mobile app I am writing, that queries the database from phones or tablets:
$query_phrase = "select
commerce_product_field_data.title as title,
commerce_product_field_data.product_id as product_id,
commerce_product__8fd244ef14.field_has_multiple_configuration_value as has_multiple_configuration,
commerce_product__field_barcode.field_barcode_value as barcode,
commerce_product__field_price.field_price_number as price,
commerce_product__field_price.field_price_currency_code as currency_code,
commerce_product__field_sku.field_sku_value as sku,
node_field_data.title as material_name,
file_managed.uri as uri,
file_managed.filename as filename
from {commerce_product_field_data}
left join commerce_product__8fd244ef14 on product_id = commerce_product__8fd244ef14.entity_id
left join commerce_product__field_barcode on product_id = commerce_product__field_barcode.entity_id
left join commerce_product__field_price on product_id = commerce_product__field_price.entity_id
left join commerce_product__field_sku on product_id = commerce_product__field_sku.entity_id
/*not working*/ left join commerce_product__field_materials on product_id = commerce_product__field_materials.entity_id
/*not working*/ left join node_field_data on commerce_product__field_materials.field_materials_target_id = node_field_data.nid
left join file_managed on product_id = file_managed.fid";
$query_phrase .= " union ";
$query_phrase .= "select
commerce_product_field_data.title as title,
commerce_product_field_data.product_id as product_id,
commerce_product__8fd244ef14.field_has_multiple_configuration_value as has_multiple_configuration,
commerce_product__field_barcode.field_barcode_value as barcode,
commerce_product__field_price.field_price_number as price,
commerce_product__field_price.field_price_currency_code as currency_code,
commerce_product__field_sku.field_sku_value as sku,
node_field_data.title as material_name,
file_managed.uri as uri,
file_managed.filename as filename
from {commerce_product_field_data}
right join commerce_product__8fd244ef14 on product_id = commerce_product__8fd244ef14.entity_id
right join commerce_product__field_barcode on product_id = commerce_product__field_barcode.entity_id
right join commerce_product__field_price on product_id = commerce_product__field_price.entity_id
right join commerce_product__field_sku on product_id = commerce_product__field_sku.entity_id
/*not working*/ right join commerce_product__field_materials on product_id = commerce_product__field_materials.entity_id
/*not working*/ right join node_field_data on commerce_product__field_materials.field_materials_target_id = node_field_data.nid
right join file_managed on product_id = file_managed.fid";
$query_phrase .= " where commerce_product__field_barcode.field_barcode_value = :barcode";
$query = db_query($query_phrase, array(":barcode" => "334242"));
When I debug my results I get:
Array
(
[title] => Dummy Product
[product_id] => 2
[has_multiple_configuration] => 0
[barcode] => 334242
[price] => 325.000000
[currency_code] => USD
[sku] => dummy sku
[material_name] => test material
[uri] => public:a
But I know I have more than one material.
You're missing the where
clause in the left join
part of the query.
$query_phrase = "select
commerce_product_field_data.title as title,
commerce_product_field_data.product_id as product_id,
commerce_product__8fd244ef14.field_has_multiple_configuration_value as has_multiple_configuration,
commerce_product__field_barcode.field_barcode_value as barcode,
commerce_product__field_price.field_price_number as price,
commerce_product__field_price.field_price_currency_code as currency_code,
commerce_product__field_sku.field_sku_value as sku,
node_field_data.title as material_name,
file_managed.uri as uri,
file_managed.filename as filename
from {commerce_product_field_data}
left join commerce_product__8fd244ef14 on product_id = commerce_product__8fd244ef14.entity_id
left join commerce_product__field_barcode on product_id = commerce_product__field_barcode.entity_id
left join commerce_product__field_price on product_id = commerce_product__field_price.entity_id
left join commerce_product__field_sku on product_id = commerce_product__field_sku.entity_id
left join commerce_product__field_materials on product_id = commerce_product__field_materials.entity_id
left join node_field_data on commerce_product__field_materials.field_materials_target_id = node_field_data.nid
left join file_managed on product_id = file_managed.fid";
$query_phrase .= " where commerce_product__field_barcode.field_barcode_value = :barcode";
$query_phrase .= " union ";
$query_phrase .= "select
commerce_product_field_data.title as title,
commerce_product_field_data.product_id as product_id,
commerce_product__8fd244ef14.field_has_multiple_configuration_value as has_multiple_configuration,
commerce_product__field_barcode.field_barcode_value as barcode,
commerce_product__field_price.field_price_number as price,
commerce_product__field_price.field_price_currency_code as currency_code,
commerce_product__field_sku.field_sku_value as sku,
node_field_data.title as material_name,
file_managed.uri as uri,
file_managed.filename as filename
from {commerce_product_field_data}
right join commerce_product__8fd244ef14 on product_id = commerce_product__8fd244ef14.entity_id
right join commerce_product__field_barcode on product_id = commerce_product__field_barcode.entity_id
right join commerce_product__field_price on product_id = commerce_product__field_price.entity_id
right join commerce_product__field_sku on product_id = commerce_product__field_sku.entity_id
right join commerce_product__field_materials on product_id = commerce_product__field_materials.entity_id
right join node_field_data on commerce_product__field_materials.field_materials_target_id = node_field_data.nid
right join file_managed on product_id = file_managed.fid";
$query_phrase .= " where commerce_product__field_barcode.field_barcode_value = :barcode";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.