2014-01-26 21 views
0

我試圖將更新的日期時間列轉換爲數字。所以我在我的Containable行爲中做了這個。試圖包含使用UNIX_TIMESTAMP的字段

$this->paginate = array(
     'limit'=>1, 
     'fields'=>array(
      'id', 
     ), 
     'contain'=>array(
      'Image'=>array('fields'=>array(
       'id', 
       'UNIX_TIMESTAMP(Image.updated) AS `rev`', 
      )) 
     ) 
    ); 

我從分頁程序

Array 
(
    [0] => Array 
     (
      [Receipt] => Array 
       (
        [id] => 19938 
       ) 
      [Image] => Array 
       (
        [0] => Array 
         (
          [id] => 18143 
          [receipt_id] => 19938 
          [Image] => Array 
           (
            [0] => Array 
             (
              [rev] => 1357872726 
             ) 

           ) 

         ) 

        [1] => Array 
         (
          [id] => 18144 
          [receipt_id] => 19938 
          [Image] => Array 
           (
            [0] => Array 
             (
              [rev] => 1357872728 
             ) 

           ) 

         ) 

       ) 

     ) 

) 

得到這個數據由於某種原因Image嵌套兩次,每次記錄。如果我刪除UNIT_TIMESTAMP,它會正確獲取數據。

$this->paginate = array(
     'limit'=>1, 
     'fields'=>array(
      'id', 
     ), 
     'contain'=>array(
      'Image'=>array('fields'=>array(
       'id', 
       'updated', 
      )) 
     ) 
    ); 

輸出

Array 
(
    [0] => Array 
     (
      [Receipt] => Array 
       (
        [id] => 19938 
       ) 

      [Image] => Array 
       (
        [0] => Array 
         (
          [id] => 18143 
          [updated] => 2013-01-10 21:52:06 
          [receipt_id] => 19938 
         ) 

        [1] => Array 
         (
          [id] => 18144 
          [updated] => 2013-01-10 21:52:08 
          [receipt_id] => 19938 
         ) 

       ) 

     ) 

) 

任何想法,爲什麼蛋糕將這個當我添加一個表達式到現場?

回答

1

您可以嘗試使用virtual fields

public function __construct($id = false, $table = null, $ds = null) { 
    parent::__construct($id, $table, $ds); 
    $this->virtualFields['name'] = sprintf(
     'UNIX_TIMESTAMP(%s.updated) AS %s.rev', $this->alias, $this->alias 
    ); 
} 

不知道如果我的符號是正確的,給它一個嘗試,我沒有測試它。

+0

我試過虛擬域,但它們不適用於關聯。 – cgTag

+0

沒錯,沒有意識到你正在關聯模型上嘗試它。 – burzum

相關問題