Propel上的非模型连接

时间:2014-02-25 16:55:31

标签: php propel

如何在Propel上写这个?架构:

obj
--
id
object_id

foo
--
id

SQL:

SELECT f.*
FROM
    foo f INNER JOIN obj o ON f.id=o.object_id
ORDER BY
    o.id
;

PHP:

$join = new \Join();
$join->addExplicitCondition(
    'foo', 'id', 'f',
    'obj', 'object_id', 'o',
    \Join::EQUAL
);

$join->setJoinType(\Criteria::INNER_JOIN);

$objectsQuery = FooQuery::create()
    ->setModelAlias('f', true)
    ->addJoinObject($join, 'o')
    ->addAscendingOrderByColumn('o.id') // error here
    ;

输出:

Cannot fetch TableMap for undefined table: o

我正在使用Propel 1.7。请注意,我没有“obj”和“foo”之间的模型关系(它应该是多态关系)。而且我宁愿避免编写自定义SQL。谢谢。

1 个答案:

答案 0 :(得分:0)

结果添加JoinObject没有添加适当的别名,所以我必须自己添加它们。这有效:

$objectsQuery = FooQuery::create()
   ->setModelAlias('f', true)
   ->addJoinObject($join, 'o') // 'o' here makes no difference, actually
   ->addAlias($join->getRightTableAlias(), $join->getRightTableName())
   ->addAscendingOrderByColumn('o.id');