Update fails to update one column.

On the below code when I remove the commented code, during the update $entity->second_entity_id column fails. Anybody can help?

<?php function change_record() { $request = $this->request->getJsonRawBody();

$validation = new Validation();
$validation->add(
    'entity_id', new PresenceOf(['message' => 'Id required'])
);
$validation->add(
    'second_entity_id', new PresenceOf(['message' => 'Second Id required'])
);

$error = $validation->validate($request);

if (count($error) > 0) {
    return [
        'success' => false,
        'code' => 400,
        'message' => 'Invalid data provided'
    ];
}
$user = $this->auth->get_user();
$entity_id = $this->filter->sanitize($request->entity_id, 'int');
$second_entity_id = $this->filter->sanitize($request->second_entity_id, 'int');
$second_entity = \Carbon\Models\SecondEntity::findFirstByEntityId($second_entity_id);
if (!$second_entity) {
    return [
        'success' => false,
        'code' => 400,
        'message' => 'Entity not exists.'
    ];
}
$entity = \Carbon\Models\Entity::findFirstById($entity_id);
$current_entity = "";
// if ($entity->second_entity_id) {
//  $current_entity = $entity->entity->second_entity_id;
// }
// if ($current_entity == $second_entity_id) {
//  return [
//      'code' => 400,
//      'message' => 'Given entity and the existing one are same, can not change.'
//  ];
// }

$entity->second_entity_id = $second_entity->id;
$entity->modified_by = $user->id;
$entity->modified_at = date('Y-m-d H:i:s');

if ($entity->save()) {
    $update = new \Carbon\Models\EntityUpdates();
    $update->entity_id = $entity_id;
    $update->event_type = 'CHANGE';
        // $update->comment = "Entity has been changed from " . $current_entity . " to " . $second_entity_id;
    $update->created_at = date("Y-m-d H:i:s");
    $update->created_by = $user->id;
    $update->save();

    $output = [
        'success' => true,
        'code' => '',
        'message' => 'Successfully updated.'
    ];
} else {
    $errors = [];
    foreach ($entity->getMessages() as $msg) {
        $errors[] = $msg->getMessage(); 
    }
    $output = [
        'success' => false,
        'code' => '',
        'message' => $errors
    ];
}

return $output;

} ?>

it is because your testing is a mess. It should be:

// if ($entity->second_entity_id && $entity->seconde_entity_id == $second_entity->id)) { // return [ // 'code' => 400, // 'message' => 'Given entity and the existing one are same, can not change.' // ]; // }