Zend模型和数据库关系

时间:2011-03-20 00:05:51

标签: php zend-framework zend-db

我从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;
    }       

是吗?

2 个答案:

答案 0 :(得分:0)

我们假设您有一个名为products的数据库表。您想要访问此表并使用数据。

首先,您需要在models/dbtable/文件夹中Product.php文件告诉zf在哪里查找数据:

class Default_Model_DbTable_Product extends Zend_Db_Table_Abstract
{
    protected $_name = 'products';

}

所以$_name是db表的名称。其次,您需要一个存储所有信息(如产品属性)的类。假设一个产品只有一个名字:

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;
    }
}

这真的很直接;)

答案 1 :(得分:0)

我真的建议你在MVC系统中使用另一个DB-Framework。

Zend对数据库并不是很好,它的IMO太复杂/太多代码无法编写。 有一些框架与Zend(VC)一样非常好用,就像PHPActiveRecord ...

- > http://www.phpactiverecord.org/projects/main/wiki/Frameworks