Announcement

Collapse
No announcement yet.

one to one auto populate name field from users to tech entity.

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

  • one to one auto populate name field from users to tech entity.

    Is there a way to set the field in a one to one relationship with users to the other table? I have tried formulas but no luck. Any help would be very much appreciated. I have one to one relationship with users and techs. I would like to auto-populate the tech fields name with the users name on save in the users entity.

  • #2
    Hi,

    You can create a beforeSave hook for Tech to copy the name of the linked User record when a Tech is updated or created.

    If you need information on how to create hooks, you can check this first: https://docs.espocrm.com/development/hooks/

    Since One-to-One relationships create a foreign field in one of the entities but not on the other, check if the table "tech" contains a field named "user_id", if that is the case you can write something like this in your Tech hook class:
    Code:
    public function beforeSave(Entity $entity, array $options=array())
    {
        $entityManager = $this->getEntityManager();
        $sisterId = $entity->get("userId");
        $sisterType = "User";
        // get the sister entity
        $sisterObject = $entityManager->getRepository($sisterType)->where(['id'=>$sisterId])->findOne();
        // assign the User first and last names as Tech name
        $techName = $sisterObject->get("firstName")." ".$sisterObject->get("lastName");
        $entity->set("name",$techName);
    }
    If the table "tech" doesn't have a "user_id" field, then the table "user" must have a "tech_id" file. In that case adjust the code above accordingly.

    Formula is basically a facility to write beforeSave hooks from the Admin GUI "without writing code", but it requires you to master Espo's custom formula language, and sometimes getting what you want using formula may become more complicated than writing just plain PHP code logic.

    So in my opinion, formula is great if you don't want to write code or you are limited because you are using a cloud instance that doesn't allow for custom scripts, but for everyone else, custom hooks can do a much better job.

    Last edited by telecastg; 08-01-2020, 06:10 PM.

    Comment


    • #3
      telacastg, Thank you for your suggestion. I created a User_To_Tech_Hook but It does not seem to be filling the tech field. Can you let me know if there is a problem. The user table now has a techId.

      <?php

      namespace Espo\Custom\Hooks\Tech;

      use Espo\ORM\Entity;

      class User_To_Tech_Hook extends \Espo\Core\Hooks\Base

      public function beforeSave(Entity $entity, array $options=array())
      {
      $entityManager = $this->getEntityManager();
      $sisterId = $entity->get("techId");
      $sisterType = "Tech";
      // get the sister entity
      $sisterObject = $entityManager->getRepository($sisterType)->where(['id'=>$sisterId])->findOne();
      // assign the User first and last names as Tech name
      $techName = $sisterObject->get("firstName")." ".$sisterObject->get("lastName");
      $entity->set("name",$techName);
      }

      End Thank you very much for your assistance,

      Comment


      • #4
        Since the User entity is the one that has the link field (techId) you need to write the hook in the Espo\Custom\Hooks\User so it will apply to the User entity (the entity Tech has "no reference" to User, only User "knows that there is a Tech linked to it because of its "techId" field)

        Try this: (disclosure: Not tested, but should work)
        PHP Code:
        namespace Espo\Custom\Hooks\User;

        use 
        Espo\ORM\Entity;

        class 
        User_To_Tech_Hook extends \Espo\Core\Hooks\Base

        public function beforeSave(Entity $entity, array $options=array())
        {
            
        // for this example $entity is a User and $sisterObject is a Tech and $entity has an attribute techId
            
        $entityManager $this->getEntityManager();
            
        $sisterId $entity->get("techId");
            
        $sisterType "Tech";
            
        // get the sister entity
            
        $sisterObject $entityManager->getRepository($sisterType)->where(['id'=>$sisterId])->findOne();
            
        // assign the User first and last names as Tech name
            
        $techName $entity->get("firstName")." ".$entity->get("lastName");
            
        $sisterObject->set("name",$techName);
            
        // persist the Tech entity
            
        $entityManager->saveEntity($sisterObject);

        Last edited by telecastg; 08-02-2020, 05:05 AM.

        Comment


        • #5
          Thank you tesecastg. I will try this. I think I want to start a new post. I was able after much trial and error, to import the users table into the tech table. This worked on both sides and now they are linked. I still need to implement the hook for when we create new users. anyway, in case someone else needs to do this.

          Creating a one to one table linked(in my case techs) with users. users is one-to-one left.

          1. export your users entity. (Note, you will have to do to your espo/data/config.php file set the records per page to what ever you need. I set my two 2600 because I had that many users. 'recordsPerPage' => 100, to 2600 or what ever you need.
          In the same file, you will also need to change this 'recordListMaxSizeLimit' => 2600 refresh and rebuild.

          2. Export the complete users table. There is a radio button that will allow you to do this when you are doing exports. Choose your entity (mine was user) and match up any fields you want to move over to the related table. Make sure you select csv in the pull-down. It will make step 3 much easier.

          3. Go to admin import and choose your target. Select the users csv export that you created in step 2. Match the name lname, fname and any other fields that you want to move over to the second table. IMPORTANT, when you choose the ID, select the userId when matching.

          For 2600 records, this import takes a while so go get a cup of coffee.

          Once the import is complete, make sure that in layout manager, you have the user field in detail view in the second table (mine is tech) and make sure that you have the tech (or what every your entity name is) field in the users table.

          Clear cache and rebuild. You should now have links in both entity's pointing to each other. This is extremely useful for using custom coding on a custom entity and I hope this helps. Let me know if you have any questions.





          Comment


          • #6
            Thanks for posting the implementation, for sure will help others. That is the spirit :-)

            Comment

            Working...
            X