예제 #1
0
 public function update($id, $data = [])
 {
     try {
         //fetch the entity
         $entity = $this->gateway->findByPk($id);
         if (!$entity) {
             return $this->payload->notFound(['id' => $id]);
         }
         //set data in the entity; do not overwrite existing $id
         unset($data['id']);
         $entity->setData($data);
         // validate the entity
         if (!$this->filter->forUpdate($entity)) {
             return $this->payload->notValid([$this->entityName => $entity, 'messages' => $this->filter->getMessages()]);
         }
         // update the entity
         if (!$this->gateway->update($entity)) {
             return $this->payload->notUpdated([$this->entityName => $entity]);
         }
         //success
         return $this->payload->updated([$this->entityName => $entity]);
     } catch (\Exception $e) {
         return $this->payload->error(['exception' => $e, 'id' => $id, 'data' => $data]);
     }
 }