Validate class that executes an sql query

Asked

Viewed 24 times

1

Hello, I have the following code in my dao.php.

public function insert(){
    $this->conn->beginTransaction();
    try {
        $sql = "QUERY DO INSERT AQUI .......";
        $stmt = $this->conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->execute();
        $this->conn->commit();
    } catch (Exception $e) {
        $this->conn->rollback();
    }
}

On my controller:

public function insert(){
    $dao = new DAO($this->conn);
    return $dao->insert();
}

Here I call the Insert class in my view to run the Insert.

$class = new Controller($conn);
//Insert
$class->insert();

Now the question, how do I validate is called ? If it is executed it shows one message if it is not shown another. I do this on my view or on the dao ? And how ? Thank you.

1 answer

1


in his DAO you can add a return of the method being it true or false:

public function insert(){
    $this->conn->beginTransaction();
    try {
        $sql = "QUERY DO INSERT AQUI .......";
        $stmt = $this->conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $response = $stmt->execute();
        $this->conn->commit();
        return $response;
    } catch (Exception $e) {
        $this->conn->rollback();
        return false;
    }
}

and in its View you can treat that return if it is true or false, for example:

$class = new Controller($conn);
//Insert
if($class->insert()){
    echo 'sucesso';
}else{
    echo 'erro';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.