Announcement

Collapse
No announcement yet.

Get entity related in many-to-many relation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get entity related in many-to-many relation

    Hi!

    I was created a many-to-many relation between two entities. Now, I want to know how can I get the relational model of one of the entities. For example,

    we have: Accounts and Concepts this relation is many-to-many. Now, I have account1 with id 11 and I want to get with entitymanager the related Concept.

    With:
    $result = $entityManager->getRepository('Account')->findRelated($accountId, 'concept'); these not work. How can I do that? Thanks!

  • #2
    Hi

    (public function findRelated(Entity $entity, $relationName, array $params = array()) from /application/Espo/ORM/Repositories/RDB.php)
    the first param is Enitity
    the second - relation name (check the name in custom/Espo/Custom/Resources/metadata/entityDefs/Account.json)

    right way can be
    $account = $entityManager->getEntity('Account', $accountId);
    $result = $entityManager->getRepository('Account')->findRelated($account, 'concepts');

    Comment


    • krmn.msg
      krmn.msg commented
      Editing a comment
      Thanks for your reply.

      I tried it, but don't function. I can not access to $entityManager, it is NULL. I am trying to access from a service file. How can I do these? In controller file, neither it don't function.

      Thanks!

  • #3
    ??? I used your format. Use $this->getEntityManager()->getRepo......

    Comment


    • #4
      tanya Is there a way to do this without needing to loop through every parent? Many ORMs have something called with that includes related entities in the initial query, which is much more efficient. It would be something like
      PHP Code:
      $entityManager->getRepository('parent')->with('child')->where('...')->find() 
      As of right now, you have to do a lot more work to get the same information:

      PHP Code:
      foreach($parents as $parent) {
        
      $parentChild[...] = $entityManager->getRepository('parent')->findRelated(...)

      The idea is to get the related data in a single query to save time, database load, and API server load. Is that possible?

      Comment


      • #5
        PHP Code:
        $childList $entityManager->getRepository('Child')->distinct()->join('parents')->where([
           
        'parents.id' => $parentIdList
        ])->find(); 

        'parents' - relationship name;

        https://github.com/espocrm/documenta...ery-paramaters
        Last edited by yuri; 02-18-2018, 06:56 PM.

        Comment

        Working...
        X