Questions tagged [query-builder]

A query-builder is a set of classes and methods that is able to programmatically build queries.

It provides a set of classes and methods that is able to programmatically build queries, and also provides a fluent API.

This functionality can be embedded on an or be independent.

Functionality

  • Queries more readable
  • Can be cross database

Query Builders documentation

2553 questions
18
votes
4 answers

Laravel Query Builder - sum() method issue

I'm new to laravel and I have some issues with the query builder. The query I would like to build is this one: SELECT SUM(transactions.amount) FROM transactions JOIN categories ON transactions.category_id == categories.id WHERE categories.kind ==…
burn15
  • 183
  • 1
  • 1
  • 4
17
votes
4 answers

How to order by count in Doctrine 2?

I'm trying to group my entity by a field (year) and do a count of it. Code: public function countYear() { $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('b.year, COUNT(b.id)') ->from('\My\Entity\Album', 'b') …
Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131
17
votes
2 answers

How to use countDistinct in Doctrine query builder (Symfony)

I am trying to count the distinct number of IDs returned for a query with his: $query = $repo->createQueryBuilder('prov') ->select('c.id') ->innerJoin('prov.products', 'prod') ->innerJoin('prod.customerItems', 'ci') …
matt_jay
  • 1,241
  • 1
  • 15
  • 33
16
votes
5 answers

How to do a LIKE database query in Symfony2

This should be simple but I can't find a working example. Here's a controller method that throws the error "Invalid parameter number: number of bound variables does not match number of tokens". I'm posting the "searchterm" variable successfully…
Acyra
  • 15,864
  • 15
  • 46
  • 53
16
votes
2 answers

Difference between .limit() and .take() in TypeORM

I am confused about different TypeORM methods with similar purposes. From TypeORM docs: .take() — pagination limit. Sets maximal number of entities to take. .skip() — pagination offset. Sets number of entities to skip. I poorly understand what…
Denys Rybkin
  • 637
  • 1
  • 6
  • 18
16
votes
3 answers

Symfony 3 Too many parameters

I'm new to Symfony, and I got an error while running a query : public function getFilteredArticles($page, $nbPerPage, $data) { $query = $this->createQueryBuilder('a') ->leftJoin('a.images', 'i') …
AKMMM
  • 294
  • 1
  • 2
  • 19
16
votes
3 answers

Laravel upsert operations with Query Builder

In one of the my worker scripts to store aggregate counts based on some metrics, I am not using Eloquent as the queries are a little complex and and it is easy to write using query builder. I am currently getting the values from the database and I…
Happy Coder
  • 4,255
  • 13
  • 75
  • 152
16
votes
3 answers

Why laravel model duplicates set of data and how (if possible) to have only one set of data?

It is convenient that laravel model provides a method that it can return results from another associated table. For example, I have a table called item and another table called feedback, where the feedback table stores feedback of an item in the…
cytsunny
  • 4,838
  • 15
  • 62
  • 129
16
votes
2 answers

Join subquery with doctrine 2 DBAL

I'm refactoring a Zend Framework 2 application to use doctrine 2.5 DBAL instead of Zend_DB (ZF1). I have the following Zend_Db query: $subSelect = $db->select() ->from('user_survey_status_entries', array('userSurveyID', 'timestamp' =>…
aimfeld
  • 2,931
  • 7
  • 32
  • 43
16
votes
1 answer

Laravel query builder - How to either group by alias, or do raw groupBy

My Laravel 5 app includes a dynamic query builder for report running. I need some group by clauses in there and have run into a problem. If I use actual sql in there I can have issues as sometimes there needs to be a sql command in amongst the sql…
Geoff Clayton
  • 1,123
  • 2
  • 10
  • 16
16
votes
1 answer

Laravel: how to use derived tables / subqueries in the laravel query builder

Edit: Though this question originally was specific for the query I'm describing underneath, the answer I got applies to almost all questions related to using derived tables / subqueries in Laravel Original Question: Lately I'm a bit stuck on the…
Luuk Van Dongen
  • 2,391
  • 6
  • 26
  • 40
16
votes
2 answers

How to filter by conditions for associated models?

I have a belongsToMany association on Users and Contacts. I would like to find the Contacts of the given User. I would need something like $this->Contacts->find()->contain(['Users' => ['Users.id' => 1]]); The cookbook speaks about giving conditions…
rrd
  • 1,441
  • 2
  • 15
  • 36
16
votes
2 answers

Symfony form query_buider and entity repository

I'm trying to create a form with data in collection type depending on the user being logged. I'm following this chapter of the Symfony cookbook. Everything works fine when the query_builder option is a closure where I get my data from DQL. As the…
Florent
  • 806
  • 2
  • 8
  • 14
15
votes
2 answers

Laravel query builder returns object or array?

I'm building a very simple web app with Laravel. I've built two separate Controllers, which each return two separate views, as follows: ProfileController: class ProfileController extends BaseController { public function user($name) { …
Tiago
  • 4,233
  • 13
  • 46
  • 70
14
votes
3 answers

Laravel - Order by as Number (int), even if column type is string

here it is:- $query = Section::orderBy("section", "desc")->get(); section here is a column with type string yet it is having numbers in it where i want to order by those numbers thanks for your response
Kingsley Akindele
  • 311
  • 1
  • 2
  • 13