如何创建一个MVC模式的Joomla组件教程(十六) - 创建管理员界面 增加编辑功能 下...

Hello Model

现在我们来创建model.

model有两个属性 _id and _data. _id存贮id,_data存储greeting数据.

构造器中首先从request中取得id

/**
 * Constructor that retrieves the ID from the request
 *
 * @access    public
 * @return    void
 */
function __construct()
{
    parent::__construct();

    $array = JRequest::getVar('cid',  0, '', 'array');
    $this->setId((int)$array[0]);
}

JRequest::getVar() 方法用来从request中获取数据。第一个参数是form变量的名称,第二个参数是参数默认值,第三个参数he name of the hash to retrieve the value from,第四个是数据类型。

构造器将取得cid数组中的第一个值并赋给id.

setId()被用来设置id,如果id变化了,那么对应的data也应该变化,否则就出错了,因此当我们设置id的值的时候,清空data的数据。
 

/**
 * Method to set the hello identifier
 *
 * @access    public
 * @param    int Hello identifier
 * @return    void
 */
function setId($id)
{
    // Set id and wipe data
    $this->_id        = $id;
    $this->_data    = null;
}

最后还要有getData()来获取数据,getData检查 data是否已经设置,如果已经设置,仅仅返回,如果没有,就从数据库载入。


/**
 * Method to get a hello
 * @return object with data
 */

function &getData()
{
    // Load the data
    if (empty( $this->_data )) {
        $query = ' SELECT * FROM #__hello '.
                '  WHERE id = '.$this->_id;
        $this->_db->setQuery( $query );
        $this->_data = $this->_db->loadObject();
    }
    if (!$this->_data) {
        $this->_data = new stdClass();
        $this->_data->id = 0;
        $this->_data->greeting = null;
    }
    return $this->_data;
}


表单 Form
以下是表单的代码清单:

<?php defined('_JEXEC') or die('Restricted access'); ?>

<form action="index.php" method="post" name="adminForm" id="adminForm">
<div class="col100">
    <fieldset class="adminform">
        <legend><?php echo JText::_( 'Details' ); ?></legend>
        <table class="admintable">
        <tr>
            <td width="100" align="right" class="key">
                <label for="greeting">
                    <?php echo JText::_( 'Greeting' ); ?>:
                </label>
            </td>
            <td>
                <input class="text_area" type="text" name="greeting" id="greeting" size="32" maxlength="250" value="<?php echo $this->hello->greeting;?>" />
            </td>
        </tr>
    </table>
    </fieldset>
</div>

<div class="clr"></div>

<input type="hidden" name="option" value="com_hello" />
<input type="hidden" name="id" value="<?php echo $this->hello->id; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="hello" />
</form>

注意,有一个id隐藏项,我们不应该改变id,而仅仅是传递。

转载于:https://www.cnblogs.com/vicenteforever/articles/1633018.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值