开发者

How do I use Doctrine 2.0 ORM models to select database data?

开发者 https://www.devze.com 2023-03-14 06:27 出处:网络
I have recently started to learn using of Doctrine 2.0 framework for PHP. Now I have already managed to utilize some Doctrine things (using MySQL), insert some data using defined XML model, but now I

I have recently started to learn using of Doctrine 2.0 framework for PHP. Now I have already managed to utilize some Doctrine things (using MySQL), insert some data using defined XML model, but now I have a problem with selecting data. Everywhere, including Doctrine site, I have found that data are being selected by DQL or raw SQL. But in this way, I lose half of advantages that ORM gives. How can I select data using pure ORM, for example like I can do with Hibernate in Java?

Here is some mapping XML example from the Doctrine site I am working with:

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                    http://doctrine-project.org/开发者_如何学Pythonschemas/orm/doctrine-mapping.xsd">

      <entity name="Product" table="products">
          <id name="id" type="integer" column="product_id">
              <generator strategy="AUTO" />
          </id>

          <field name="name" column="product_name" type="string" />
      </entity>
</doctrine-mapping>


The simplest way to read data is to use EntityManager->find(), you pass in the class you want to load as the first argument (a string) and the ID of that entity as the second. I've recently started with Doctrine 2 myself, the best advice I can give you is to read all of the docs Orbling linked to above, and to use Annotations instead of XML (at least to start off with).

HTH.


In the beginning I missed that one too, but what is important to understand is that DQL doesn't understand concept of tables (read note DQL is not SQL) it is purely object-oriented query. That is you query your models, not the database directly.

Manual itself states that DQL "is very similar to the Hibernate Query Language".

0

精彩评论

暂无评论...
验证码 换一张
取 消