This looks exactly like something I would have written in my pre-laracasts days.
Converting SQL Queries Strategy
Hey everyone!
I've got a very challenging project on my plate that I need some advice on.
I've been tasked to create a full fledged API for OpenCart. They have the beginnings of one in the latest version but it doesn't do much, so my client needs a full version built.
Obviously my product of choice is Lumen, so I've done some initial set up and I've begun the building of my Repositories.
What I'd like to ask is for some advice on a manageable strategy on converting the OpenCart (so called) models, which are nothing more than plain PHP classes and methods of SQL queries, accessed via a DB object.
I'm using Repositories to try and make the process a bit easier so I can use the existing method signatures. In other words I can't change the Controller code that calls the models.
So, now for some code.
This is a small snippet from a very large method, I'm not including the whole thing so as to save space here. This is in catalog/model/catalog/product.php, the getProducts method.
public function getProducts($data = array()) {
$sql = "
SELECT
p.product_id,
(SELECT AVG(rating) AS total
FROM " . DB_PREFIX . "review r1
WHERE r1.product_id = p.product_id
AND r1.status = '1'
GROUP BY r1.product_id) AS rating,
(SELECT price
FROM " . DB_PREFIX . "product_discount pd2
WHERE pd2.product_id = p.product_id
AND pd2.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "'
AND pd2.quantity = '1'
AND (
(pd2.date_start = '0000-00-00' OR pd2.date_start < NOW())
AND
(pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())
)
ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount,
(SELECT price
FROM " . DB_PREFIX . "product_special ps
WHERE ps.product_id = p.product_id
AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "'
AND (
(ps.date_start = '0000-00-00' OR ps.date_start < NOW())
AND
(ps.date_end = '0000-00-00' OR ps.date_end > NOW()))
ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special";
if (!empty($data['filter_category_id'])) {
if (!empty($data['filter_sub_category'])) {
$sql .= " FROM " . DB_PREFIX . "category_path cp
LEFT JOIN " . DB_PREFIX . "product_to_category p2c
ON (cp.category_id = p2c.category_id)";
} else {
$sql .= " FROM " . DB_PREFIX . "product_to_category p2c";
}
if (!empty($data['filter_filter'])) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf
ON (p2c.product_id = pf.product_id)
LEFT JOIN " . DB_PREFIX . "product p
ON (pf.product_id = p.product_id)";
} else {
$sql .= " LEFT JOIN " . DB_PREFIX . "product p
ON (p2c.product_id = p.product_id)";
}
} else {
$sql .= " FROM " . DB_PREFIX . "product p";
}
....
....
$product_data = array();
$query = $this->db->query($sql);
foreach ($query->rows as $result) {
$product_data[$result['product_id']] = $this->getProduct($result['product_id']);
}
return $product_data;
}
This is the mess I'm dealing with.
This method simply creates a collection of product_ids based on a given filter array and then executes getProduct on each. Simple enough, though the code is spaghetti.
Anyone willing to help me develop a strategy for converting all these into Eloquent will having my undying admiration and appreciation.
Thanks.
V
Please or to participate in this conversation.