This is what I ended up with, let me know if you have any suggestions on
how to improve this:
protected function getRelatedEntities($ent_class) {
$em = \ORM::entityManager();
$class = $em->getClassMetadata($ent_class);
$all_meta = $em->getMetadataFactory()->getAllMetadata();
$class_name = $class->getReflectionClass()->getName();
foreach($all_meta as $ent) {
/**
* @var $ent \Doctrine\ORM\Mapping\ClassMetadata
*/
$mappings = $ent->getAssociationMappings();
$fks = [];
foreach($mappings as $key => $field) {
if ($field['targetEntity'] == $class_name &&
$field['isOwningSide'] == true) {
$fks[] = $key;
}
}
if (!empty($fks)) {
yield $ent->getName() => $fks;
}
}
}
public function findRelatedItems($ent_class) {
$em = \ORM::entityManager();
foreach($this->getRelatedEntities($ent_class) as $cls => $fields) {
$rep = $em->getRepository($cls);
foreach($fields as $field) {
$ret = $rep->findBy([$field => $this->file]);
if ($ret) {
yield $rep->getClassName() => $ret;
}
}
}
}
Thanks
On Saturday, April 8, 2017 at 6:25:46 PM UTC-4, Marco Pivetta wrote:
>
> That is pretty much it - you'd just need to tinker with
> `$em->getClassMetadataFactory()->getAllMetadata()` and the
> `ClassMetadata#getAssociationNames()` method
>
> Marco Pivetta
>
> http://twitter.com/Ocramius
>
> http://ocramius.github.com/
>
> On Sun, Apr 9, 2017 at 12:17 AM, Michael Krasnow <[email protected]
> <javascript:>> wrote:
>
>> The closest I got was listing fields that have foreign keys to other
>> entities, I couldn't find a method or combination of methods to do the
>> opposite.
>>
>> $em = \ORM::entityManager();
>> $class = $em->getClassMetadata('\Entity\File\File');
>> var_dump($class->getAssociationNames());
>>
>>
>> On Friday, April 7, 2017 at 10:55:43 PM UTC-4, Marco Pivetta wrote:
>>>
>>> No, just try out some stuff with that first (it's all documented in
>>> `ClassMetadata.php`).
>>>
>>>
>>> On 8 Apr 2017 4:53 a.m., "Michael Krasnow" <[email protected]> wrote:
>>>
>>>> Thanks for pointing that out to me, I am looking over the docs for it.
>>>> I found getAssociationMappings() and it returns a ton of info, but it
>>>> doesn't seem to be the info I need
>>>>
>>>> Would it be possible to get a basic example?
>>>>
>>>> Thanks
>>>> On Friday, April 7, 2017 at 10:27:16 PM UTC-4, Marco Pivetta wrote:
>>>>>
>>>>> Using the ClassMetadata API, this should be trivial. What have you
>>>>> tried so far?
>>>>>
>>>>> On 8 Apr 2017 4:23 a.m., "Michael Krasnow" <[email protected]> wrote:
>>>>>
>>>>>> Hi all,
>>>>>>
>>>>>> I have a need to find all entities that have foreign keys to a
>>>>>> specific Entity.
>>>>>> I searched around but could not find any leads.
>>>>>>
>>>>>> This is all using MYSQL as the database.
>>>>>>
>>>>>> Thanks
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "doctrine-user" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to [email protected].
>>>>>> To post to this group, send email to [email protected].
>>>>>> Visit this group at https://groups.google.com/group/doctrine-user.
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "doctrine-user" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To post to this group, send email to [email protected].
>>>> Visit this group at https://groups.google.com/group/doctrine-user.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>> --
>> You received this message because you are subscribed to the Google Groups
>> "doctrine-user" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/doctrine-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.