2013-06-12 69 views
0

比方說,我有一個名爲模型讀取包含以下鎖定模式:添加字段,當一個模型被讀取

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

+2

將此作爲文章模型的屬性遠比擁有'$ user-> read($ article)'更復雜。我會設計你反對填充這樣一個屬性,而是去調用一個方法。 – Nervetattoo

+0

$ user-> read我假設會是一個輔助方法?我想將該字段添加到模型中,因爲我將整個模型作爲ajax響應進行觸發。 –

+2

與修補它到實體本身的問題,當你開始修改代碼,並決定保存實體沒有保存通話使用白名單模式變得明顯。 我更喜歡,而且常常由具有'型號#toPublic'方法來修補在這種情況下的控制器的模型。 – Nervetattoo

回答

1

在你讀的模型,你可以在__init()功能定義find過濾器:

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數組語法)。

+0

看起來像最好的解決辦法,但$ result-> read_by(或$ result-> name和$結果 - > _ ID爲此事)總是返回未定義的錯誤。 –

+1

你確定你沒有遇到連接問題?您的connections.php引導程序配置如何查看? –

+0

作爲讀取::發現(「全部」)返回我期望的正是連接顯示正常。 –

相關問題