我已经使用 Magento 一年多了,并且已经学得足够好了。现在我想学习Zend,但我被模型困住了。
我习惯在 Magento 中使用实体和实体集合,并且我可能想要使用 Zend_Db_Table
、Zend_Db_Table_Row
和/或 Zend_Db_Table_Rowset
代码>.我感到困惑的是每个班级的角色。
我知道我可以扩展每个类,并且我知道在我的 Product_Table
类(扩展 Zend_Db_Table_Abstract
)中,可以使用私有方法来告诉 Zend 使用哪些类对于行和行集,但是我对此感到不舒服。
在 Magento 中包含以下代码:
示例 1
// I understand that maybe I'll use the `new` keyword instead
// Mage::getModel() is only for exemplification
$product = Mage::getModel('catalog/product');
$product->setName('product name');
$product->setPrice(20);
$product->save();
if($id = $product->getId()){
echo 'Product saved with id' . $id;
}
else{
echo 'Error saving product';
}
示例 2
$collection = Mage::getModel('catalog/product')->getCollection();
// this is the limit, I'm ok with other method's name
$collection->setPageSize(10);
$collection->load()
foreach($collection as $product){
echo $product->getName() . ' costs ' . $product->getPrice() . PHP_EOL;
}
我如何在 Zend Framework 中实现类似的功能?或者,如果这确实是一个坏主意,那么在 Zend Framework 中实现模型的最佳实践是什么?
谢谢
I have been working over an year with Magento and have learned it good enough. Now I want to learn Zend, and I'm stuck with models.
I'm used to have entities and collection of entities in Magento, and it's likely that I'll want to use Zend_Db_Table
, Zend_Db_Table_Row
and/or Zend_Db_Table_Rowset
. What I am confused of is the role each class.
I know that I can extend each class, and I understand that in my Product_Table
class (that extends Zend_Db_Table_Abstract
) it's possible to have private methods that will tell Zend what classes to use for rows and rowsets, however I'm not feeling comfortable with it.
Having this code in Magento:
Example 1
// I understand that maybe I'll use the `new` keyword instead
// Mage::getModel() is only for exemplification
$product = Mage::getModel('catalog/product');
$product->setName('product name');
$product->setPrice(20);
$product->save();
if($id = $product->getId()){
echo 'Product saved with id' . $id;
}
else{
echo 'Error saving product';
}
Example 2
$collection = Mage::getModel('catalog/product')->getCollection();
// this is the limit, I'm ok with other method's name
$collection->setPageSize(10);
$collection->load()
foreach($collection as $product){
echo $product->getName() . ' costs ' . $product->getPrice() . PHP_EOL;
}
How I can implement something similar in Zend Framework? Alternatively if this is a really a bad idea, what are the best practices to implement models in Zend Framework?
Thanks
发布评论
评论(4)
正如其他地方提到的,
Zend
团队对模型层的思考与大多数其他 PHP 框架创建者不同。他们当前关于使用原始工具提供数据库支持的实体模型的“最佳”方式的想法可以在 快速入门指南。也就是说,大多数人对 Zend Framework 中的模型的解决方案是引导 Doctrine。
The
Zend
team, as mentioned elsewhere, thinks differently about the Model layer than most other PHP Framework creators. Their current thoughts on "the best" way to use their raw tools to provide a Database backed Entity Model can be found in the quick start guide.That said, most people's solution to Models in Zend Framework is bootstrapping Doctrine.
以下是我个人实施模型的方法。我将使用一个现实生活中的示例:我的
User
模型。每当我创建模型时,我都会使用两个文件和两个类:模型本身(例如
Application_Model_User
)和映射器对象(例如Application_Model_UserMapper
)。模型本身显然包含数据、保存、删除、修改等方法。映射器对象包含获取模型对象、查找对象等的方法。这是用户模型的前几行:
对于每个属性,我有getter 和 setter 方法。
id
的示例:我还使用一些标准的“魔术方法”来公开公共 getter 和 setter(在每个模型的底部):
示例
save
方法:我在
save()
方法,当信息验证失败时使用异常。对于映射器对象,我至少有两种方法:一种返回用于选择对象的查询对象的方法,一种执行查询、初始化并返回对象的方法。我使用它是为了可以在控制器中操作查询以进行排序和过滤。
编辑
就像我在评论中所说,这篇文章:http ://weierophinney.net/matthew/archives/202-Model-Infrastruct.html 是我当前模型实现的灵感。
更多选项
您还可以使用
Zend_Form
进行验证,而不是自己进行验证:http://weierophinney.net/matthew/archives/200-Using-Zend_Form-in-Your-Models.html。我个人不喜欢这个选项,因为我认为Zend_Form
使用起来很尴尬并且很难精确控制。当大多数人第一次学习 Zend Framework 时,他们会学习子类化
Zend_Db
相关类。这是一篇文章,演示了这一点: http:// akrabat.com/zend-framework/on-models-in-a-zend-framework-application/我提到我不喜欢这样做。以下是几个原因:
编辑2
对于第二个示例:您可以使用 Zend_Paginator 对于 这。我提到过,在包装器中,您创建一个返回数据库查询对象以选择对象的方法。这是我的简化但有效的用户映射器:
为了处理分页器,我编写了一个自定义分页器插件并将其保存到library/Application/Paginator/Adapter/Users.php。确保您在 application.ini 中正确设置了
appnamespace
和autoloaderNamespaces[]
。这是插件:在我的控制器中:
然后在视图脚本中渲染分页器。
Here is how I, personally, implement models. I'll use a real life example: my
User
model.Whenever I create a model, I use two files and two classes: the model itself (e.g.
Application_Model_User
) and a mapper object (e.g.Application_Model_UserMapper
). The model itself obviously contains the data, methods for saving, deleting, modifying, etc. The mapper object contains methods for fetching model objects, finding objects, etc.Here are the first few lines of the User model:
For each property, I have a getter and setter method. Example for
id
:I also use some standard "magic methods" for exposing public getters and setters (at the bottom of each model):
Example
save
method:I validate inside the
save()
method, using exceptions when the information fails to validate.For the mapper object, I have at least two methods: a method that returns a query object for selecting objects, and one that executes the query, initializes and returns objects. I use this so I can manipulate the query in my controller for sorting and filtering.
EDIT
Like I said in my comments, this post: http://weierophinney.net/matthew/archives/202-Model-Infrastructure.html was the inspiration for my current Model implementation.
More options
You can also use
Zend_Form
to do validation, instead of rolling your own: http://weierophinney.net/matthew/archives/200-Using-Zend_Form-in-Your-Models.html. I personally don't like this option since I think thatZend_Form
is awkward to use and hard to precisely control.When most people first learn Zend Framework, they learn to subclass
Zend_Db
related classes. Here is an article that demonstrates this: http://akrabat.com/zend-framework/on-models-in-a-zend-framework-application/I mentioned that I don't like doing this. Here are a few reasons why:
EDIT 2
For your second example: You can use Zend_Paginator for this. I mentioned that, in your wrapper, you create a method that returns a database query object for selecting objects. Here's my simplified but working user mapper:
To handle the paginator, I write a custom Paginator plugin and save it to
library/Application/Paginator/Adapter/Users.php
. Be sure you have yourappnamespace
andautoloaderNamespaces[]
setup correctly in application.ini. Here is the plugin:In my controller:
Then render the paginator in your view script.
我做了类似于 SimpleCode 的方式。我的风格源自 Pádraic Brady。他有多篇博客文章,但他最好、最快的资源是他写的一本在线书:在深渊中生存!。此链接将直接带您进入有关模型、数据映射器和其他很酷的内容(例如延迟加载)的章节。这个想法如下:
您拥有诸如用户之类的实体,其属性在数组中定义。您的所有实体都使用从该数组获取或更新该数组的神奇 getter/setter 扩展一个抽象类。
对我来说,数据映射器与实体是分开的,它们的工作是对数据库进行 CRUD(创建、读取、更新和删除)。因此,如果我们需要从数据库加载实体,我会调用特定于该实体的映射器来加载它。例如:
此代码旨在给出如何以这种方式构建代码的想法。我没有测试这个,所以我在编写它时可能创建了一些拼写错误/语法错误。就像其他人提到的那样,Zend 让您可以使用模型做您想做的事,没有对错之分,这完全取决于您。我通常为数据库中要使用的每个表创建一个表类。因此,如果我有一个用户表,我通常有一个用户实体、用户映射器和用户表类。 UserTable 将扩展 Zend_Db_Table_Abstract,并且根据我正在做的事情,内部不会有任何方法,或者有时我会根据我的需要覆盖插入或删除等方法。我最终得到了很多文件,但我相信代码的分离使得我可以更轻松地快速到达我需要添加更多功能或修复错误的地方,因为我知道代码的所有部分都在哪里。
希望这有帮助。
I do something similar to SimpleCode's way. My style derives from Pádraic Brady. He has multiple blog posts but the best and quickest resource of his is a online book he wrote: Survive the Deep End!. This link should take you straight to his chapter on Models, Data Mappers, and other cool goodies such as Lazy Loading. The idea is the following:
You have entities such as a User with The properties are defined in an array. All your entities extend an abstract class with magic getter/setters that get from or update this array.
Data Mappers to me are separate from the Entities, their job is to do the CRUD (create, read, update, and delete) to the db. So, if we need to load an entity from the db, I call a mapper specific to that entity to load it. For example:
This code is to give an idea of how to architect the code in this way. I didn't test this so I may have created some typos/syntax errors as I wrote it. Like others have mentioned, Zend lets you do what you want with Models, there is no right and wrong it's really up to you. I usually create a table class for every table in the db that I want to work with. So if I have a user table, I usually have a User entity, User Mapper, and a User Table class. The UserTable would extend Zend_Db_Table_Abstract and depending on what I'm doing won't have any methods inside or sometimes I'll overwrite methods like insert or delete depending on my needs. I end up with lots of files but I believe the separation of code makes it much easier to quickly get to where I need to be to add more functionality or fix bug since I know where all the parts of the code would be.
Hope this helps.
文件夹结构
application/models/DbTable/User.php
application/forms/User.php
application/controllers/IndexController.php
application/views/scripts/index/index.phtml
Folder Structure
application/models/DbTable/User.php
application/forms/User.php
application/controllers/IndexController.php
application/views/scripts/index/index.phtml