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
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 = ?;
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?
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.
The JPA setParameter Query method is very useful for basic entity properties that can be mapped using the default Hibernate ORM types.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With