Home >Backend Development >PHP Tutorial >Symfony2 uses Doctrine for database query method example summary, symfony2doctrine_PHP tutorial

Symfony2 uses Doctrine for database query method example summary, symfony2doctrine_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:56:29901browse

A summary of examples of how Symfony2 uses Doctrine for database query, symfony2doctrine

This article describes how Symfony2 uses Doctrine for database query. Share it with everyone for your reference, the details are as follows:

Predefined variables used in the text:

$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('AcmeStoreBundle:Product')

1. Basic method

$repository->find($id);
$repository->findAll();
$repository->findOneByName('Foo');
$repository->findAllOrderedByName();
$repository->findOneBy(array('name' => 'foo', 'price' => 19.99));
$repository->findBy(array('name' => 'foo'),array('price' => 'ASC'));

2. DQL

$query = $em->createQuery(
'SELECT p FROM AcmeStoreBundle:Product p WHERE p.price > :price ORDER BY p.price ASC'
)->setParameter('price', '19.99′);
$products = $query->getResult();

Note:

(1) To obtain a result, use:

$product = $query->getSingleResult();

To use the getSingleResult() method, you need to wrap it with a try catch statement to ensure that only one result is returned. The example is as follows:

->setMaxResults(1);
try {
$product = $query->getSingleResult();
} catch (\Doctrine\Orm\NoResultException $e) {
$product = null;
}

(2) setParameter('price', '19.99'); Use this external method to set the value of the "placeholder" price in the query statement, instead of directly writing the value into the query statement, which is helpful to prevent SQL injection attack, you can also set multiple parameters:

->setParameters(array(
'price' => '19.99′,
'name' => 'Foo',
))

3. Use Doctrine’s query builder

$query = $repository->createQueryBuilder('p')
->where('p.price > :price')
->setParameter('price', '19.99′)
->orderBy('p.price', 'ASC')
->getQuery();
$products = $query->getResult();

I hope this article will be helpful to everyone’s PHP program design based on the Symfony framework.

Articles you may be interested in:

  • Summary of the method of obtaining data from the database in Symfony2
  • Detailed explanation of creating page instances in Symfony2
  • Symfony2 session and cookie Usage summary
  • Detailed explanation of form usage of Symfony2 framework study notes
  • Detailed explanation of Symfony2 framework project creation and template setting examples
  • Plug-in format analysis of Symfony2 study notes
  • Detailed explanation of system routing in Symfony2 study notes
  • Detailed explanation of controller usage in Symfony2 study notes
  • Detailed explanation of template usage in Symfony2 study notes
  • Example analysis of controller usage in Symfony2 development
  • Detailed explanation of installing third-party Bundles instances in Symfony2
  • Detailed explanation of Symfony2 using third-party library Upload to create image upload instances
  • Symfony2 joint query implementation method

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1111902.htmlTechArticleSymfony2 uses Doctrine to perform database query method examples summary, symfony2doctrine This article describes the example of Symfony2 using Doctrine to perform database query method. Share it with everyone for your reference...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn