Zend models and database relathionships
Posted
by
user608341
on Stack Overflow
See other posts from Stack Overflow
or by user608341
Published on 2011-03-20T00:05:50Z
Indexed on
2011/03/20
0:10 UTC
Read the original article
Hit count: 135
Hi people, i'm starting with Zend Framework and I'm a little bit confused with models and relathionships (one-to-many, many-to-many etc).
The "Zend Framework Quick Start" says to create a Zend_Db_Table, a Data Mapper and finally our model class
Suppose we have a database like this:
table A (
id integer primary key,
name varchar(50)
);
table B (
id integer primary key,
a_id integer references A
);
then, i'll create:
Application_Model_DbTable_A extends Zend_Db_Table_Abstract,
Application_Model_AMapper,
Application_Model_A,
Application_Model_DbTable_B extends Zend_Db_Table_Abstract,
Application_Model_BMapper,
Application_Model_B,
if I understood, i've to store the references informations in Application_Model_DbTable_A:
protected $_dependentTables = array('B');
and Application_Model_DbTable_B:
protected $_referenceMap = array(
'A' => array(
'columns' => array('a_id'),
'refTableClass' => 'A',
'refColums' => array('id')
)
);
and my models class:
class Application_Model_A
{
protected $_id;
protected $_name;
public function __construct(array $options = null)
{
if(is_array($options)) {
$this->setOptions($options);
}
}
public function __set($name, $value)
{
$method = 'set' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid property');
}
$this->$method($value);
}
public function __get($name)
{
$method = 'get' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid property');
}
return $this->$method();
}
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 setName($name)
{
$this->_name = (string) $name;
return $this;
}
public function getName()
{
return $this->_name;
}
public function setId($id)
{
$this->_id = (int) $id;
return $this;
}
public function getId()
{
return $this->_id;
}
class Application_Model_B
{
protected $_id;
protected $_a_id;
public function __construct(array $options = null)
{
if(is_array($options)) {
$this->setOptions($options);
}
}
public function __set($name, $value)
{
$method = 'set' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid property');
}
$this->$method($value);
}
public function __get($name)
{
$method = 'get' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid property');
}
return $this->$method();
}
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 setA_id($a_id)
{
$this->_a_id = (int) $a_id;
return $this;
}
public function getA_id()
{
return $this->_a_id;
}
public function setId($id)
{
$this->_id = (int) $id;
return $this;
}
public function getId()
{
return $this->_id;
}
it's that right?
© Stack Overflow or respective owner