使用“use”在Symfony2中推进查找查询

时间:2013-09-05 23:50:10

标签: php symfony propel

我正在尝试使用此查询从我的数据库获取一些信息:

$broadcastsQuery = BroadcastQuery::create()
        ->limit(20);
        ->useBroadcastPartQuery(null, \Criteria::INNER_JOIN)
            ->useTopRelatedByIdTopBeginQuery(null, \Criteria::INNER_JOIN)
            ->endUse()
            ->useTopRelatedByIdTopEndQuery(null, \Criteria::RIGHT_JOIN)
            ->endUse()
        ->endUse()
        ->filterBySended(true)
        ->find();

以下是我的schema.xml文件中包含相关表的部分:

<table name="broadcast">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
    <column name="id_channel" type="integer" />
    <column name="id_media" type="integer" />
    <column name="start_at" type="timestamp" />
    <column name="title" type="varchar" size="255" />
    <column name="sended" type="boolean" />
    <unique name="broadcast_id_plurimedia">
        <unique-column name="id_plurimedia" />
    </unique>
    <foreign-key foreignTable="channel">
        <reference local="id_channel" foreign="id" />
    </foreign-key>
</table>
<table name="broadcast_part">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
    <column name="id_broadcast" type="integer" />
    <column name="id_top_begin" type="integer" />
    <column name="id_top_end" type="integer" />
    <foreign-key foreignTable="broadcast">
        <reference local="id_broadcast" foreign="id" />
    </foreign-key>
    <foreign-key foreignTable="top">
        <reference local="id_top_begin" foreign="id" />
    </foreign-key>
    <foreign-key foreignTable="top">
        <reference local="id_top_end" foreign="id" />
    </foreign-key>
</table>
<table name="top">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
    <column name="id_channel" type="integer" />
    <column name="genre" type="varchar" size="20" />
    <column name="source" type="varchar" size="20" />
    <column name="real_date" type="timestamp" />
    <column name="frame" type="tinyint" />
    <column name="title" type="varchar" size="255" />
    <column name="orphan" type="boolean" />
    <column name="type" type="varchar" size="10" />
    <foreign-key foreignTable="channel">
        <reference local="id_channel" foreign="id" />
    </foreign-key>
</table>

当我尝试执行此查询时,我收到此错误消息:

...Syntax error or access violation: 1066 Not unique table/alias: "top"...

我知道我可以在第二次使用“top”表时使用别名(useTopRelatedByIdTopEndQuery),但我找不到办法。

我还遇到了另一个问题,当我尝试只使用一次以验证我是否正确地从我的数据库中检索数据时,我的Broadcast对象中没有任何BroadcastPart。

这是我的收藏中的一个广播转储:

[0] => Broadcast Object
            (
                [startCopy:protected] => 
                [id:protected] => 1
                [id_channel:protected] => 328
                [start_at:protected] => 2013-09-05 09:31:00
                [title:protected] => Killo Design
                [sended:protected] => 1
                [aChannel:protected] => 
                [collBroadcastParts:protected] => 
                [collBroadcastPartsPartial:protected] => 
                [alreadyInSave:protected] => 
                [alreadyInValidation:protected] => 
                [alreadyInClearAllReferencesDeep:protected] => 
                [broadcastPartsScheduledForDeletion:protected] => 
                [validationFailures:protected] => Array
                    (
                    )

                [_new:protected] => 
                [_deleted:protected] => 
                [modifiedColumns:protected] => Array
                    (
                    )

                [virtualColumns:protected] => Array
                    (
                    )

            )

1 个答案:

答案 0 :(得分:1)

查询的别名实际上由您传递的参数null

表示

尝试以下代码

$broadcastsQuery = BroadcastQuery::create('a')
    ->limit(20);
    ->useBroadcastPartQuery('b', \Criteria::INNER_JOIN)
        ->useTopRelatedByIdTopBeginQuery(null, \Criteria::INNER_JOIN)
        ->endUse()
        ->useTopRelatedByIdTopEndQuery('c', \Criteria::RIGHT_JOIN)
        ->endUse()
    ->endUse()
    ->filterBySended(true)
    ->find();