Yii How to optimize a sum() query - yii

I want to skip the foreach loop;
How can I get the sum by using a yii query?
$sql = 'select size_kb from comp_arch_stats where company_id = ' . ($model->company_id) . ' and arch_month = ' . $month . ' and arch_year = ' . $year . ';';
$val = Yii::app()->db->createCommand($sql)->queryAll();
$sum = 0;
foreach ($val AS $result) {
$sum += $result['size_kb'];
}

"'select sum(size_kb) as size_kb from comp_arch_stats where company_id = ' . ($model->company_id)"

Try this
$criteria=new CDbCriteria;
$criteria->select = 'sum(size_kb) AS KbCount';
$criteria->condition ="company_id = :company_id AND arch_month =:arch_month AND arch_year=:arch_year "
$criteria->params = array (
':company_id '=> $model->company_id,
':arch_month' => $month,
':arch_year' => $year,
);
comp_arch_stats::model()->findAll($criteria);

Related

Prestashop Search only display exactly

I am new to Prestahop 1.6 and I want to change the behaviour of the search form
Currently searching '2/05' will return '22/05', '20/5', '2/051'
Desired: '2/05' will return '2/05' ONLY if exists.
You need to overrride the find method of search class in override/classes/Search.php , here what i use on my shop :
class Search extends SearchCore {
public static function find($id_lang, $expr, $page_number = 1, $page_size = 1, $order_by = 'position', $order_way = 'desc', $ajax = false, $use_cookie = true, Context $context = null) {
if (! $context) {
$context = Context::getContext();
}
$db = Db::getInstance(_PS_USE_SQL_SLAVE_);
// TODO : smart page management
if ($page_number < 1) {
$page_number = 1;
}
if ($page_size < 1) {
$page_size = 1;
}
if (! Validate::isOrderBy($order_by) || ! Validate::isOrderWay($order_way)) {
return false;
}
$intersect_array = array();
$score_array = array();
$words = Search::extractKeyWords($expr, $id_lang, false, $context->language->iso_code);
foreach ($words as $key => $word) {
if (! empty($word) && strlen($word) >= (int) Configuration::get('PS_SEARCH_MINWORDLEN')) {
// $sql_param_search = self::getSearchParamFromWord($word);
$word = str_replace(array(
'%',
'_'
), array(
'\\%',
'\\_'
), $word);
$start_search = Configuration::get('PS_SEARCH_START') ? '%' : '';
$end_search = Configuration::get('PS_SEARCH_END') ? '' : '%';
$sql_param_search = ($word[0] == '-' ? ' \'' . $start_search . pSQL(Tools::substr($word, 1, PS_SEARCH_MAX_WORD_LENGTH)) . $end_search . '\'' : ' \'' . $start_search . pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)) . $end_search . '\'');
$intersect_array[] = 'SELECT DISTINCT si.id_product
FROM ' . _DB_PREFIX_ . 'search_word sw
LEFT JOIN ' . _DB_PREFIX_ . 'search_index si ON sw.id_word = si.id_word
WHERE sw.id_lang = ' . (int) $id_lang . '
AND sw.id_shop = ' . $context->shop->id . '
AND sw.word LIKE ' . $sql_param_search;
if ($word[0] != '-') $score_array[] = 'sw.word LIKE \'' . $start_search . pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)) . $end_search . '\'';
} else {
unset($words[$key]);
}
}
if (! count($words)) {
return $ajax ? array() : array(
'total' => 0,
'result' => array()
);
}
$score = '';
if (is_array($score_array) && ! empty($score_array)) {
$score = ',(
SELECT SUM(weight)
FROM ' . _DB_PREFIX_ . 'search_word sw
LEFT JOIN ' . _DB_PREFIX_ . 'search_index si ON sw.id_word = si.id_word
WHERE sw.id_lang = ' . (int) $id_lang . '
AND sw.id_shop = ' . $context->shop->id . '
AND si.id_product = p.id_product
AND (' . implode(' OR ', $score_array) . ')
) position';
}
$sql_groups = '';
if (Group::isFeatureActive()) {
$groups = FrontController::getCurrentCustomerGroups();
$sql_groups = 'AND cg.`id_group` ' . (count($groups) ? 'IN (' . implode(',', $groups) . ')' : '= 1');
}
$results = $db->executeS('
SELECT DISTINCT cp.`id_product`
FROM `' . _DB_PREFIX_ . 'category_product` cp
' . (Group::isFeatureActive() ? 'INNER JOIN `' . _DB_PREFIX_ . 'category_group` cg ON cp.`id_category` = cg.`id_category`' : '') . '
INNER JOIN `' . _DB_PREFIX_ . 'category` c ON cp.`id_category` = c.`id_category`
INNER JOIN `' . _DB_PREFIX_ . 'product` p ON cp.`id_product` = p.`id_product`
' . Shop::addSqlAssociation('product', 'p', false) . '
WHERE c.`active` = 1
AND product_shop.`active` = 1
AND product_shop.`visibility` IN ("both", "search")
AND product_shop.indexed = 1
' . $sql_groups, true, false);
$eligible_products = array();
foreach ($results as $row) {
$eligible_products[] = $row['id_product'];
}
// $eligible_products2 = array();//half OR
foreach ($intersect_array as $query) {
$eligible_products2 = array(); // full AND
foreach ($db->executeS($query, true, false) as $row) {
$eligible_products2[] = $row['id_product'];
}
$eligible_products = array_unique(array_intersect($eligible_products, array_unique($eligible_products2)));
if (! count($eligible_products)) {
return $ajax ? array() : array(
'total' => 0,
'result' => array()
);
}
}
$product_pool = '';
foreach ($eligible_products as $id_product) {
if ($id_product) {
$product_pool .= (int) $id_product . ',';
}
}
if (empty($product_pool)) {
return $ajax ? array() : array(
'total' => 0,
'result' => array()
);
}
$product_pool = ((strpos($product_pool, ',') === false) ? (' = ' . (int) $product_pool . ' ') : (' IN (' . rtrim($product_pool, ',') . ') '));
if ($ajax) {
$sql = 'SELECT DISTINCT p.id_product, pl.name pname, cl.name cname,
cl.link_rewrite crewrite, pl.link_rewrite prewrite ' . $score . '
FROM ' . _DB_PREFIX_ . 'product p
INNER JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestrictionOnLang('pl') . '
)
' . Shop::addSqlAssociation('product', 'p') . '
INNER JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON (
product_shop.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestrictionOnLang('cl') . '
)
WHERE p.`id_product` ' . $product_pool . '
ORDER BY position DESC LIMIT 10';
return $db->executeS($sql, true, false);
}
if (strpos($order_by, '.') > 0) {
$order_by = explode('.', $order_by);
$order_by = pSQL($order_by[0]) . '.`' . pSQL($order_by[1]) . '`';
}
$alias = '';
if ($order_by == 'price') {
$alias = 'product_shop.';
} elseif (in_array($order_by, array(
'date_upd',
'date_add'
))) {
$alias = 'p.';
}
$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity,
pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`name`,
image_shop.`id_image` id_image, il.`legend`, m.`name` manufacturer_name ' . $score . ',
DATEDIFF(
p.`date_add`,
DATE_SUB(
"' . date('Y-m-d') . ' 00:00:00",
INTERVAL ' . (Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20) . ' DAY
)
) > 0 new' . (Combination::isFeatureActive() ? ', product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity, IFNULL(product_attribute_shop.`id_product_attribute`,0) id_product_attribute' : '') . '
FROM ' . _DB_PREFIX_ . 'product p
' . Shop::addSqlAssociation('product', 'p') . '
INNER JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestrictionOnLang('pl') . '
)
' . (Combination::isFeatureActive() ? 'LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_shop` product_attribute_shop FORCE INDEX (id_product)
ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop=' . (int) $context->shop->id . ')' : '') . '
' . Product::sqlStock('p', 0) . '
LEFT JOIN `' . _DB_PREFIX_ . 'manufacturer` m FORCE INDEX (PRIMARY)
ON m.`id_manufacturer` = p.`id_manufacturer`
LEFT JOIN `' . _DB_PREFIX_ . 'image_shop` image_shop FORCE INDEX (id_product)
ON (image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop=' . (int) $context->shop->id . ')
LEFT JOIN `' . _DB_PREFIX_ . 'image_lang` il ON (image_shop.`id_image` = il.`id_image` AND il.`id_lang` = ' . (int) $id_lang . ')
WHERE p.`id_product` ' . $product_pool . '
GROUP BY product_shop.id_product
' . ($order_by ? 'ORDER BY ' . $alias . $order_by : '') . ($order_way ? ' ' . $order_way : '') . '
LIMIT ' . (int) (($page_number - 1) * $page_size) . ',' . (int) $page_size;
$result = $db->executeS($sql, true, false);
$sql = 'SELECT COUNT(*)
FROM ' . _DB_PREFIX_ . 'product p
' . Shop::addSqlAssociation('product', 'p') . '
INNER JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestrictionOnLang('pl') . '
)
LEFT JOIN `' . _DB_PREFIX_ . 'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE p.`id_product` ' . $product_pool;
$total = $db->getValue($sql, false);
if (! $result) {
$result_properties = false;
} else {
$result_properties = Product::getProductsProperties((int) $id_lang, $result);
}
return array(
'total' => $total,
'result' => $result_properties
);
}
}

display dynamic data in multiple columns

I use an html string in php to display sql query results in a single column:
How can I use multiple columns to lay it out like this:
while($row = mysqli_fetch_array($q)){
$id = $row['id'];
$title = $row['title'];
$desc = $row['description'];
$link = $row['link'];
$output .= '<a href="' . $link . '">
<h3>' . $title . '</h3>
<p>' . $desc . '</p>
</a>';
}
Credit to hungrykoala for help on this one
$i = 0 ;
$resultsPerRow = 2 ;
$output = '<tr>';
while($row = mysqli_fetch_array($q)){
$i++;
$id = $row['id'];
$title = $row['title'];
$desc = $row['description'];
$link = $row['link'];
$output .= '<td>' .$title. '<br>' .$desc. '</td>';
if ($i % $resultsPerRow == 0) {
$output .= '</tr><td><br></td><tr>';
}
}
echo($output);
Then output to a table tag

The entry on SQL it's being recognized as column

I'm trying to use the extension RelatedContentByTags, but the entry on bolt_taxonomy column it's being recognized as column.
I have this error when I put {{ relatedcontentbytags(record) }} on index.
'Twig_Error_Runtime thrown with message
"An exception has been thrown during the rendering of a template
("An exception occurred while executing ' SELECT bolt_entries.id FROM bolt_entries LEFT JOIN
bolt_taxonomy ON bolt_entries.id = bolt_taxonomy.content_id WHERE
bolt_entries.status = "published"AND bolt_entries.id != 3 AND
bolt_taxonomy.contenttype = "entries" AND (bolt_taxonomy.taxonomytype = "tags" AND
(bolt_taxonomy.slug = "teste2"))':
SQLSTATE[42703]: Undefined column: 7 ERROR: column "published" does not exist
LINE 1: ...t_taxonomy.content_id WHERE bolt_entries.status = "published...^") in "entry.twig" at line 65."
And the extension.php is that:
$results = array();
foreach ($tables as $name) {
$table = sprintf('%s%s', $tablePrefix, $name);
$querySelect = '';
$querySelect .= sprintf(' SELECT %s.id FROM %s', $table, $table);
$querySelect .= sprintf(' LEFT JOIN %s', $taxonomyTable);
$querySelect .= sprintf(' ON %s.id = %s.content_id', $table, $taxonomyTable);
$querySelect .= sprintf(' WHERE %s.status = "published"', $table);
if ($name == $record->contenttype['slug']) {
$querySelect .= sprintf('AND %s.id != '. $record->id, $table);
}
$querySelect .= sprintf(' AND %s.contenttype = "%s"', $taxonomyTable, $name);
$querySelect .= sprintf(' AND (%s)', $queryWhere);
$queryResults = $app['db']->fetchAll( $querySelect );
if (!empty($queryResults)) {
$ids = implode(' || ', \utilphp\util::array_pluck($queryResults, 'id'));
$contents = $app['storage']->getContent($name, array('id' => $ids, 'returnsingle' => false));
$results = array_merge( $results, $contents );
}
}
Depending on what database you are using (I'm guessing Postgresql based on the error code) text enclosed in double-quotes "" can be used to denote a table name; single-quotes '' are normally used to denote character literals, so try changing "published" to 'published' (the same goes for entries, tags, and teste2 too of course).

PDO bindParam with unknown number of parameters

I would like to run an update query for every row with a specific ID:
e.g.
$ids = array(111, 112, 113);
$query = "UPDATE mytable SET columnName = 'Y' WHERE id = :id1 or id = :id2 or id = :id3";
$stmt->bindParam(':id1', $ids[0], PDO::PARAM_INT);
$stmt->bindParam(':id2', $ids[1], PDO::PARAM_INT);
$stmt->bindParam(':id3', $ids[2], PDO::PARAM_INT);
This works fine if I know there are 3 ids to update, but how would I do this if the number of id fields is variable?
Something like this would do the trick.
$ids = array(111, 112, 113);
$valueString = "";
foreach($ids as $key => $val) {
$valueString .= ":id" . $key . " = " . $val;
if (end($ids) != $val) {
$valueString .= ", ";
}
}
$query = "UPDATE mytable SET columnName = 'Y' WHERE ". $valueString;
foreach($ids as $key => $val) {
$stmt->bindParam(':id' . $key, $val, PDO::PARAM_INT);
}

Opencart sort by manufacturer in Category pages

Ok, I would like to add one more sort variable on category pages, that is to sort by Manufacturer.
Running on 1.5.3.1 and have tried this:
1. catalog/controller/product/category.php
added the following:
$this->data['sorts'][] = array(
'text' => $this->language->get('text_manufacturer_asc'),
'value' => 'manufacturer-ASC',
'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=manufacturer&order=ASC' . $url)
);
$this->data['sorts'][] = array(
'text' => $this->language->get('text_manufacturer_desc'),
'value' => 'manufacturer-DESC',
'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=manufacturer&order=DESC' . $url)
);
catalog/language/*/product/category.php
added:
$_['text_manufacturer_asc'] = '▲ -Proizvođaču- ▲';
$_['text_manufacturer_desc'] = '▼ -Proizvođaču- ▼';
catalog/model/catalog/product.php
before: 'p.date_added' I inserted a line:
'manufacturer'
and few lines below I changed the block with the following one:
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} elseif ($data['sort'] == 'p.price') {
$sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
} elseif ($data['sort'] == 'manufacturer') {
$sql .= " SELECT * FROM". DB_PREFIX . "product p ORDER BY manufacturer";
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY p.sort_order";
}
But it is not working.
Error says:
PHP Notice: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROMproduct p ORDER BY manufacturer ASC,...
and another one I have found:
PHP Notice: Error: Unknown column 'special' in 'order clause'<br />Error No: 1054<br />SELECT p.product_id, (SELECT AVG(rating) AS total FROM review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id)...
Can anybody help?
You are almost there but You will have to modify it a little bit more.
First, You really do not want to order by manufacturer ID stored in product.manufacturer_id but by manufacturer name. In this case we need to modify the query in catalog\model\catalog\product.php model in getProducts method - find this line:
$sql .= " LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
(should be line 89 in OC 1.5.5.1) and before it add:
$sql .= " LEFT JOIN " . DB_PREFIX . "manufacturer m ON p.manufacturer_id = m.manufacturer_id";
Also You'll need to allow manufacturer as a sorting option:
$sort_data = array(
'pd.name',
'p.model',
'p.quantity',
'p.price',
'rating',
'p.sort_order',
'p.date_added',
'manufacturer', // <== this is added
);
Now in the sorting section add sorting by manufacturer name:
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} elseif ($data['sort'] == 'p.price') {
$sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
} elseif ($data['sort'] == 'manufacturer') { // <== this is added
$sql .= " ORDER BY m.name"; // <== this is added
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY p.sort_order";
}
And that's it.