【问题标题】:How to build a edit form with the lithium framework如何使用锂框架构建编辑表单
【发布时间】:2011-08-15 13:40:19
【问题描述】:

我正在尝试使用锂框架 (0.10) 编写编辑表单。我使用 MySQL 作为 DBMS。控制器如下所示:

public function edit() {
    $success = false;

    $data = Posts::find(42);

    return compact('data');
}

视图文件:

<?=$this->form->create(); ?>
    <?=$this->form->field('title');?>
    <?=$this->form->field('body', array('type' => 'textarea'));?>
    <?=$this->form->submit('Add Post'); ?>
<?=$this->form->end(); ?>

<?php if ($success): ?>
    <p style="color: red;">Post Successfully Saved</p>
<?php endif; ?>

调用站点时会收到以下错误消息:

Fatal error: Cannot use object of type lithium\data\entity\Record as array in /var/www/web/frameworks/lithium-0.10/app/resources/tmp/cache/templates/template_views_posts_edit.html_483_1313415231_358.php on line 2

我做错了什么?在锂中构建编辑表单的正确方法是什么?不幸的是,official lithium docs 中没有这方面的信息。

【问题讨论】:

    标签: php mysql frameworks lithium


    【解决方案1】:

    您想将数据传递给表单。这样就变成了

    <?=$this->form->create($data); ?>
    

    你可以看看几个月前我玩过的http://li3.me/docs/manual/quickstart。希望这也适用于最新版本。

    【讨论】:

    • 感谢您的回答!还有一个问题,我必须使用什么方法来保存更改的数据? ATM 我有if($this-&gt;request-&gt;data) { $post = Posts::update($this-&gt;request-&gt;data); $success = $post-&gt;save(); }
    • 正如您在dev.lithify.me/forks/harikt/lithium_blog 中看到的,首先我得到对象$user = User::find($this-&gt;request-&gt;id);,如果不重定向,则通过$user-&gt;save($this-&gt;request-&gt;data) 保存它。
    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    相关资源
    最近更新 更多