如何在doctrine Zend中构建此查询

时间:2011-05-13 15:31:19

标签: zend-framework doctrine

我有所有模特。我需要在学说中定义关系并使用doctrine构建查询。

没有学说的查询工作正常。

SELECT * FROM asset_site_link l
join assets a on l.assetID = a.id
join assetTypes t on t.id = a.assetTypeID
join assetCategories c on c.id = a.categoryID
where t.name="image" AND c.name = "static_banner"
and l.siteID = "2"

我的第一次尝试是这样的,但是没有用。

 $q = Doctrine_Query::create()
    ->select('r.*')
    ->from('assetManagement_Model_asset r')
    ->leftJoin('r.assetTypeID t')
    ->leftJoin('r.categoryID c')  
    ->leftJoin('r.assetSiteLink l')                        
    ->where('r.isDeleted = 0')
    ->andWhere('t.name = ?', "image")
    ->andWhere('c.name = ?', "static_banner")
    ->andWhere ('l.siteID = ?', "2");

虽然下面的查询工作正常(没有assetSiteLink加入)

 $q = Doctrine_Query::create()
    ->select('r.*')
    ->from('assetManagement_Model_asset r')
    ->leftJoin('r.assetTypeID t')
    ->leftJoin('r.categoryID c')  
    ->where('r.isDeleted = 0')
    ->andWhere('t.name = ?', "image")
    ->andWhere('c.name = ?', "static_banner");

告诉您Asset资产模型与AssetSiteLink有一对多关系

任何想法?

2 个答案:

答案 0 :(得分:1)

   $q = Doctrine_Query::create()
        ->select('r.*, l.*')
        ->from('linkManagement_Model_assetSiteLink l')
        ->leftJoin('l.assetSiteLink r')                        
        ->leftJoin('r.assetTypeID t')
        ->leftJoin('r.categoryID c')              
        ->where('r.isDeleted = 0')
        ->andWhere('t.name = ?', "image")
        ->andWhere('c.name = ?', "static_banner")
        ->andWhere('l.siteID = ?', "2")
   ;

答案 1 :(得分:1)

如果有人想知道为什么上面的代码不会为它们执行,可能是因为使用了“andWhere()”函数。在我的Zend Framework版本中,没有“andWhere()”函数;你只需要调用where()两次,而不是。

这意味着,对我来说,上面的代码需要像这样编写:

q = Doctrine_Query::create()
        ->select('r.*, l.*')
        ->from('linkManagement_Model_assetSiteLink l')
        ->leftJoin('l.assetSiteLink r')                        
        ->leftJoin('r.assetTypeID t')
        ->leftJoin('r.categoryID c')              
        ->where('r.isDeleted = 0')
        ->where('t.name = ?', "image")
        ->where('c.name = ?', "static_banner")
        ->where('l.siteID = ?', "2")