PhalconPHP:如何在SELECT标记中设置Selected选项(PhalconPHP: How to set a Selected option in SELECT tag)
我想用来自Db的值设置<option> 。 到目前为止,我试过这个但没有工作:
$site_select = new Select('site_id', $this->_sites);I want to set the <option> with the value coming from Db. So far I tried this but not working:
$site_select = new Select('site_id', $this->_sites);最满意答案
更新
正如Timothy建议的那样使用数据库列生成选择:
new Select('site_id', Sites::find(), array('using' => array('site_id', 'site_name')));设置给定选择的选定值:
$site_select->setDefault('YOUR_DB_VALUE');然而,关于Phalcon形式还有另一个可爱的伎俩。 您可以将数据库实体传递给表单类,您的表单将自动填充。
形成:
class YourFormClass extends Phalcon\Forms\Form { public function initialize($entity = null, $options = null) {控制器:
$entity = YourModel::findFirst(); $form = new YourFormClass ($entity, $options);请注意,表单输入名称必须与DB / Model列匹配。
Update
Generating the select using DB columns as Timothy recommended:
new Select('site_id', Sites::find(), array('using' => array('site_id', 'site_name')));Setting the selected value of the given select:
$site_select->setDefault('YOUR_DB_VALUE');However there is another lovely trick about Phalcon forms. You could pass your DB entity to the form class and your form will be auto populated.
Form:
class YourFormClass extends Phalcon\Forms\Form { public function initialize($entity = null, $options = null) {Controller:
$entity = YourModel::findFirst(); $form = new YourFormClass ($entity, $options);Note that Form input names must match DB/Model columns.
更多推荐
发布评论