I'm working with an OpenCart store that's based on an mcv design pattern. I need have a model function that returns an array but I need to make a call within that array to another function that returns an array. This function below breaks here:
'gallery_images' => $this->getGalleryImages($data['product_id'])
public function getImportProductInfo() {
$query = $this->db->query("SELECT `product_id` FROM " . DB_PREFIX . "product WHERE `status` = '0' AND `affiliate_id` = '" . $this->affiliate->getID() . "'");
$pids = array();
foreach($query->rows as $result) {
$pids[] = $result['product_id'];
}
// product & product description
$query_product = $this->db->query("SELECT p.model,
p.product_id,
p.quantity,
p.image,
p.price,
p.weight,
p.length,
p.width,
p.height,
pd.name AS 'product_name',
pd.description AS 'product_description',
cd.name AS `category_name`,
m.name AS 'manufacturer_name'
FROM " . DB_PREFIX . "product p
LEFT JOIN " . DB_PREFIX . "product_description pd ON p.product_id = pd.product_id
LEFT JOIN " . DB_PREFIX . "product_to_category ptc ON p.product_id = ptc.product_id
LEFT JOIN " . DB_PREFIX . "category_description cd ON ptc.category_id = cd.category_id
LEFT JOIN " . DB_PREFIX . "manufacturer m ON p.manufacturer_id = m.manufacturer_id
WHERE p.product_id IN (" . $this->db->escape(implode(',',$pids)) . ")");
foreach($query_product->rows as $data) {
$product_data[] = array(
'product_id' => $data['product_id'],
'model' => $data['model'],
'quantity' => $data['quantity'],
'featured_image' => $data['image'],
'price' => $data['price'],
'weight' => $data['weight'],
'length' => $data['length'],
'width' => $data['width'],
'height' => $data['height'],
'product_name' => $data['product_name'],
'description' => $data['product_description'],
'category_name' => $data['category_name'],
'manufacturer_name' => $data['manufacturer_name'],
'gallery_images' => array($this->getGalleryImages($data['product_id']))
);
}
return $product_data;
}
Here is the getGalleryImages() function. Note here I have also tried return implode(', ',$images); so i dont have to build the array but it still breaks.
public function getProductGalleryImages($product_id) {
$query = $this->db->query("SELECT `image` FROM " . DB_PREFIX . "product_image WHERE `product_id` = '" . (int)$product_id . "'");
$images = array();
foreach($query->rows as $result) {
$images[] = $result['image'];
}
//return implode(', ',$images);
return $images;
}
each product has multiple image urls stored in an intersecting table and I need to add to the array and it's been fighting me all morning... Any Ideas would be greatly appreciated. Thanks in advance :)
OK here is the sql embedded in the array that worked
'gallery_images' => $this->db->query("SELECT `image` FROM " . DB_PREFIX . "product_image WHERE `product_id` = '" . (int)$data['product_id'] . "'")
BUT it returns this:
[gallery_images] => stdClass Object (
[row] => Array ( [image] => motorcycle/12_06_27/031.JPG )
[rows] => Array ( [0] => Array ( [image] => motorcycle/12_06_27/031.JPG ) )[num_rows] => 1 ) ) )
All I want is the ['image'] part.