Cannot access protected property $id

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hi-ko
    Senior Member
    • May 2015
    • 102

    #1

    Cannot access protected property $id

    Disclaimer: I am not a PHP developer, so please bare with me asking this question

    Years ago I implemented a custom (afterSave) hook to calculate values for a custom field referencing the value of an auto-increment field (which is not accessable before save).

    I now upgraded this espocrm to the latest version but now get an exception accessing the entity id value I used so far to update the database.

    Code:
    ERROR: Slim Application Error Type: Error Code: 0 Message: Cannot access protected property Espo\Modules\Crm\Entities\Contact::$id File: /var/www/espocrm/custom/Espo/Custom/Hooks/Contact/ContactNo.php Line: 23 Trace: #0 /var/www/espocrm/application/Espo/Core/Hook/GeneralInvoker.php(186): Espo\Custom\Hooks\Contact\ContactNo->afterSave()
    If the entity id is now private, what is the recommended way to modify a field value of a new record afterSave?

    The function I used so far looks like this:

    PHP Code:
        public function afterSave(Entity $entity, array $options): void
        
    {
            if ( 
    $entity->isNew() ) {
                
    $entityId $entity->id;
                
    $entityNew $this->entityManager->getEntity('Contact'$entityId);
                
    $entityNo 'CO_' str_pad($entityNew->get('contactId'), 5'0'STR_PAD_LEFT );
                
    $update $this->entityManager->getQueryBuilder()->update()
                    ->
    in('Contact')
                    ->
    set(['contactNo' => $entityNo ])
                    ->
    where([
                        
    'id' => $entityId
                    
    ])
                    ->
    build();
                
    $this->entityManager->getQueryExecutor()->execute($update);
            }
        } 
    I know, for the example above I may alsoso independent from if this code above is a good example for a afterSave hook, my question is how to read values in such a hook after save and store calclulated values in another field.
    Last edited by hi-ko; 12-09-2025, 07:39 PM.
  • item
    Active Community Member
    • Mar 2017
    • 1556

    #2
    Hi,
    just see : $entity->getId();

    But i see some strange .. if this is Contact afterSave .. and you update Contact .. it's so i think

    PHP Code:

        
    public function afterSave(Entity $entity, array $options): void
        
    {
            if ( 
    $entity->isNew() ) {
                
    $entityNo 'CO_' str_pad($entity->getId(), 5'0'STR_PAD_LEFT );
                
    $update $this->entityManager->getQueryBuilder()->update()
                    ->
    in('Contact')
                    ->
    set(['contactNo' => $entityNo ])
                    ->
    where([
                        
    'id' => $entity->getId()
                    ])
                    ->
    build();
                
    $this->entityManager->getQueryExecutor()->execute($update);
            }
        } 
    Last edited by item; 12-09-2025, 08:28 PM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    • hi-ko
      Senior Member
      • May 2015
      • 102

      #3
      Hi @item,
      thank you very much for your quick help!

      Using the getter getId() actually did the trick. Issue solved.

      just fyi: contactId is a custom autoincrement field, so I have to reread the entity again as $entityNew to get the value filled by the db using autoincrement/sequence. What may seem a little strange to you is simply a work around to retrieve the autoincrement value that is not available in the PHP entity at that point in time.
      Last edited by hi-ko; 12-10-2025, 08:54 AM.

      Comment

      Working...