由数据映射器处理集合模式中的项目 [英] Handling items in the collection pattern by a data mapper

查看:24
本文介绍了由数据映射器处理集合模式中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与谁应该处理复杂查询中的条件,数据映射器还是服务层?》下面是代码,方便参考.

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

在这段代码中,ArticleCollection 是域对象的集合,我们称它们为Article.ArticleCollectionMapper 从数据库中获取数据并将其分配给 $list 的那一刻,需要创建 Article 的实例(对于每一行).Article 的实例是否会通过类似 $list->addArticle($newArticle) 的方法添加到我们的集合实例 ($list) 中,应该使用像 ArticleFactory 这样的工厂对象,还是我没有考虑过其他选项?

In this code ArticleCollection is a collection of Domain Objects, let's call them Articles. 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?

推荐答案

我不认为实际使用工厂对象来添加文章.不过,您可能会看到自己使用一个来制作 Article 的实例(在第二个示例中).我继续做的是向 ArticleCollection 实例添加一个 addArticles () 方法.通过这种方式,您可以简单地从映射器调用 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() 方法,然后只需调用 addArticle 替换上面 foreach 中的内容().请注意,上述示例极其简化,并且需要对代码进行调整以符合您的标准.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆