Symfony2 using doctrine to make a database Query method instance summary, symfony2doctrine_php Tutorial

Source: Internet
Author: User

Symfony2 uses doctrine to carry on the database Query method instance summary, Symfony2doctrine


The example in this paper describes the database query method Symfony2 using doctrine. Share to everyone for your reference, 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 (' 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 >:p rice ORDER by P.price ASC ')-> ; Setparameter (' Price ', ' 19.99′ '); $products = $query->getresult ();

Note:

(1) To obtain a result can be used:

$product = $query->getsingleresult ();

Using the Getsingleresult () method you need to wrap it up with a try catch statement to ensure that only one result is returned, as in the following example:

->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, rather than writing the value directly into the query statement, to prevent SQL injection attacks, you can also set multiple parameters:

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

3, the use of doctrine query generator

$query = $repository->createquerybuilder (' P ')->where (' P.price >:p rice ')->setparameter (' Price ', ' 19.99′ )->orderby (' P.price ', ' ASC ')->getquery (); $products = $query->getresult ();

It is hoped that this article is helpful to the PHP program design based on Symfony framework.

Articles you may be interested in:

    • Symfony2 a summary of how to achieve data acquisition from a database
    • Symfony2 Creating a Page instance
    • Summary of Symfony2 's session and cookie usage
    • Symfony2 Framework Learning Notes Form usage
    • Symfony2 framework Create project and template Setup examples
    • Analysis of plugin format of Symfony2 learning notes
    • Symfony2 Study Notes System routing
    • Symfony2 study notes of the controller usage detailed
    • Symfony2 Study Notes Template usage detailed
    • An example analysis of controller usage of SYMFONY2 development
    • Symfony2 installing a third-party bundles instance
    • Symfony2 use third-party library upload to make picture upload instance detailed
    • Implementation method of Symfony2 Federated query

http://www.bkjia.com/PHPjc/1111902.html www.bkjia.com true http://www.bkjia.com/PHPjc/1111902.html techarticle Symfony2 uses doctrine to carry on the database Query method instance Summary, Symfony2doctrine This article narrates Symfony2 uses the doctrine to carry on the database query method. Share to everyone 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.