-
Notifications
You must be signed in to change notification settings - Fork 25
How to use Database query builder
Aleksandar Babic edited this page Sep 15, 2013
·
3 revisions
Here is example of Query builder usage in application's service:
class PostFactory
{
/**
* @var \database\Query
*/
private $query;
function __construct(\database\DB $db)
{
$this->query = $db->createQuery();
$this->query->select("p,*")
->from("posts p")
->join("INNER JOIN users u ON u.user_id = p.user_id");
}
function getPosts($args = array())
{
$args = array_merge(array(
"posts_per_page" => 10,
"is_active" => 1
), $args);
$this->query->limit($args["posts_per_page"]);
$this->query->where("p.is_active = ?", $args["is_active"]);
return $this->query->execute()->fetchCollection();
}
function getPostsByUserId($user_id, $args = array())
{
$this->query->where("p.user_id = ?", $user_id);
return $this->getPosts($args);
}
}