PhalconPHP:如何在SELECT标记中设置一个Selected选项 [英] PhalconPHP: How to set a Selected option in SELECT tag

查看:106
本文介绍了PhalconPHP:如何在SELECT标记中设置一个Selected选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用来自Db的值设置<option>. 到目前为止,我已经尝试过了,但是没有用:

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的建议使用DB列生成选择:

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');

但是,关于Phalcon表格还有另一个可爱的窍门.您可以将数据库实体传递给表单类,然后表单将被自动填充.

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.

表格:

class YourFormClass extends Phalcon\Forms\Form
{
    public function initialize($entity = null, $options = null)
    {

控制器:

$entity = YourModel::findFirst();
$form = new YourFormClass ($entity, $options);

请注意,表单输入名称必须与DB/Model列匹配.

Note that Form input names must match DB/Model columns.

这篇关于PhalconPHP:如何在SELECT标记中设置一个Selected选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆