Filter to exclude a related entities instances

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iceman17
    Junior Member
    • Sep 2017
    • 15

    Filter to exclude a related entities instances


    I create two entities "Registration" and "SIM". Then set a relationship between them, One-to-Many. ie. "Registration" (1 <----------> M) "SIM"

    How can I prevent an instance of SIM to be linked to Registration a second time? for example, say if (SIM instance #1) 78880183 is linked to Registration(instance #1) then (Registration instance #2) should not be able to link (SIM instance #1) 78880183 again.

    Kind regards
    Samantha
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    Hello Samantha.

    You can create a filter for SIM where you check if registrationId is null. http://forum.espocrm.com/forum/devel...-custom-entity http://forum.espocrm.com/forum/devel...-for-drop-down
    And you can create beforeSave hook, where you check if registrationId is changed. If yes, throw an error https://www.espocrm.com/documentatio...lopment/hooks/

    Comment

    • iceman17
      Junior Member
      • Sep 2017
      • 15

      #3
      Hi Tanya,
      Thank you for responding.

      How do a check if the linked field is null?.
      Here are my entities and the hook I created.
      Click image for larger version

Name:	ESPOCRM- FORMREGO-HOOK-BEFORESAVE.png
Views:	433
Size:	38.8 KB
ID:	31621
      Click image for larger version

Name:	ESPOCRM- FORMREGO-fields.png
Views:	400
Size:	75.2 KB
ID:	31622Click image for larger version

Name:	ESPOCRM-SIM-fields.png
Views:	412
Size:	71.3 KB
ID:	31624
      Attached Files

      Comment

      • tanya
        Senior Member
        • Jun 2014
        • 4308

        #4
        As I understand, you want, SIM has only one FORMREGO
        than you need a hook for SIM and checking looks like this
        Code:
        $regoformId = $entity->get('fORMREGOId')
        if ($regoformId) {
           throw \Error('This SIM is used in other FORMREGO');
        }
        (https://github.com/php-fig/fig-stand...ng-standard.md)

        Comment

        Working...