Announcement

Collapse
No announcement yet.

How to use "conditions" in entityDefs

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

  • How to use "conditions" in entityDefs

    I created two custom entities: course and teachingStaff which have a many-to-may relationship called courseTeachingStaff.
    When I create a course, I need to define one or more teachers and one or more assistants. Both teachers and assistants are records of teachingStaff.
    Reading the documentation, it seems that multiple links can share the same relationship (courseTeachingStaff, in my case) provided the necessary condition for each link.
    I suppose that I have to create an additional column (using additionalColumns parameter) called "role" for the middle table course_teaching_staff where the value of "teacher" or "assistant" will be stored. Probably, in the custom/Espo/Custom/Resources/metadata/entityDefs/Course.json I need to add something like:

    Code:
    "links": {
            "teachers": {
                "type": "hasMany",
                "entity": "TeachingStaff",
                "relationName": "courseTeachingStaff",
                "conditions": {
                    "role": "teacher"
                },
                "additionalColumns": {
                    "role": {
                        "type": "varchar",
                        "len": "40"
                    }
                }
            },
            "assistants": {
                "type": "hasMany",
                "entity": "TeachingStaff",
                "relationName": "courseTeachingStaff",
                "conditions": {
                     "role": "assistant"
                },
                "additionalColumns": {
                    "role": {
                        "type": "varchar",
                        "len": "40"
                    }
                }
            }
        }​
    ​
    Unfortunately, I am rather stuck because I was not able to write a value in the "role" column of the middle table course_teaching_staff not even for a simple case when there is only one link (and not two links like I need).
    I have understood that I need to configure entities using a json configuration file (one for courses and one for teachingStaff) but then I don't know what other files have to be modified or created to customize espoCRM in order to manage courses and teaching staff. It is very frustrating because I know that espoCRM can do this but even after searching the web, reading documention and posts in the forum I don't have anything working.

    Really need help.

  • #2
    I'd recommend creating separate relationships.

    Columns can be written when you are using ORM's relate or updateRelation methods. But I would not recommend choosing this approach for your case.

    Comment


    • #3
      Also, if you would like to use the conditional relationship, you will need to specify the same condition on the opposite entityDefs side.

      Comment


      • #4
        I would like to take the easy road but I don't know which it is.
        People in the TeachingStaff entity may assume different roles depending on the course. I thought that if I create different entity for each role than I have to insert the same person details twice: one in the teacher entity and one in the assistant entity. But maybe I am wrong.
        Maybe, I have to do something like in opportunities where each contact can have a different role. I can program in PHP and I know json but it is not easy to find information on what to modify and where it is. Suggestions are welcome!

        Comment


        • #5
          You need a single entity but different relationships.

          Course - (teachers) many-to-many (coursesAsTeacher) - Person
          Course - (assistants) many-to-many (coursesAsAssistant) - Person

          You can create it via the UI.

          Comment


          • #6
            It works! And that was so simple. Even after reading documentation and searching (asking in) the forum I didn't realize that you can create different relationships to connect two entities.
            Thank you very much, Yuri!
            Last edited by Letsgetlost; 11-30-2023, 06:36 AM.

            Comment

            Working...
            X