开发者

Can't create Doctrine Query - many addFroms

开发者 https://www.devze.com 2023-01-25 18:28 出处:网络
I\'m having trouble converting a SQL query to Doctrine. I have the following setup: path (hierarchy) Id | template_type | object_id | name | path | rgt | lft | level

I'm having trouble converting a SQL query to Doctrine.

I have the following setup:

path (hierarchy)

Id | template_type | object_id | name | path | rgt | lft | level

property

Id | other | columns

development

Id | other | columns

I'm trying to select Paths with the template type "Property" (these always have a record of 'object_id' in the property table), and I'm trying to join the parent path (which will always have a record in the development table).

the following raw SQL works perfectly:

SELECT
    node.*,
    parent.*,
    development.*,
    property.*
FROM
    path AS node,
    path AS parent,
    development,
    property
WHERE
    node.lft
BETWEEN
    parent.lft AND  parent.rgt
AND
    node.template_type =  'Property'
AND
    parent.level = node.level - 1
AND
    development.id = parent.object_id
AND
    property.id = node.object_id

When trying to convert it to Doctrine I seem to be having trouble.

$q = Doctrine_Query::create()

    ->select("node.*")
    ->addSelect("parent.*")
    ->addSelect("development.*")
    ->addSelect("property.*")

    ->from("Path node")
    ->addFrom("Path parent")
    ->addFrom("Development development")
    ->addFrom("Property property")

    ->where("node.lft BETWEEN parent.lft AND parent.rgt")
    ->addWhere("node.template_type = 'Property'")
    ->addWhere("parent.level = node.level - 1")
    ->addWhere("development.id = parent.object_id")
    ->addWhere("property.id = node.object_id");

The query it's generating is this:

SELECT p.id                          AS p__id, 
       p.template_type               AS p__template_type, 
       p.object_id                   AS p__object_id, 
       p.created_at                  AS p__created_at, 
       p.updated_at                  AS p__updated_at, 
       p.meta_page_title             AS p__meta_page_title, 
       p.meta_navigation_title       AS p__meta_navigation_title, 
       p.meta_path                   AS p__meta_path, 
       p.meta_keywords               AS p__meta_keywords, 
       p.meta_description            AS p__meta_description, 
       p.meta_visible_in_navigation  AS p__meta_visible_in_navigation, 
       p.root_id                     AS p__root_id, 
       p.lft                         AS p__lft, 
       p.rgt                         AS p__rgt, 
       p.level                       AS p__level, 
       p2.id                         AS p2__id, 
       p2.template_type              AS p2__template_type, 
       p2.object_id                  AS p2__object_id, 
       p2.created_at                 AS p2__created_at, 
       p2.updated_at                 AS p2__updated_at, 
       p2.meta_page_title            AS p2__meta_page_title, 
       p2.meta_navigation_title      AS p2__meta_navigation_title, 
       p2.meta_path                  AS p2__meta_path, 
       p2.meta_keywords              AS p2__meta_keywords, 
       p2.meta_description           AS p2__meta_description, 
       p2.meta_visible_in_navigation AS p2__meta_visible_in_navigation, 
       p2.root_id                    AS p2__root_id, 
       p2.lft                        AS p2__lft, 
       p2.rgt                        AS p2__rgt, 
       p2.level                      AS p2__level, 
       d.id                          AS d__id, 
       d.name                        AS d__name, 
       d.latitude                    AS d__latitude, 
       d.longitude                   AS d__longitude, 
       d.introduction                AS d__introduction, 
       d.description                 AS d__description, 
       d.thumbnail                   AS d__thumbnail, 
       d.path_id                     AS d__path开发者_StackOverflow_id, 
       p3.id                         AS p3__id, 
       p3.price                      AS p3__price, 
       p3.number_of_bedrooms         AS p3__number_of_bedrooms, 
       p3.key_features               AS p3__key_features, 
       p3.description                AS p3__description, 
       p3.thumbnail                  AS p3__thumbnail, 
       p3.property_type_id           AS p3__property_type_id, 
       p3.path_id                    AS p3__path_id 
FROM   path p, 
       path p2, 
       development d, 
       property p3 
WHERE  ( p.lft BETWEEN p2.lft AND p2.rgt 
         AND p.template_type = 'Property' 
         AND p2.level = node.level - 1 
         AND d.id = p2.object_id 
         AND p3.id = p.object_id ) 

Which is resulting in this error:

1054 - Unknown column 'node.level' in 'where clause'

any ideas? I can see that it should be saying "AND p2.level = p.level - 1", but it doesnt seem to be converting node.

Any ideas?


When you start to get complicated queries, sometimes you don't want to rely on doctrine to generate your sql code. Not to mention the pain you are going through trying to convert an already working query to satisfy Doctrine.

In these cases, I would usually recommend running the queries as SQL instead of passing it through the doctrine query builder.

Take a look at Doctrine_RawSql()

0

精彩评论

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

关注公众号