[英]Zend models and database relationships
我從Zend Framework開始,我對模型和關聯(一對多,多對多等)感到有些困惑。
“ Zend Framework快速入門”說明要創建一個Zend_Db_Table,一個數據映射器,最后是我們的模型類
假設我們有一個這樣的數據庫:
table A (
id integer primary key,
name varchar(50)
);
table B (
id integer primary key,
a_id integer references A
);
然后,我將創建:
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,
據我了解,我必須將引用信息存儲在Application_Model_DbTable_A中:
protected $_dependentTables = array('B');
和Application_Model_DbTable_B:
protected $_referenceMap = array(
'A' => array(
'columns' => array('a_id'),
'refTableClass' => 'A',
'refColums' => array('id')
)
);
和我的模型課:
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;
}
是嗎
好吧,假設您有一個名為products
的數據庫表。 您要訪問該表並使用數據。
首先,您需要在models/dbtable/
文件夾中使用Product.php
文件來告訴zf在哪里查找數據:
class Default_Model_DbTable_Product extends Zend_Db_Table_Abstract
{
protected $_name = 'products';
}
因此, $_name
是數據庫表的名稱。 其次,您需要一個用於存儲所有信息(例如產品屬性)的類。 假設一個產品只有一個名稱:
class Default_Model_Product{
protected $_name;
public function setName($name) {
$this->_name = $name;
return $this;
}
public function getName() {
return $this->_name;
}
public function __construct(array $options = null) {
if (is_array ( $options )) {
$this->setOptions ( $options );
}
}
//you need the methods below to access an instanz of this class
//put it in every model class
public function __set($name, $value) {
$method = 'set' . $name;
if (('mapper' == $name) || ! method_exists ( $this, $method )) {
throw new Exception ( 'Invalid product property' );
}
$this->$method ( $value );
}
public function __get($name) {
$method = 'get' . $name;
if (('mapper' == $name) || ! method_exists ( $this, $method )) {
throw new Exception ( 'Invalid product 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;
}
}
最后,您需要一個實際訪問數據的映射器類。 您從表中檢索數據並將其作為對象存儲在數組中:
class Default_Model_ProductMapper {
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 ( 'Default_Model_DbTable_Product' );
}
return $this->_dbTable;
}
//e.g. fetch all
public function fetchAll() {
$resultSet = $this->getDbTable ()->fetchAll ();
$entries = array ();
foreach ( $resultSet as $row ) {
$entry = new Default_Model_Product ();
$entry->setName ( $row->name );
$entries [] = $entry;
}
return $entries;
}
}
這確實很簡單;)
我真的建議您在MVC系統中使用另一個DB框架。
Zend在數據庫方面不太好,這是IMO太復雜/編寫太多代碼。 有一些框架可以與Zend(VC)很好地配合使用,例如PHPActiveRecord ...
-> http://www.phpactiverecord.org/projects/main/wiki/Frameworks
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.