php - Improve my Zend Stored Procedure calling code -
i'm wondering how can improve zend code calls stored procedure. @ moment i'm using mysql db, , action function in controller below works, seems nasty.
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'd prefer use zend_db classes call stored procedure i'm not sure how can done?
since i'm calling number of stored procedures, think better create helper class wraps logic connecting db. expose methods wrap underlying stored procedure. controller code call
storedprocedurehelper::callmystoredprodecure($this->_request->getparam('param', 0);
is possible or recommended?
i favor use of models data access. also, advise define db adapter in application's config file it's transparent application code, , use pdo application not tied particular db manager in case need point different database in future.
for example, instead of having of data access logic in controller's action, simplified follows:
//within controller action $model = myubbercoolsupermodel(); $model->myubbercoolmethod($params); //and in model, 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(); // additional logic might want }
i think way clearer, controller responsible invoking method on model, model handles data access. if related data access needs refactored, changed, or whatever, know model have go instead of changing controller actions might impact other things.
hope helps ;)
cheers.
Comments
Post a Comment