Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createQueryBuilder and setParameter with multiple parameters?

Tags:

sql

symfony

I've been trying to create this SQL query and i keep on having an error with a 2nd parameter.

Maybe i am missing something here.

Here is my request :

$pointsTEF = $this->getDoctrine()->getManager();
                $pointsTEFs = $pointsTEF->createQueryBuilder('t')
                ->select('t.points')
                ->from('AppBundle:Tef', 't')
                ->where('t.epreuve = :epreuve')
                ->setParameter('epreuve',$valeur)
                ->andWhere('t.resultat = :resultat')
                ->setParameter('resultat',$valeurEpreuve)
                ->getQuery()
                ->getResult();

Is this the proper way of using setParameter(); ?

An exception occurred while executing 'SELECT t0_.points AS points_0 FROM tef t0_ WHERE t0_.epreuve = ? AND t0_.resultat = ?' with params ["CE", "B1"]:

SQLSTATE[HY093]: Invalid parameter number: Columns/Parameters are 1-based

like image 541
Cyberflow Avatar asked Apr 25 '16 04:04

Cyberflow


People also ask

What are query parameters in SQL?

Let's start by explaining what query parameters are. Query parameters are a way to build and execute parameterized queries. So, instead of: SELECT * FROM employees e WHERE e.emp_number = ?;

What are query parameters in JDBC?

Let's start by explaining what query parameters are. Query parameters are a way to build and execute parameterized queries. So, instead of: SELECT * FROM employees e WHERE e.emp_number = ?; By using a JDBC prepared statement, we need to set the parameter before executing the query: 3. Why Should We Use Query Parameters?

How to create a query builder using entity manager?

Let’s create a query builder using entity manager as follows − We can use repository to create query builder. It is described below, Aliases are same as SQL alias. We create alias for Student table using QueryBuilder as described below − Parameters are used as placeholders for the dynamic values in the query.

What is the JPA SetParameter query method?

The JPA setParameter Query method is very useful for basic entity properties that can be mapped using the default Hibernate ORM types.


1 Answers

Do this

->setParameters(array('param1'=> $param1, 'param2' => $param2))

Documentation
https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/query-builder.html#binding-parameters-to-your-query

like image 52
Letsrocks Avatar answered Sep 20 '22 16:09

Letsrocks