如何访问Propel生成的CrossRef表中的非主键值?

时间:2016-06-12 08:32:05

标签: php xml schema propel cross-reference

基本上,我有一个N:M关系(在“计划”和“订阅”表之间)。计划和主题主键是这种N:M关系的主键,这种N:M关系也具有“价格”属性。 由于我需要更新此表的price属性,我无法弄清楚如何使用Propel生成的类访问它。 我可以看到,Plan和Subscription类似乎没有任何方法可以让我做我需要的。

这是我的schema.xml的一部分,其中声明了这些树关系(计划,订阅及其CrossRef)

<table name="subscriptions" phpName="Subscription">
    <column name="id_subscription" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
    <column name="name" type="varchar" required="true"/>
    <column name="description" type="longvarchar" required="true"/>
</table>

<table name="planes_subscriptions" isCrossRef="true">
    <column name="id_plan" type="integer" primaryKey="true"/>
    <column name="id_subscription" type="integer" primaryKey="true"/>
    <column name="price" type="real"/>

    <foreign-key foreignTable="planes" onDelete="CASCADE" onUpdate="CASCADE">
        <reference local="id_plan" foreign="id_plan"/>
    </foreign-key>
    <foreign-key foreignTable="subscriptions" onDelete="CASCADE" onUpdate="CASCADE">
        <reference local="id_subscription" foreign="id_subscription"/>
    </foreign-key>
</table>

<table name="planes" phpName="Plan">
    <column name="id_plan" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
    <column name="name" type="varchar" required="true"/>
    <column name="description" type="longvarchar" required="false"/>
    <column name="price" type="real" required="true"/>
</table>

我在考虑我可能需要在Propel生成的类上编写一些代码才能执行此操作,但我无法弄清楚如何通过其主键访问此表的一行。

我研究过Propel的文档,但他们只是不处理这个特例:

Blog: Many-to-many Relationships: Check!

Basic Relationships

1 个答案:

答案 0 :(得分:0)

这只是一个良好睡眠的夜晚(实际上是黎明 - 早晨)的问题,并重新研究。

可以使用Propel生成的方法更新对象。我只需要通过其原色键

过滤我的ObjectQuery(PlansSubscriptionsQuery)
// Create planSuscription query
$planSubscription = PlanesSubscriptionsQuery::create()->
// filter the query by plan and subscription ids
filterByIdPlan($idPlan)->filterByIdSubscription($idSuscripcion)->
// Magic comes here: an associative array with the value to update
update(array('Price' => $newSubscriptionPrice));
// Just checking amount of affected rows
echo $planSuscripcion;

重要事项:请注意,在关联数组中,列值以大写字母开头( P rice)。到目前为止,我可以看到,这是因为内部Propel以这种方式工作。如果您的专栏名称由两个或多个单词组成,则每个首字母单词都是capilatized:例如 d P 大米或 M y S uper A wesome C olumn的ñ AME

I found a question here that made me realize of this,我的schema.xml文件没问题,但是我试图访问在schema.xml文件中设置了下划线名称的列(price,id_plan,id_subscription等),这是不正确的

我希望这有助于某人。