Symfony2 uses Doctrine to summarize database query method instances. symfony2doctrine_PHP tutorial

Source: Internet
Author: User
Symfony2 summarizes database query method instances using Doctrine, symfony2doctrine. Symfony2 summarizes database query method instances using Doctrine. symfony2doctrine this article describes the database query method using Doctrine in Symfony2. Share this with you for your reference Symfony2 database query method instance summary using Doctrine, symfony2doctrine

This example describes how to use Doctrine to query databases in Symfony2. We will share this with you for your reference. The details are as follows:

Variables used in the predefined text:

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

1. Basic methods

$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, you can 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 '). This external method is used to set the "placeholder" price value in the query statement, instead of directly writing the value into the query statement, which helps prevent SQL injection attacks, you can also set multiple parameters:

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

3. query builder using Doctrine

$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 help you design PHP programs based on the Symfony framework.

Articles you may be interested in:
  • Summary of how Symfony2 can obtain data from the database
  • Symfony2 create page instance details
  • Session and cookie usage in Symfony2
  • Symfony2 framework learning Notes
  • Details about how to create a project and a template setting instance in the Symfony2 framework
  • Symfony2 study notes plugin format analysis
  • System route details for Symfony2 study notes
  • A detailed explanation of the controller usage of Symfony2 learning Notes
  • Template usage of Symfony2 learning Notes
  • Symfony2 development-Controller usage instance analysis
  • How to install a third-party Bundles instance in Symfony2
  • How to use a third-party library Upload to create an image Upload instance in Symfony2
  • Symfony2 joint query implementation method

The example in this article describes how to use Doctrine to query databases in Symfony2. Share it with you for your reference...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.