2011-06-07 18 views
0

什麼是CakePHP中這樣做的最好辦法:CakePHP的 - 郵政型無分貝表

我有一個帖子模型+觀點+控制器與我的數據庫「後類型」字段 - 鏈接到我的職位類型ID 。

的職位類型ID是: - 預覽 - 回顧 - 新聞

什麼我問的是:什麼是檢索後類型名稱的最佳方式(本機),而無需創建一個表Post_types和將其與post_id鏈接。

我試圖在配置列表中創建一個數組,它工作,但我認爲它可以更好,然後這。

回答

2

您可以使用Post模型的afterFind方法,並在其中添加post_type字段。另請參閱cookbook

+0

這很完美。正是我需要的。 – 2011-06-07 17:03:11

-1

使用$ hasOne將表格文章與表格post_types相關聯。 用例如 $ this-> Post-> find('all'); 你會在一個數組中獲得帖子和post_type。

+0

因爲OP明確表示他不希望爲表單類型使用表格。 – dhofstet 2011-06-07 15:37:55

1

從我的理解你的post_type是一個枚舉字段值preview, review, news或varchar你只知道只添加這3種類型,對吧?

你可以嘗試以下查詢

SELECT DISTINCT(post_type) FROM posts; 

這將返回在您posts表所使用的所有post_types

在Cake中,您可以使用findquery方法執行此操作。

# Using find 
$this->Post->find('all', array(
    'fields' => array('DISTINCT(post_type)') 
); 

# Or using the query directly (maybe easier in this case) 
$this->Post->query("SELECT DISTINCT(post_type) FROM posts;");