0

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

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 对象 在过滤器中显示如下:

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

)

4

1 回答 1

1

在您的 Reads 模型中,您可以find在函数中定义过滤器__init()

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数组中的任何内容:

$reads = Reads::find('first', ['user' => 'username']);

(我在这些示例中使用 PHP 5.4 数组语法。)

于 2013-06-13T04:00:24.040 回答