Symfony Propel查询

时间:2011-11-11 20:53:13

标签: symfony1 propel

我想在Symfony / Propel

上进行此查询
SELECT 
folders.NAME,
COUNT(documents.NAME),
COUNT(files.idfiles),
SUM(files.size)

FROM  `folders`
LEFT JOIN `documents_has_folders` ON (documents_has_folders.folders_idfolders = folders.idfolders)
LEFT JOIN documents ON (documents_has_folders.DOCUMENTS_IDDOCUMENTS=documents.IDDOCUMENTS) 
LEFT JOIN files ON (documents.IDDOCUMENTS=files.DOCUMENTS_IDDOCUMENTS) 

GROUP BY folders.idfolders

我做这个查询

$x = FoldersQuery::create()
            ->addSelectColumn(FoldersPeer::NAME)
            ->addSelectColumn("COUNT(".DocumentsPeer::IDDOCUMENTS.")")
            ->addSelectColumn("COUNT(".FilesPeer::IDFILES.")")
            ->addSelectColumn("SUM(".FilesPeer::SIZE.")")
            ->addJoin(DocumentsHasFoldersPeer::FOLDERS_IDFOLDERS, FoldersPeer::IDFOLDERS, CRITERIA::LEFT_JOIN)
            ->addJoin(DocumentsHasFoldersPeer::DOCUMENTS_IDDOCUMENTS, DocumentsPeer::IDDOCUMENTS, CRITERIA::LEFT_JOIN)
            ->addJoin(DocumentsPeer::IDDOCUMENTS, FilesPeer::DOCUMENTS_IDDOCUMENTS, CRITERIA::LEFT_JOIN)
            ->addGroupByColumn(FoldersPeer::IDFOLDERS)
            ->find();

返回:

500 | Internal Server Error | PropelException
Unable to execute SELECT statement [SELECT folders.NAME, COUNT(documents.IDDOCUMENTS), COUNT(files.IDFILES), SUM(files.SIZE) FROM LEFT JOIN folders ON (documents_has_folders.FOLDERS_IDFOLDERS=folders.IDFOLDERS) LEFT JOIN documents ON (documents_has_folders.DOCUMENTS_IDDOCUMENTS=documents.IDDOCUMENTS) LEFT JOIN files ON (documents.IDDOCUMENTS=files.DOCUMENTS_IDDOCUMENTS) WHERE folders.REMOVE_DATE IS NULL GROUP BY folders.IDFOLDERS] [wrapped: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN folders ON (documents_has_folders.FOLDERS_IDFOLDERS=folders.IDFOLDERS)' at line 1]

为什么FROM上没有表???为什么会创建错误的查询?

非常感谢。

编辑:

同样的问题,如果我这样做的查询:

$c = new Criteria();
    $c->addSelectColumn(FoldersPeer::NAME);
    $c->addSelectColumn("COUNT(".DocumentsPeer::IDDOCUMENTS.")");
    $c->addSelectColumn("COUNT(".FilesPeer::IDFILES.")");
    $c->addSelectColumn("SUM(".FilesPeer::SIZE.")");

    $c->addJoin(DocumentsHasFoldersPeer::FOLDERS_IDFOLDERS, FoldersPeer::IDFOLDERS, CRITERIA::LEFT_JOIN);
    $c->addJoin(DocumentsHasFoldersPeer::DOCUMENTS_IDDOCUMENTS, DocumentsPeer::IDDOCUMENTS, CRITERIA::LEFT_JOIN);
    $c->addJoin(DocumentsPeer::IDDOCUMENTS, FilesPeer::DOCUMENTS_IDDOCUMENTS, CRITERIA::LEFT_JOIN);

    $c->addGroupByColumn(FoldersPeer::IDFOLDERS);

在FROM之后再没有表......

Unable to execute SELECT statement [SELECT folders.NAME, COUNT(documents.IDDOCUMENTS), COUNT(files.IDFILES), SUM(files.SIZE) FROM LEFT JOIN folders ON (documents_has_folders.FOLDERS_IDFOLDERS=folders.IDFOLDERS AND folders.remove_date IS NULL ) LEFT JOIN documents ON (documents_has_folders.DOCUMENTS_IDDOCUMENTS=documents.IDDOCUMENTS AND documents.remove_date IS NULL ) LEFT JOIN files ON (documents.IDDOCUMENTS=files.DOCUMENTS_IDDOCUMENTS AND documents.remove_date IS NULL AND files.remove_date IS NULL ) WHERE folders.remove_date IS NULL AND documents.remove_date IS NULL AND files.remove_date IS NULL GROUP BY folders.IDFOLDERS] [wrapped: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN folders ON (documents_has_folders.FOLDERS_IDFOLDERS=folders.IDFOLDERS ' at line 1]

1 个答案:

答案 0 :(得分:6)

这不是Propel 1.6(也许之前)构建查询的Propel方法。我们不使用Criteria或Peer类。以下查询:

SELECT 
folders.NAME,
COUNT(documents.NAME),
COUNT(files.idfiles),
SUM(files.size)

FROM  `folders`
LEFT JOIN `documents_has_folders` ON (documents_has_folders.folders_idfolders = folders.idfolders)
LEFT JOIN documents ON (documents_has_folders.DOCUMENTS_IDDOCUMENTS=documents.IDDOCUMENTS) 
LEFT JOIN files ON (documents.IDDOCUMENTS=files.DOCUMENTS_IDDOCUMENTS) 

可以这样写:

$query = FoldersQuery::create())
    ->joinDocuments('documents')
    ->joinFiles('files')
    ->withColumn('COUNT(documents.NAME)', 'CountName')
    ->withColumn('COUNT(files.IDFILES)', 'CountIdFiles')
    ->withColumn('SUM(files.size)', 'Sum')
    ->select(array('Name', 'CountName', 'CountIdFiles', 'Sum'))
    ;

可能需要调整,但这是正确的方法。