Creating entity while filling link field, resulting in beig null

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Firyo
    Senior Member
    • Jun 2022
    • 134

    Creating entity while filling link field, resulting in beig null

    Hi there,

    I currently am facing an issue I never had in the past.
    Here is the process (and the problem) in question.

    My Entity "Test" (for the example) is saved,
    I check in its "beforeSave" hook something, if "true", I call a method inside the record Service of the entity "TestNumTwo"

    This method take the "Test" entity id in its parameters.
    Then, I create a "TestNumTwo" new entity and link it to my "Test" entity.

    After saving, no error nor warning are thrown BUT the 2 entities aren't linked at all.
    The field "test_id" in the DB is empty and my "TestNumTwo" is created without the link.


    I tried 2 differents ways to create my "TestNumTwo" entity (in order to check if something different would happen).


    First one
    PHP Code:
    $this->entityManager->createEntity('TestNumTwo', [
    'name' => $name,
    'description' => $description,
    'context' => $context,
    'testId' => $entityId, // "Test" entity id here
    'createdById' => $createdBy
    ]);
    The one I tried to do (with no different result)
    PHP Code:
    $testNumTwo = $this->entityManager
    ->getRDBRepository('TestNumTwo')
    ->getNew();
    
    $testNumTwo->set('name', $name);
    $testNumTwo->set('description', $description);
    $testNumTwo->set('context', $context);
    $testNumTwo->set('testId', $entityId);
    $testNumTwo->set('createdById', $createdBy);
    
    $this->entityManager->saveEntity($testNumTwo);

    What did I do wrong and how should I fix it ?

    Thanks.

    Regards,
    Firyo.
  • yuri
    Member
    • Mar 2014
    • 8455

    #2
    If it's v8.2 (or higher for those who read in the future), check the real name of the link. Maybe it's cTest.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • Firyo
      Senior Member
      • Jun 2022
      • 134

      #3
      Hi Yuri,

      I have the 8.1.4

      Comment

      • Firyo
        Senior Member
        • Jun 2022
        • 134

        #4
        Well, after hours of debugging (with the great help of Xdebug) I found it was because this process (which was part of our legacy code) was placed in the "beforeSave" hook but should have been placed inside the "afterSave" from the start.

        I also discovered the "->getFetched" function inside "Espo/ORM/Entity"

        Comment

        Working...