【发布时间】:2013-06-13 07:19:38
【问题描述】:
我的问题与“Who should handle the conditions in complex queries, the data mapper or the service layer?”中@tereško's答案的更新部分有关,下面是代码供参考和方便。
$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 );
在这段代码中ArticleCollection 是域对象的集合,我们称它们为Articles。在ArticleCollectionMapper 从数据库中获取数据并将其分配给$list 的那一刻,需要创建Article 的实例(对于每一行)。 Article 的实例是否会通过$list->addArticle($newArticle) 之类的方法添加到我们的集合实例($list)中,是否应该使用ArticleFactory 之类的工厂对象,或者还有其他我没有考虑过的选项?
【问题讨论】:
标签: php design-patterns collections architecture datamapper