Zend 模型和数据库关系

发布于 2024-10-25 04:15:26 字数 3826 浏览 2 评论 0原文

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

是这样吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2024-11-01 04:15:26

好吧,我们假设您有一个名为 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;
    }
}

这非常简单;)

Well let's assume you have a db table called products. You want to access this table and work with the data.

First you need in models/dbtable/ folder a Product.php file to tell zf where to look for the data:

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

}

So $_name is the name of the db table. Second you need a class where you store all the information like product attributes. Let's say a product has just a 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;
    }
}

Finally you need a mapper class where you actually access the data. You retrieve the data from the table and store it as objects in an array:

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

This is really straight forward ;)

蓝天白云 2024-11-01 04:15:26

我真的建议您在 MVC 系统中使用另一个 DB 框架。

Zend 在数据库方面不太好,而且在我看来,它太复杂/需要编写太多代码。
有些框架可以很好地与 Zend (VC) 配合使用,例如 PHPActiveRecord...

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

I really recommend you to use another DB-Framework in your MVC-System.

Zend isn't quite nice with Databases and it's IMO too complicated/ too much code to write.
There are Frameworks which play very nice with Zend (VC) like PHPActiveRecord...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文