首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取模型时添加字段

读取模型时添加字段
EN

Stack Overflow用户
提问于 2013-06-12 22:10:50
回答 1查看 129关注 0票数 0

假设我有一个名为Reads的模型,它包含以下锁定模式:

代码语言:javascript
复制
protected $_schema = array(
    '_id' => array('type' => 'id'),
    'name'  => array('type' => 'string', 'unique' => true),
    'read_by' => array('type' => 'string', 'array' => true)
);

"read_by“字段实际上包含了阅读特定文章的用户列表。当模型被加载或::find()被调用时,我希望它有另一个名为"read“的字段,如果在read_by中找到当前登录的用户id,则返回true,如果没有返回false。这个是可能的吗?我考虑过使用::applyFilter方法,但不确定从那里到哪里。

我在用Mongo做我的分贝。谢谢。

编辑如下所示,由$chain->next($self、$params、$chain)设置的$result对象;在过滤器中显示如下所示:

代码语言:javascript
复制
lithium\data\collection\DocumentSet Object
(
    [_original:protected] => Array
    (
    )

[_parent:protected] => 
[_pathKey:protected] => 
[_model:protected] => app\models\Reads
[_query:protected] => lithium\data\model\Query Object
    (
        [_map:protected] => Array
            (
            )

        [_entity:protected] => 
        [_data:protected] => Array
            (
            )

        [_schema:protected] => 
        [_classes:protected] => Array
            (
                [schema] => lithium\data\Schema
            )

        [_fields:protected] => Array
            (
                [0] => Array
                    (
                    )

                [1] => Array
                    (
                    )

            )

        [_alias:protected] => Array
            (
                [Reads] => 1
            )

        [_paths:protected] => Array
            (
                [Reads] => 
            )

        [_models:protected] => Array
            (
                [Reads] => app\models\Reads
            )

        [_autoConfig:protected] => Array
            (
                [0] => map
            )

        [_initializers:protected] => Array
            (
                [0] => model
                [1] => entity
                [2] => conditions
                [3] => having
                [4] => group
                [5] => order
                [6] => limit
                [7] => offset
                [8] => page
                [9] => data
                [10] => calculate
                [11] => schema
                [12] => comment
            )

        [_built:protected] => 1
        [_config:protected] => Array
            (
                [conditions] => Array
                    (
                        [feed_id] => Array
                            (
                                [$in] => Array
                                    (
                                        [0] => MongoId Object
                                            (
                                                [$id] => 51b79acbac53cfb51645ffa7
                                            )

                                    )

                            )

                        [read_by] => Array
                            (
                                [$nin] => Array
                                    (
                                        [0] => 51592dcc6d6d877c0a000002
                                    )

                            )

                    )

                [order] => Array
                    (
                        [date_added] => DESC
                    )

                [limit] => 25
                [user] => 51592dcc6d6d877c0a000002
                [fields] => 
                [page] => 
                [with] => Array
                    (
                    )

                [type] => read
                [model] => app\models\Reads
                [mode] => 
                [source] => reads
                [alias] => Reads
                [having] => Array
                    (
                    )

                [group] => 
                [offset] => 
                [joins] => Array
                    (
                    )

                [data] => Array
                    (
                    )

                [whitelist] => Array
                    (
                    )

                [calculate] => 
                [schema] => 
                [comment] => 
                [map] => Array
                    (
                    )

                [relationships] => Array
                    (
                    )

            )

        [_methodFilters:protected] => Array
            (
            )

    )

[_result:protected] => lithium\data\source\mongo_db\Result Object
    (
        [_cache:protected] => 
        [_iterator:protected] => 0
        [_current:protected] => 
        [_started:protected] => 
        [_init:protected] => 
        [_valid:protected] => 
        [_key:protected] => 
        [_resource:protected] => MongoCursor Object
            (
            )

        [_autoConfig:protected] => Array
            (
                [0] => resource
            )

        [_config:protected] => Array
            (
                [resource] => MongoCursor Object
                    (
                    )

                [init] => 1
            )

        [_methodFilters:protected] => Array
            (
            )

    )

[_valid:protected] => 1
[_stats:protected] => Array
    (
    )

[_started:protected] => 
[_exists:protected] => 
[_schema:protected] => 
[_autoConfig:protected] => Array
    (
        [0] => model
        [1] => result
        [2] => query
        [3] => parent
        [4] => stats
        [5] => pathKey
        [6] => exists
        [7] => schema
    )

[_data:protected] => Array
    (
    )

[_config:protected] => Array
    (
        [init] => 1
    )

[_methodFilters:protected] => Array
    (
    )

)

EN

回答 1

Stack Overflow用户

发布于 2013-06-13 04:00:24

在读取模型中,可以在find函数中定义__init()过滤器:

代码语言:javascript
复制
public static function __init() {
    static::applyFilter('find', function($self, $params, $chain) {
        //get the find result            
        $result = $chain->next($self, $params, $chain);
        //set default of 'read' field to false
        $read = false;
        //check if user is in 'read_by' array
        if (isset($params['user']) && isset($result->read_by)) {
            $read = in_array($params['user'], $result->read_by) ? true : false;
        });
        $result->read = $read;
        return $result;
    }
}

然后,在运行查找时,提供匹配的用户id或名称,或存储在read_by数组中的任何内容:

代码语言:javascript
复制
$reads = Reads::find('first', ['user' => 'username']);

(在这些示例中,我使用的是PHP5.4数组语法。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17076257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档