Eu estou montando um sistema com o Zf baseado no ZFDay feita pela school of net...
porém não esta gravando o registro quando eu tento executar o post no form do cadastro.
Ele passa pelo insert do datamapper mas não insere no banco de dados.
DataMapper.
class Application_Model_CategoriasMapper extends Icefusion_Db_DataMapperAbstract {
protected $_dbTable = "Application_Model_DbTable_Categorias";
protected $_model = "Application_Model_Categorias";
protected function _insert(Icefusion_Db_DomainObjectAbstract $obj) {
try {
$dbTable = $this->getDbTable();
$data = array(
'name' => $obj->getName(),
'description' => $obj->getDescription(),
'url' => $obj->getUrl(),
'meta-description' => $obj->getMeta_description(),
'keywords' => $obj->getKeywords(),
'top_id' => $obj->getTop_id(),
'shop_id' => $obj->getShop_id(),
'status' => $obj->getStatus(),
'order' => $obj->getOrder(),
'update' => $obj->getUpdate()
);
$dbTable->insert($data);
return true;
} catch (Zend_Exception $e) {
return false;
}
}
protected function _update(Icefusion_Db_DomainObjectAbstract $obj) {
try {
$dbTable = $this->getDbTable();
$data = array(
'name' => $obj->getName(),
'description' => $obj->getDescription(),
'url' => $obj->getUrl(),
'meta-description' => $obj->getMeta_description(),
'keywords' => $obj->getKeywords(),
'top_id' => $obj->getTop_id(),
'shop_id' => $obj->getShop_id(),
'status' => $obj->getStatus(),
'order' => $obj->getOrder(),
'update' => $obj->getUpdate()
);
$dbTable->update($data, array('id = ?' => $obj->getId()));
return true;
} catch (Zend_Exception $e) {
return false;
}
}
}
DbTable
class Application_Model_DbTable_Categorias extends Zend_Db_Table_Abstract {
protected $_name = 'mkx_categories';
}
Model
class Application_Model_Categorias extends Icefusion_Db_DomainObjectAbstract {
protected $_mapper = "Application_Model_CategoriasMapper";
private $name = null;
private $description = null;
private $url = null;
private $meta_description = null;
private $keywords = null;
private $top_id = null;
private $shop_id = null;
private $status = null;
private $order = null;
private $update = null;
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getDescription() {
return $this->description;
}
public function setDescription($description) {
$this->description = $description;
}
public function getUrl() {
return $this->url;
}
public function setUrl($url) {
$this->url = $url;
}
public function getMeta_description() {
return $this->meta_description;
}
public function setMeta_description($meta) {
$this->meta_description = $meta;
}
public function getKeywords() {
return $this->keywords;
}
public function setKeywords($keywords) {
$this->keywords = $keywords;
}
public function getTop_id() {
return $this->top_id;
}
public function setTop_id($topid) {
$this->top_id = $topid;
}
public function getShop_id() {
return $this->top_id;
}
public function setShop_id($shopid) {
$this->top_id = $shopid;
}
public function getStatus() {
return $this->status;
}
public function setStatus($status) {
$this->status = $status;
}
public function getOrder() {
return $this->order;
}
public function setOrder($order) {
$this->order = $order;
}
public function getUpdate(){
return $this->update;
}
public function setUpdate($update){
$this->update = $update;
}
}
Controller
class Admin_CategoriasController extends Zend_Controller_Action {
public function init() {
if (!Zend_Auth::getInstance()->hasIdentity()) {
return $this->redirect('/admin/auth');
}
}
public function indexAction() {
$form = new Icefusion_View_Helper_Admin_Forms_Categorias();
$this->view->form = $form->getFormCadastro();
$this->view->formExp = $form->getFormExportar();
/* $categorias = new Application_Model_Categorias();
var_dump($categorias->fetchAll());exit; */
$report = new Icefusion_View_Helper_Admin_Reports_Categorias();
$this->view->report = $report->getReportTable();
}
public function saveAction(){
$form = new Icefusion_View_Helper_Admin_Forms_Categorias();
if ($this->_request->isPost()) {
$data = $this->_request->getPost();
if ($form->isValid($data)) {
$categoria = new Application_Model_Categorias($data);
$categoria->save();
$this->_redirect("admin/categorias/");
}
}
$this->view->form = $form;
}
}
DataMapperAbstract
abstract class Icefusion_Db_DataMapperAbstract {
private static $_db = null;
protected $_dbTable = null;
protected $_model = null;
public function getDb() {
if (is_null(self::$_db))
self::$_db = Zend_Db_Table::getDefaultAdapter();
return self::$_db;
}
public function getDbTable() {
$this->_dbTable = new $this->_dbTable;
if (!$this->_dbTable instanceof Zend_Db_Table_Abstract)
throw new Exception('Tipo inválido de tabela');
return $this->_dbTable;
}
public function save(Icefusion_Db_DomainObjectAbstract $obj) {
if (is_null($obj->getId())){
$this->_insert($obj);
}else{
$this->_update($obj);
}
}
public function fetchAll(Zend_Db_Select $select = null) {
$dbTable = $this->getDbTable();
$db = $this->getDb();
$data = (!is_null($select)) ? $db->fetchAll($select) : $dbTable->fetchAll();
$dataObjArray = array();
foreach ($data as $row){
$dataObjArray[] = $this->_populate($row);
}
return $dataObjArray;
}
public function find($id) {
$result = $this->getDbTable()->find((int) $id);
if (0 == count($result))
return false;
$row = $result->current();
return $this->_populate($row);
}
public function getAsArray($id) {
$result = $this->getDbTable()->find((int) $id);
if (0 == count($result))
return false;
$row = $result->current();
return $row->toArray();
}
public function delete($id) {
$result = $this->getDbTable()->find((int) $id);
if (0 == count($result))
return false;
$row = $result->current();
return $row->delete();
}
protected function _populate($data) {
$obj = new $this->_model;
foreach ($data as $k => $v) {
$method = 'set' . ucfirst($k);
if (!method_exists($obj, $method)) {
throw new Exception('Invalid property - ' . $method);
}
$obj->$method($v);
}
return $obj;
}
public function getLastInsertId(){
$db = $this->getDb();
return $db->lastInsertId();
}
abstract protected function _insert(Icefusion_Db_DomainObjectAbstract $obj);
abstract protected function _update(Icefusion_Db_DomainObjectAbstract $obj);
}
DomainObjectAbstract
abstract class Icefusion_Db_DomainObjectAbstract {
private $id = null;
protected $_mapper = null;
public function __construct(array $options = null) {
if (is_array($options))
$this->setOptions($options);
}
public function setOptions(array $options) {
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods))
$this->$method($value);
}
return $this;
}
public function setId($id) {
if (!is_null($this->id)) {
throw new Exception('ID nao pode ser alterado');
}
$this->id = $id;
}
public function getId() {
return $this->id;
}
public function getMapper() {
$m = new $this->_mapper;
return $m;
}
public function save() {
$this->getMapper()->save($this);
}
public function fetchAll() {
return $this->getMapper()->fetchAll();
}
public function find($id) {
return $this->getMapper()->find($id);
}
public function getAsArray($id) {
return $this->getMapper()->getAsArray($id);
}
public function delete($id) {
return $this->getMapper()->delete($id);
}
public function getLastInsertId(){
return $this->getMapper()->getLastInsertId();
}
}