假设我有一个名为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作为我的数据库。感谢。
修改 根据Dave的回答,下面是$ chain-> next设置的$ result对象($ self,$ params,$ chain);在过滤器中显示如下:
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
(
)
)
答案 0 :(得分:1)
在您的阅读模型中,您可以在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数组语法。)