Skip to content

Instantly share code, notes, and snippets.

@javiermar
Created May 3, 2013 18:38
Show Gist options
  • Save javiermar/5512571 to your computer and use it in GitHub Desktop.
Save javiermar/5512571 to your computer and use it in GitHub Desktop.
DAO for User
<?php
class Model_Dao_User extends Zend_Db_Table_Abstract
{
protected $_name = "users";
protected $_primary = "id";
public function findBy($value, $by = 'id')
{
$select = $this->select()->where("{$by} = ?", $value)->limit(1);
try{
$result = $this->fetchRow($select);
}catch(exception $e){
throw new Zend_Db_Table_Select_Exception("Unable to retrieve {$this->_name} record");exit;
}
if($result instanceof Zend_Db_Table_Row){
return $result;
}else{
return false;
}
}
public function getList()
{
$select = $this->select()->order('id');
$result = $this->fetchAll($select);
if($result instanceof Zend_Db_Table_Rowset){
return $result;
}else{
return false;
}
}
/**
* Determines whether to insert or update a record
*
* @param Model_User $obj
* @param bool $ommitPassword
*
* @return int $id - primary key of new record
*/
public function save(Model_User $obj, $ommitPassword = false)
{
if(is_numeric($obj->id()) && $obj->id() !== 0){
$id = $this->_updateRow($obj, $ommitPassword);
}else{
$id = $this->_insertRow($obj, $ommitPassword);
}
return $id;
}
/**
*
* @param Model_User $obj
* @param bool $ommitPassword
*
* @return type
*/
private function _insertRow(Model_User $obj, $ommitPassword = false)
{
$data = $obj->toArray();
if($ommitPassword){
unset($data['password']);
}
$id = $this->insert($data);
return $id;
}
/**
*
* @param Model_User $obj
* @param bool $ommitPassword
*
* @return int
*/
private function _updateRow(Model_User $obj, $ommitPassword = false)
{
$data = $obj->toArray();
if($ommitPassword){
unset($data['password']);
}
$where = $this->getAdapter()->quoteInto( 'id = ?', $obj->id());
$id = $this->update($data, $where);
return $id;
}
public function deleteRow($id)
{
$where = $this->getAdapter()->quoteInto( 'id = ?', $id);
$this->delete($where);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment