简体   繁体   English

原则:按相关值过滤实体

[英]Doctrine: Filter entities by related values

So, I have this (seemingly very simple) problem: I have a Doctrine entity, a Plan . 因此,我有一个这个问题(看似非常简单):我有一个Doctrine实体,一个Plan Plan s have associated Feature s (with a value property), these have associated FeatureTypes . Plan具有关联的Feature (具有value属性),这些具有关联的FeatureTypes

Now, given a list of FeatureType IDs and the possible values for each of these, I want to filter through all Plan entities. 现在,给定FeatureType ID的列表以及每个ID的可能值,我想筛选所有Plan实体。

My entities are pretty much like this: 我的实体非常像这样:

Plan
- id
- features (One-to-Many)

Feature
- id
- plan (Many-to-One; column would be plan_id)
- featureType (Many-to-One; column would be featureType_id)
- value

FeatureType
- id

What I mean is this: There is an array like this one: 我的意思是:有一个像这样的数组:

[
    1 => [
        '1 month',
        '3 months',
        '6 months',
    ],
    2 => [
        'Prepaid',
        'On Demand',
    ]
]

The keys are FeatureType IDs, the nested arrays are the value s I'm searching for. 关键是FeatureType ID,嵌套数组是我要搜索的value s。 I want all Plan s that have a Feature associated for every FeatureType in the above array, where this Feature 's value also is in the array from above. 我希望所有具有以上数组中每个FeatureType关联的Feature Plan ,其中该Featurevalue也在上面的数组中。

From all I can tell, the only solution is to simply get all Plans, and then filter through them in PHP after the fact. 据我所知,唯一的解决方案是简单地获取所有计划,然后在事后用PHP对其进行过滤。 I don't really like that approach though. 不过,我不太喜欢这种方法。 Is it possible to do this directly in Doctrine somehow? 是否有可能以某种方式直接在教义中这样做?

Ok, it appears that I have found an answer (even though this is still not as clean as I had hoped). 好的,看来我已经找到了答案(即使这仍然不像我希望的那样干净)。

I'm now looping through the array ( $features ), adding a conditional INNER JOIN for every parameter: 我现在遍历数组( $features ),为每个参数添加条件INNER JOIN:

$i = 0;

foreach ($features as $feature => $values) {
    $qb->innerJoin('plan.features', "feature_{$i}", 'WITH', "feature_{$i}.featureType = :featureType_{$i} AND feature_{$i}.value IN (:values_{$i})")
        ->setParameter("featureType_{$i}", $feature)
        ->setParameter("values_{$i}", $values);

    $i++;
}

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

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