简体   繁体   English

Symfony2:向多对多关系添加连接条件

[英]Symfony2: Adding a join condition to a ManyToMany relationship

I have to change something in an existing Symfony2 project, but unfortunately I have never worked with Symfony2 before.我必须在现有的 Symfony2 项目中更改某些内容,但不幸的是我以前从未使用过 Symfony2。

The database contains the following tables:该数据库包含以下表格:

Location
========
id 
....

Deal
========
id 
deleted
...

deal_location
=================
deal 
location 

There is a Many-To-Many relationship between Deal and Location.交易和位置之间存在多对多关系。 This is mapped in the Location.orm.yml file like this:这在 Location.orm.yml 文件中映射如下:

 manyToMany:
    deals:
        cascade: ['all']
        targetEntity: Deal
        mappedBy: locations

What I want to do is to exclude all deals which where deleted (deleted = 1) when reading the locations from the Database.我想要做的是在从数据库中读取位置时排除所有被删除(删除 = 1)的交易。

As I found out, this can be done in de LocationRepository class.正如我发现的,这可以在 de LocationRepository 类中完成。 In that class, I found the following function:在那个类中,我发现了以下函数:

    public function getFindAllByLatLonQueryBuilder($lat, $lon)
{

    $qb = $this->createQueryBuilder('l');
    $qb
        ->where('l.deleted IS NULL OR l.deleted = false')
        ->orderBy('(((ACOS(SIN((:lat*PI()/180)) *
        SIN((l.latitude*PI()/180))+COS((:lat*PI()/180)) *
        COS((l.latitude*PI()/180)) * COS(((:lon-l.longitude)*
        PI()/180))))*180/PI())*60*1.1515*1.609344)', 'ASC')
        ->setParameter('lat', $lat)
        ->setParameter('lon', $lon)
    ;

    return $qb->getQuery()->getResult();
}

I found a similar question and added the following line:我发现了一个类似的问题并添加了以下行:

 ->leftJoin('l.deals', 'deals', 'WITH', 'deals.deleted = 0')

Unfortunately this doesn't work.不幸的是,这不起作用。 How can I make this work?我怎样才能使这项工作?

Instead of having two conditions in your where clause I would leave only where('l.deleted IS NOT true AND deals.deleted IS NOT true') and would simply add the leftJoin clause.而不是在你的where子句中有两个条件,我只会留下where('l.deleted IS NOT true AND deals.deleted IS NOT true')并且会简单地添加leftJoin子句。

Something like this should do the work:这样的事情应该做的工作:

public function getFindAllByLatLonQueryBuilder($lat, $lon)
{
    $qb = $this->createQueryBuilder('l')
        ->leftJoin('l.deal', 'deal', 'WITH', 'deal.deleted IS NOT true') // you can also try 'deal.deleted != true'
        ->where('l.deleted IS NOT true')
        ->orderBy('(((ACOS(SIN((:lat*PI()/180)) *
        SIN((l.latitude*PI()/180))+COS((:lat*PI()/180)) *
        COS((l.latitude*PI()/180)) * COS(((:lon-l.longitude)*
        PI()/180))))*180/PI())*60*1.1515*1.609344)', 'ASC')
        ->setParameter('lat', $lat)
        ->setParameter('lon', $lon)
    ;
    return $qb->getQuery()->getResult();
}

try尝试

->leftJoin('l.deals', 'deal', 'WITH', 'deal.deleted = 0')

you always have to join the object's name which in your case seems to be deals instead of deal given your yml.您总是必须加入对象的名称,在您的情况下,该名称似乎是交易而不是给定您的 yml 的交易。

Another thing which makes me wonder is why for location they check on deleted = null or false (see your code) but you check against deleted = 0. Are you sure this is the right check?让我想知道的另一件事是为什么他们检查删除的位置 = null 或 false(请参阅您的代码)但您检查删除的 = 0。您确定这是正确的检查吗?

leftJoin() not help u, is only needed if u use data from it in query, it not exclude deals, because u in this query get only localization. leftJoin() 对你没有帮助,只有当你在查询中使用它的数据时才需要它,它不排除交易,因为你在这个查询中只得到本地化。 I think exclude localization with deleted deals not help to, because in some localization u will have deleted and not deals.我认为排除本地化与已删除的交易无济于事,因为在某些本地化中,您将删除而不是交易。 It must be in other part of code where u get deals for localizations.它必须在您获得本地化交易的代码的其他部分。 So u must first find where deals are get from db, to make it only take not deleted.因此,您必须首先找到从 db 获取交易的位置,以使其不删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM