問題描述
使用 CakePHP:
Using CakePHP:
我有一個多對一的關系,讓我們假設它是許多葉子到樹.當然,我烘焙了一個表單,將一個葉子添加到一棵樹中,您可以通過表單助手創建的下拉框(標簽)指定它是哪個樹.
I have a many-to-one relationship, let's pretend it's many Leafs to Trees. Of course, I baked a form to add a Leaf to a Tree, and you can specify which Tree it is with a drop-down box ( tag) created by the form helper.
唯一的問題是,SELECT 框始終默認為 Tree #1,但我希望它默認為要添加到的 Tree:
The only thing is, the SELECT box always defaults to Tree #1, but I would like it to default to the Tree it's being added to:
例如,調用 example.com/leaf/add/5
會調出界面,將新的葉子添加到樹 #5.Leaf.tree_id
的下拉框將默認為Tree 5",而不是當前默認為Tree 1".
For example, calling example.com/leaf/add/5
would bring up the interface to add a new Leaf to Tree #5. The dropdown box for Leaf.tree_id
would default to "Tree 5", instead of "Tree 1" that it currently defaults to.
我需要在我的 Leaf 控制器和 Leaf view/add.ctp
中放入什么來做到這一點?
What do I need to put in my Leaf controller and Leaf view/add.ctp
to do this?
推薦答案
你不應該使用 select()
, or text()
, or radio()
等;這是可怕的做法.你應該使用 input()
:
You should never use select()
, or text()
, or radio()
etc.; it's terrible practice. You should use input()
:
$form->input('tree_id', array('options' => $trees));
然后在控制器中:
$this->data['Leaf']['tree_id'] = $id;
這篇關于CakePHP 在 SELECT 輸入中選擇默認值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!