Prestashop database query builder enchancement
Prestashop (right now latest version is 1.5.3) has some sort of ORM, but it loads a hell lot of things, and is usable only for one object mapping. In case you want to retrieve (only) something from database using conditionals and joins, or simply getting more than one row, you have 2 options:
- Write your query using raw SQL
- Use DbQuery instance
Here is an example of query using DbQuery:
$query = new DbQuery();
$query
->select('*')
->from('state')
->where('`active` = 1')
->orderBy('`name` ASC')
;
$states = Db::getInstance()->executeS($query);
But it is annoing to instantiate each time new DbQuery, so by adding these simple lines to ovveride/classes/db/DbQuery.php
class DbQuery extends DbQueryCore
{
public static function query(){
return new self();
}
}
we’ll be able to write the same query a bit faster:
$states = Db::getInstance()->executeS(
DbQuery::query()
->select('*')
->from('state')
->where('`active` = 1')
->orderBy('`name` ASC')
);