由於您是蛋糕的新手,我認爲從HABTM關係開始並不是一個好主意,這是您在問題中需要的,也是最複雜的關係。因此,我假設您的模型關係更簡單: hasMany .Eg如下:
/*this is your state model
*state table in database should like:id,name
*and business table in database:id,name,state_id
*/
class State extends AppModel
{
var $name = 'State';
var $hasMany = array(
'Business'=>array(
'className'=>'Business',
'foreignKey'=>'state_id',
)
); //this means a State can hasMany Businesses while a Business only belongs to one State
}
然後做出行動,你的狀態控制器,因爲這:
/*in your state controller*/
function showBusinessesByState($statename)
{
if($statename && $thestate = $this->State->findByName($statename))
{
$this->set('state',$thestate);//debug $thestate you'll find data you need
}
else
{
$this->Session->setFlash("something wrong happens!");
}
}
現在,我想你可以處理視圖文件自己,用$state
變量重新發現其中也包含企業列表的數據$thestate
。
現在在做/app/config/routes.php路由部分:
Router::connect('/states/*',array('controller'=>'states','action' => 'showBusinessesByState'));
完成之後,你可能會得到你所需要的與/states/somestate
。當你經歷這個,你可能會嘗試解決這以最好的方式 - 。
謝謝,正是我所需要的!我會盡力鼓搗這個。只是想要朝正確的方向推進。我會閱讀數據庫關係。 – xtine 2010-04-12 19:53:42
好吧,遇到了問題。我創建了我的模型/控制器/視圖,但是當我嘗試查看/狀態時,它給我一個錯誤: 警告(512):SQL錯誤:1054:'字段列表'中的未知列'State.id'[CORE/SELECT * State'.'state_id','State'.'state','State'.'state_abbr','State'.'''''''''''''','''''''''''''' FROM'states'' AS' State' WHERE 1 = 1 我不知道爲什麼它試圖找到state.id,因爲我將外鍵設置爲state_id。 – xtine 2010-04-14 05:22:35
@ xtine,讓我看看控制器中的'find'代碼,plz。索引函數中的 – Young 2010-04-14 05:33:13