比方說,我有一個名爲模型讀取包含以下鎖定模式:添加字段,當一個模型被讀取
protected $_schema = array(
'_id' => array('type' => 'id'),
'name' => array('type' => 'string', 'unique' => true),
'read_by' => array('type' => 'string', 'array' => true)
);
的「read_by」字段實際上包含了讀了特定物品的用戶的列表。當模型被加載或:: find()被調用時,我希望它有另一個稱爲「read」的字段,如果當前登錄用戶的id在read_by中找到,則返回true,否則返回false。這可能嗎?我考慮過使用:: applyFilter方法,但我不確定從那裏去哪裏。
我使用Mongo作爲我的分貝。謝謝。
編輯 按照下文Dave的答案,則$結果對象由$鏈 - >下($自,$參數,可以$鏈)設置;在過濾器中會出現類似如下:
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
(
)
)
將此作爲文章模型的屬性遠比擁有'$ user-> read($ article)'更復雜。我會設計你反對填充這樣一個屬性,而是去調用一個方法。 – Nervetattoo
$ user-> read我假設會是一個輔助方法?我想將該字段添加到模型中,因爲我將整個模型作爲ajax響應進行觸發。 –
與修補它到實體本身的問題,當你開始修改代碼,並決定保存實體沒有保存通話使用白名單模式變得明顯。 我更喜歡,而且常常由具有'型號#toPublic'方法來修補在這種情況下的控制器的模型。 – Nervetattoo