Propel ORM-具有关系类型的多对多关系



是否可以在Propel中的关系中创建一个外部字段。主要目的是建立一种类型的关系。

例如,我们有联系人和机会。我需要Contacts和Opportunities之间具有这种关系类型的关系。

数据示例:

contact_id   | opportunity_id   | association_type
------------------------------------------------------
<contact_id> | <opportunity_id> | <Executive Sponsor>
<contact_id> | <opportunity_id> | <Business Evaluator>

是否可以在Propel中实现?

感谢

绝对可能,只需将列添加到cross_ref表中即可:

<table name="contact_opportunity" isCrossRef="true">
  <column name="contact_id" type="INTEGER" primaryKey="true"/>
  <column name="opportunity_id" type="INTEGER" primaryKey="true"/>
  <!-- your new field -->
  <column name="association_type" type="VARCHAR" required="true" />
  <foreign-key foreignTable="contact">
    <reference local="contact_id" foreign="id"/>
  </foreign-key>
  <foreign-key foreignTable="opportunity">
    <reference local="opportunity_id" foreign="id"/>
  </foreign-key>
</table>

然后你可以像其他任何东西一样查询它:

$association = ContactOpportunityQuery::create()
  ->filterByContact($contact)
  ->filterByOpportunity($opportunity)
  ->findOne();
$association->getAssociationType();

最新更新