原则ManyToMany与加入条件的关系 [英] Doctrine ManyToMany relation with join condition

查看:675
本文介绍了原则ManyToMany与加入条件的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表之间的多对多关系,但是我想在连接表上添加一个连接条件。

I've got a Many-To-Many relation between two tables but I want to add a join condition on the join table.

/**
 * @ManyToMany(targetEntity="Company", inversedBy="Accounts")
 * @JoinTable(name="account_company",
 *      joinColumns = { @JoinColumn(name="account_id", referencedColumnName="id") },
 *      inverseJoinColumns = { @JoinColumn(name="company_id", referencedColumnName="id") }
 * )
 */
protected $companies;

我会有一个条件,如account_company.is_deleted = 0,我该怎么做? / p>

I would have a condition like "account_company.is_deleted = 0", how can I make it ?

推荐答案

是的,您可以选择使用自定义dql来水合您的对象及其ManyToMany集合:

Yes, you have the choice to hydrate your object and its ManyToMany collection using custom dql:

创建一个添加条件的存储库方法:

make a repository method that adds conditions on your join:

// repository class:
public function getAccountWithNonDeletedCompanies($id)
{
    return $this->createQueyBuilder('account')
        ->leftJoin('account.companies', 'companies', 'WITH', 'companies.deleted = 0')
        ->andWhere('account.id = :id')
        ->setParameter('id', $account)
        ->getQuery()
        ->getOneOrNullResult()
    ;
}

从我可以理解的,你永远不会显示删除的公司? (软删除)。

From what I can understand, you never want to display deleted companies ? (soft delete).

在这种情况下,您可能需要使用SQL过滤器: http://blog.loftdigital.com/doctrine-filters-and-soft-delete

In this case, you may want to use SQL Filters: http://blog.loftdigital.com/doctrine-filters-and-soft-delete

这篇关于原则ManyToMany与加入条件的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆