repository A have a reference to repository B. How get all A where B.field='x'?

StackOverflow https://stackoverflow.com/questions/18576868

  •  27-06-2022
  •  | 
  •  

質問

I try to figure out how I get all Elements A where the referenced Element B has a value in a special field.

Lets say i have the follow models:

Person
id  name    active  companyref
1   Pers1   1       1
1   Pers2   1       2

Company
id  name    filter
1   Comp1   1
1   Comp2   5

When I want to get all the active persons I do the following in my repository:

function findAllActive() {
 $query = $this->createQuery();
 return $query->matching(
            $query->equals('aktiv', 1)
        )->execute();
}

Now my question is how can I get all persons where the refCompany got filter=5?


Edit: after the answer of freshp dont work I post my precise case:

this will work (I write the sql by my own):

function findAllActiveForPaketstufe($paketstufe) { 
 $query = $this->createQuery();
 $sql="SELECT p . * 
FROM  `tx_myext_domain_model_person` AS p
INNER JOIN tx_myext_domain_model_firma AS f ON f.uid = p.firma_ref
WHERE paketstufe =3";

 return $query->matching(
        $query->equals('aktiv', 1)
        )->execute();
}

and this will not work (I write the sql only half):

function findAllActiveForPaketstufe($paketstufe) { 
 $query = $this->createQuery();
 $sql="SELECT p . * 
FROM  `tx_myext_domain_model_person` AS p
INNER JOIN tx_myext_domain_model_firma AS f ON f.uid = p.firma_ref";

 return $query->matching(
            $query->logicalAnd(
                $query->equals('aktiv', 1),
                $query->equals('person.firmaref.paketstufe', intval($paketstufe))
            )
        )->execute();
}

I get the Error: Fatal error: Call to a member function getParentKeyFieldName()

Instead of "person.firmaref.paketstufe" I also tried:

  • firmaref.paketstufe
  • p.firmaref.paketstufe
  • firma_ref.paketstufe
  • and so on

And what I realy want is something like this (I dont have to write the sql at all):

 $query = $this->createQuery();
 return $query->matching(
      $query->locicalAnd(
            $query->equals('companyref.filter', 5),
            $query->equals('active', 1)
      )
 )->execute();

How can I get this to work?

役に立ちましたか?

解決

You can do it like:

function findAllActive() {
 $query = $this->createQuery();
 return $query->matching(
      $query->locicalAnd(
            $query->equals('companyref.filter', 5),
            $query->equals('active', 1)
      )
 )->execute();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top