0

In my code, when the function throws an error such as, organisation name is required or it is alread exists, the catch block works properly. But in the case of success, eventhough the corresponding organisation is inserted,i got the status of 200 with error message. What is the mistake in my code.

insertTenancy is the function written in Mycontroller.

 public function insertTenancy($data,$user, $res)
{

    $org_name = $data['org_name'];
    $queryString = "SELECT * FROM organisation WHERE name = ? and user_id =?";
    $query = $this->db->prepare($queryString);
    $query->execute([$org_name,$user]);

    if($query->fetch(\PDO::FETCH_ASSOC)){
        //throw new Exception('This organisation is already in use');
        return CCommon::prepareError($res, 'This organisation is already in use', 403);
    }

     $insertOrg = "INSERT INTO organisation (currency_id,name,user_id) VALUES (1,:name,:user_id)";

        $insertOrgname = $this->db->prepare($insertOrg);
        $insertOrgname->bindParam(':name', $org_name);
        $insertOrgname->bindParam(':user_id', $user_id);
        $insertOrgname->execute();
        $lastorgId = $this->db->lastInsertId();
        return $lastorgId;
 }

Following function written in routerContainer

try
    {
        $postData = $request->getParsedBody();
        $currentUser = $this->user;
        $data = array();
        $data = $this->MyController->insertTenancy($postData,$currentUser,$response);
            echo json_encode($data);
            exit;
    }
    catch (Exception $e)
    {
        $data = array();
        $data['success'] = false;
        $data['message'] = $e->getMessage();
        return CBCommon::prepareError($response, $data['message'], 403);
        exit;
    }
shalin
  • 373
  • 3
  • 19

1 Answers1

0

You create a new array then you add false to the index 'success'.

So why should

$data['message']

be defined?

Maybe you want change your

$error 

into

 $data['message']
Simon Müller
  • 451
  • 1
  • 4
  • 17