Symfony Propel Query - PullRequest
       34

Symfony Propel Query

3 голосов
/ 12 ноября 2011

Я хочу сделать этот запрос на 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 Ответ

6 голосов
/ 12 ноября 2011

Это не способ Propel строить запросы начиная с Propel 1.6 (может быть, раньше).Мы не используем критерии или классы сверстников.Следующий запрос:

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'))
    ;

Может потребоваться корректировка, но это правильный путь.

...