Check Duplicates on linked entity field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kolbermoorer
    Junior Member
    • Dec 2018
    • 7

    Check Duplicates on linked entity field

    Hello,
    we want to check on duplicates for a number in the entity + linked entity so that
    - the same number can occurs more than one times
    - but the same number can only appears once per linked entity

    That code is already working, so that the person has the same name and the same unique field, then the duplicates dialogue appears.

    This works:

    Code:
    <?php
    
    namespace Espo\Custom\Services;
    
    use \Espo\ORM\Entity as Entity;
    
    class Kreditor extends \Espo\Core\Templates\Services\Base
    {
        protected function getDuplicateWhereClause(Entity $entity, $data)
        {
            return array(
                'AND' => array(
                    array(
                        'name' => $entity->get('name')
                    ),
                    array(
                        'uniqueField' => $entity->get('uniqueField'),
                    ),
                ),
            );
        }
    }

    The fields "name" and "uniqueField" are inside the entity "Test", this entity has a "n:1" relation to the other entity "Mandant". What we want to achieve is that the same unique number can only appears once per linked entity "Mandant".

    Click image for larger version

Name:	relation.png
Views:	172
Size:	12.2 KB
ID:	44830


    This does not work:

    Code:
    <?php
    
    namespace Espo\Custom\Services;
    
    use \Espo\ORM\Entity as Entity;
    
    class Kreditor extends \Espo\Core\Templates\Services\Base
    {
        protected function getDuplicateWhereClause(Entity $entity, $data)
        {
            return array(
                'AND' => array(
                    array(
                        'mandant' => $entity->get('mandant')
                    ),
                    array(
                        'uniqueField' => $entity->get('uniqueField'),
                    ),
                ),
            );
        }
    }

    So my question is how the linked entity can be included in the duplicate check?

    Best regards,
    Thomas Kutschker

    P.s: Great CRM tool!

  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    hello,
    you can try to check by fieldName + 'Id' field, like
    Code:
     
     array(    'mandantId' => $entity->get('mandantId'), ),

    Comment

    • kolbermoorer
      Junior Member
      • Dec 2018
      • 7

      #3
      Originally posted by tanya
      hello,
      you can try to check by fieldName + 'Id' field, like
      Code:
      array( 'mandantId' => $entity->get('mandantId'), ),
      Hello,
      your code works in my test sytem! I think I tried it with mandant_id and mandant, but not with mandantId. As I now understand, mandantId is the unique identifier for the entity relation.

      Best regards,
      Thomas

      Comment

      Working...