Home > Enterprise >  Check if update happened in put request
Check if update happened in put request

Time:07-25

I am new at PHP. We are creating REST API in Phalcon and I've created a put request. It already works, but I would like to check if update has really happened before sending a success response. So I've created a conditional for that ( if (!$product->update()) ), but it always returns 'true'. How can I check if any field has changed in a record?

 public function put()
{
    $id = $this->getParam('id');
    
    $input = $this->getRawData();
    
    $product = Product::findFirst([
        'conditions' => 'id = :id:',
        'bind' => ['id' => $id]
    ]);

    if ($product === null){
        throw new NotFoundException();
    }

    $product->assign($input);
    $product->update();

    if (!$product->update()) {
        $this->errorResponse($product->getMessages());
    } else {
        $this->successResponse($product->toArray($product->update()));
    }
}

CodePudding user response:

You are calling $product->update() three times. You do it once after the assign, then again for your if test, which is why it's always returning TRUE there I believe, and once inside the toArray() which may not actually return anything since the second and third updates don't have any data to update (not sure about that though).

I would code this as follows:

$product->assign($input);
$results = $product->update();

if (!results)) {
    $this->errorResponse($product->getMessages());
} else {
    $this->successResponse($results->toArray());
}

I am assuming that the $product->assign($input); statement is working as expected to update the $product data for you. I don't use that. I prefer to do direct assignments for updates so nothing is left to chance, ie. $product->whatever = $input['whatever'];.

Give this a try and hopefully it will work as expected for you.

CodePudding user response:

You can use Model Events, i.e. afterUpdate and notSaved, like:

use Phalcon\Mvc\Model;
use Phalcon\Http\Response;

class ModelBase extends Model
{
    public function afterUpdate()
    {
        $response = new Response();
        $response->setJsonContent([
           'success' => true,
           'message' => "Record updated"
        ])->send();
    }

    public function notSaved()
    {
        $response = new Response();
        $response->setJsonContent([
           'success' => false,
           'message' => 'Record not saved'
        ])->send();
    }
}

The Product and all other models will extend ModelBase. Then your code could be:

public function put()
{ 
  $id = $this->getParam('id');

  $input = $this->getRawData();

  $product = Product::findFirst([
    'conditions' => 'id = :id:',
    'bind' => ['id' => $id]
  ]);

  if ($product === null){
    throw new NotFoundException();
  }

  $product->assign($input);
  $product->update();
}

And Phalcon event will respond if the model was updated or not. If you prefer, you can also use custom http response codes for update or notSaved. More information about Model Events in the documentation

  • Related