View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{zone-data:component-name}
Zend_Db_Model
{zone-data}

{zone-data:proposer-list}
[Borja Fernandez|mailto:borjafp@vivirasturias.com]
{zone-data}

{zone-data:revision}
1.2 - 19 July 2006: Updated from community comments.
{zone-data}

{zone-data:overview}
An object that wraps a row in a database table or view, encapsulates the database access, and allows to adds domain logic on that data.

{zone-data}

{zone-data:references}
*

{zone-data}

{zone-data:requirements}
none
{zone-data}

{zone-data:dependencies}
* Zend_Db
* Zend_Exception
{zone-data}

{zone-data:operation}
Define a Class based on Zend_Db_Model_Abstract and add your own domain logic.
{zone-data}

{zone-data:class-list}
* Zend_Db_Model
* Zend_Db_Model_Exception
{zone-data}

{zone-data:use-cases}
||UC-01||
{code}
// Initialization
$model = new Zend_Db_Model();
$model->setModelsDir(APP . '/models/');

// This return an object based on
// APP . '/models/' . Users.php class
$user = $model->factory('users');

// Basic insert
$user->username='myusername';
$user->password='mypass';
$user->email=$email;
$user->insert();

// associative array insert
$user->assignArray($array);
$user->insert();

// Basic update
$user->find('myusername');
$user->password='newpassword';
$user->update();


// Basic delete
$user->find('myusername');
$user->delete();

{code}

||UC-02||
{code}
/** Example model file based on this table
** CREATE TABLE users (
** username varchar(12) not null primary key,
** password varchar(12) not null,
** group_id unsigned int
** );
**
** CREATE TABLE groups(
** id unsigned int primary key,
** name varchar(12) not null
** );

// Users.php on APP . '/models/' dir
class Users Extends Zend_Db_Model_Abstract {
public function loadObjects() {
$this->addObject('group','group_id');
}
}

class Groups Extends Zend_Db_Model_Abstract {}

// Change group name if is an admin user
$users->find($id);
if ($users->group->name == 'ADMIN') {
$users->group->name = 'SUPERADMIN';
}
$users->save();

// Others uses
echo $users->lastLogin->dateValue('Year: %Y');
{code}
{zone-data}

{zone-data:skeletons}
{code}
Zend_Db_Model
setModelsDir($dir)
setAdapter($db)
factory($model,$id = null)

Zend_Db_Model_Abstract
public function __construct($object,$id = null)
private function _checkDb()
private function describeTable()
public function insert()
public function update()
public function delete()
public function toArray()
public function getAdapter()
public function assignArray($data)
public function addField($field,$data,$filter = null)
public function key($keyname = null)
public function keyvalue()
public function table($tablename = null)
public function assignPostVars()
public function find($id)

Zend_Db_Model_Exception


{code}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>