orm - 使用 Propel ORM,如何删除对象的所有相关记录?

标签 orm propel

我有一个非常简单的 Propel 问题。我正在将潜在客户存储在数据库中。这些潜在客户具有一项或多项社区利益。我使用的表被命名为“lead”、“community”和“lead_communities”连接表。删除潜在客户的所有社区兴趣的最佳方法是什么?

这里有一些更多的细节。 Propel 架构如下所示:

    <table name="community">
      <column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" />
      <column name="name" type="VARCHAR" size="255" />
      <column name="address" type="VARCHAR" size="255" />
      etc...


    <table name="lead_communities">
      <column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true"/>
      <column name="lead_id" type="INTEGER" />
      <column name="community_id" type="INTEGER" />
      <column name="created_date" type="DATE" size="4" />
      <foreign-key foreignTable="community" refPhpName="Lead_Communities">
        <reference local="community_id" foreign="id"/>
      </foreign-key>
      <foreign-key foreignTable="lead" refPhpName="Lead_Communities">
        <reference local="lead_id" foreign="id"/>
      </foreign-key>      
    </table>  

    <table name="lead">
      <column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true"/>
      <column name="salutation" type="VARCHAR" size="20" />
      <column name="first_name" type="VARCHAR" size="255" defaultValue="" />
      <column name="last_name" type="VARCHAR" size="255" defaultValue="" />
      <column name="email" type="VARCHAR" size="255" defaultValue="" />
      etc..

以下是我从数据库中选择潜在客户的方法:
$lead = LeadQuery::create()->filterByEmail($enauk)->findOne();

所以,我希望做的是:
$lead->deleteLeadCommunities();

最佳答案

好吧,我能想到的没有任何其他上下文的最简单方法就是使用 delete() call 对连接表进行查询。 :

$numDeleted = LeadCommunitiesQuery::create()
                ->filterByLead($lead)
                ->delete();

为了清楚起见,没有像您想要的那样生成的方法( deleteLeadCommunities ),但是为了方便起见,您可以随时编写它,并替换 $lead在我的示例代码中 $this .所以里面Lead.php :
public function deleteLeadCommunities() {
  return LeadCommunitiesQuery::create()
           ->filterByLead($this)
           ->delete();
}

关于orm - 使用 Propel ORM,如何删除对象的所有相关记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14843645/

相关文章:

php - 如何在Kohana ORM中处理belongs_to关系

python - SQLAlchemy:运算符 LIKE 和自定义类型

python - 映射属性的基础查询

symfony1 - 在 Propel 中获取聚合列的值

php - 是否可以使用 Propel2 进行全文(匹配)查询?

criteria - Propel:SQL 中的 OR 相当于什么?

php - 获取结果集列名

python - Django 。从查询集中删除 select_related

java - Hibernate - 在 OneToMany 上使用 JOIN 时 sqlQuery 映射冗余记录

mysql - 插入 1.6 大于/小于