Improve Call Code with Zend Stored Procedure

I am wondering how I can improve the Zend code that invokes the stored procedure. I am currently using MySQL DB, and the action function in my controller below works, but seems unpleasant.

public function callSPAction()
{
    $param = $this->_request->getParam('param', 0);

    $bootstrap = $this->getInvokeArg('bootstrap');
    $config = $bootstrap->getOptions();

    $mysqli = new mysqli(
        $config['resources']['db']['params']['host'],
        $config['resources']['db']['params']['root']['username'],
        $config['resources']['db']['params']['root']['password'],
        $config['resources']['db']['params']['dbname']);

    $rs = $mysqli->query(sprintf('CALL mystoredprocedure(%d)',$param));
    if(mysqli_error($mysqli))
    { 
        throw new exception(mysqli_error($mysqli), mysqli_errno($mysqli)); 
    } 
    $this->_helper->redirector('index', 'index');
}

I would prefer to use the Zend_DB classes to call the stored procedure, but I'm not sure how this can be done?

Since I call several stored procedures, I think it would be better to create a helper class that wraps the logic for connecting to the database. It will expose methods that will wrap the main stored procedure. My controller code could just be called

StoredProcedureHelper::callMyStoredProdecure($this->_request->getParam('param', 0);

Is this possible or even recommended?

+3
1

. , db , , PDO, , .

, , :

//within some controller action
$model = MyUbberCoolSuperModel();
$model->myUbberCoolMethod($params);

//And in your model, which can extend Zend_Db_Table_Abstract
public function myUbberCoolMethod($params)
{
   $pdo = $this->getAdapter()->prepare("CALL myProcedure(:param)");
   $pdo->bindParam(':param', $param, PDO::PARAM_STR); 
   $pdo->execute();
   // Any additional logic you might want to do
}

, , , . , -, , , - , , , , - , .

, ;)

.

+3

Source: https://habr.com/ru/post/1743345/


All Articles