Services/Record.php: beforeUpdateEntity() misplaced?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michib
    Member
    • Sep 2016
    • 65

    Services/Record.php: beforeUpdateEntity() misplaced?

    Hi,

    I'm subclassing \Espo\Services\Record to intercept updates for a custom Entity. There I'm overriding beforeUpdateEntity() to find out what attributes are being updated. Looking at the updateEntity($id, $data) method it seems to me that beforeUpdateEntity($entity, $data) is actually misplaced because its called just after $entity->set($data).. shouldn't be moved before 'setting' new data? If not how could I find which attributes values are being updated?



    Code:
    public function updateEntity($id, $data)
        {
            unset($data->deleted);
    
            if (empty($id)) {
                throw new BadRequest();
            }
    
            $this->filterInput($data);
            $this->handleInput($data);
    
            unset($data->modifiedById);
            unset($data->modifiedByName);
            unset($data->modifiedAt);
            unset($data->createdById);
            unset($data->createdByName);
            unset($data->createdAt);
    
            if ($this->getEntityBeforeUpdate) {
                $entity = $this->getEntity($id);
            } else {
                $entity = $this->getRepository()->get($id);
            }
    
            if (!$entity) {
                throw new NotFound();
            }
    
            if (!$this->getAcl()->check($entity, 'edit')) {
                throw new Forbidden();
            }
    
            $entity->set($data);
    
            $this->beforeUpdateEntity($entity, $data);
    
            if (!$this->isValid($entity)) {
                throw new BadRequest();
            }
    
            if (!$this->checkAssignment($entity)) {
                throw new Forbidden();
            }
    
            if ($this->checkForDuplicatesInUpdate) {
                $this->processDuplicateCheck($entity, $data);
            }
    
            if ($this->storeEntity($entity)) {
                $this->afterUpdateEntity($entity, $data);
                $this->prepareEntityForOutput($entity);
    
                $this->processActionHistoryRecord('update', $entity);
    
                return $entity;
            }
    
            throw new Error();
        }
    thanks, best regards,
    Michele
    Last edited by michib; 12-03-2018, 06:14 PM.
  • yuri
    Member
    • Mar 2014
    • 8478

    #2
    Hi Michele,

    $entity->isAttributeChanged('attributeName');
    $previousValue = $entity->getFetched('attributeName');
    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

    • michib
      Member
      • Sep 2016
      • 65

      #3
      Hi Yuri, thanks.

      Michele
      Last edited by michib; 12-04-2018, 12:21 PM.

      Comment

      Working...