由数据处理映射在收集模式项目 [英] Handling items in the collection pattern by a data mapper
问题描述
我的问题是有关 @tereško的答案在<一个更新部分href=\"http://stackoverflow.com/questions/11942842/who-should-handle-the-conditions-in-complex-queries-the-data-mapper-or-the-serv/11943107#11943107\">Who要处理复杂的查询条件,数据映射器或服务层?下面是code,以供参考和方便。
My question is related to the update section of @tereško's answer in "Who should handle the conditions in complex queries, the data mapper or the service layer?" Below is the code for reference and convenience.
$category = new Category;
$category->setTitle( 'privacy' );
$list = new ArticleCollection;
$list->setCondition( $category );
$list->setDateRange( mktime( 0, 0, 0, 12, 9, 2001) );
// it would make sense, if unset second value for range of dates
// would default to NOW() in mapper
$mapper = new ArticleCollectionMapper;
$mapper->fetch( $list );
foreach ( $list as $article )
{
$article->setFlag( Article::STATUS_REMOVED );
}
$mapper->store( $list );
在此code ArticleCollection
是域对象的集合,我们姑且称之为文章
秒。当下 ArticleCollectionMapper
从数据库中获取数据,将其分配给 $列表
,的实例制品
需要进行(每行)。将文章
的情况下,通过一个方法被添加到我们收集实例( $列表
),如 $列表 - &GT; addArticle($ newArticle)
,应该像 ArticleFactory
被用于,或有另一种选择我的避风港T考虑?
In this code ArticleCollection
is a collection of Domain Objects, let's call them Article
s. The moment the ArticleCollectionMapper
fetches data from the database, assigning it to $list
, instances of Article
need to be made (for each row). Would the instances of Article
be added to our collection instance ($list
) via a method like $list->addArticle($newArticle)
, should a Factory Object like ArticleFactory
be used for that, or is there another option I haven't considered?
推荐答案
我不认为实际使用工厂对象添加的文章。你可以使用一个做实例文章
(在第二个例子),但看到自己。我说干就干,做的是增加一个 addArticles()
方法的 ArticleCollection
实例。这样你就可以简单的叫你从mapper ArticleCollection
的实例的方法。 ArticleCollectionMapper
可能看起来是这样的:
I wouldn't think to actually use a factory object to add the articles. You may see yourself using one to make the instance of Article
(in the second example), though. What I went ahead and did was add an addArticles ()
method to the ArticleCollection
instance. This way you can simply call the method on your instance of ArticleCollection
from the mapper. ArticleCollectionMapper
may look something like:
class ArticleCollectionMapper extends DataMapperAbstract
{
public function fetch ( ArticleCollection $articles )
{
$prepare = $this->connection->prepare( "SELECT ..." );
$prepare->execute();
// filter conditions
$articles->addArticles( $prepare->fetchAll() );
}
}
您会需要从 ArticleCollection
实例,这是排除在上面的代码段获得的条件做一些过滤。那么,我们的域对象的 addArticles()
的实施将类似于以下内容:
You'd need to do some filtering by getting the conditions from the ArticleCollection
instance, which is excluded from the snippet above. Then our domain object's addArticles()
implementation would look similar following:
class ArticleCollection extends DomainObjectAbstract
{
protected $collection = array();
public function addArticles ( Array $articles )
{
foreach ( $articles as $article )
{
$articleCollectionItem = new Article;
$articleCollectionItem->setParams( $article );
// however you prefer filling your list with `Article` instances
$this->collection[] = $articleCollectionItem;
}
}
}
您可能还需要添加一个 addArticle()
方法根据自己的需要,然后只需替换在的foreach $ C什么$ C>上面调用
addArticle()
。需要注意的是上面的例子非常简化,code的将会的需要,以满足你的标准进行调整。
You may also want to add an addArticle()
method depending on your needs, and then just replacing what's within the foreach
above with a call to addArticle()
. Note that the above examples are extremely simplified and code will need to be adapted in order to meet your standards.
这篇关于由数据处理映射在收集模式项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!