Can anyone tell me how to determine query type i.e. select, update, delete or insert before it is executed over MySQL.
I strongly believe internally Zend Framework might be using mysql*_query function to execute them.
I need a centralized function which will return the query type before the execution.
I am using three files for every database table
I am giving you the example.
Say I want to create model for categories table.
So I will create following files
DbTable/Categories.php
class Application_Model_DbTable_Categories extends Zend_Db_Table_Abstract {
protected $_name = 'categories';
protected $_dependentTables = array('Application_Model_DbTable_Videos');
}
Categories.php
class Application_Model_Categories extends Application_Model_CommonGetterSetter {
protected $_type = array('id' => 'int', 'name' => 'string', 'slug' => 'string', 'status' => 'int');
public function __construct(array $options = null) {
parent::__construct($options, __CLASS__);
}
}
CategoriesMapper.php
class Application_Model_CategoriesMapper {
protected $_dbTable;
public function setDbTable($dbTable) {
if (is_string($dbTable)) {
$dbTable = new $dbTable();
}
if (!$dbTable instanceof Zend_Db_Table_Abstract) {
throw new Exception('Invalid table data gateway provided');
}
$this->_dbTable = $dbTable;
return $this;
}
public function getDbTable() {
if (null === $this->_dbTable) {
$this->setDbTable('Application_Model_DbTable_Categories');
}
return $this->_dbTable;
}
public function save(Application_Model_Categories $categories) {
$data = array(
'name' => $categories->name,
'slug' => $categories->slug,
'status' => $categories->status,
);
if (@$categories->id <= 0) {
return $this->getDbTable()->insert($data);
} else {
$this->getDbTable()->update($data, array('id = ?' => $categories->id));
return $categories->id;
}
}
CommonGetterSetter.php
class Application_Model_CommonGetterSetter {
protected $_data = array();
private $_class_name;
public function __construct(array $options = null, $class_name = null) {
if (is_array($options)) {
foreach ($options as $key => $value) {
$this->$key = $value;
}
$this->_class_name = $class_name;
}
}
public function __set($name, $value) {
if (!array_key_exists($name, $this->_type)) {
throw new Exception("Invalid {$this->_class_name} property". $name);
}
else {
settype($value, $this->_type[$name]);
$this->_data[$name] = $value;
}
}
public function __get($name) {
if (!array_key_exists($name, $this->_type)) {
throw new Exception("Invalid {$this->_class_name} property". $name);
}
else {
return $this->_data[$name];
}
}
}
So I want to find out which query was executed , where and what should i write?
Mny thanks in advance.
I know the code is bit long but that was to give a complete idea.