Announcement

Collapse
No announcement yet.

Services/Record.php: beforeUpdateEntity() misplaced?

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

  • 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.

  • #2
    Hi Michele,

    $entity->isAttributeChanged('attributeName');
    $previousValue = $entity->getFetched('attributeName');

    Comment


    • #3
      Hi Yuri, thanks.

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

      Comment

      Working...
      X