简体   繁体   English

抓住总是被叫

[英]catch always get called

I'm having problem saving data to the database since catch exception is always being called. 由于总是调用catch异常,因此在将数据保存到数据库时遇到了问题。 try always get ignored. 尝试总是被忽略。 I don't really know what's happening. 我真的不知道发生了什么。 I've been working for this for hours and I can't get it to work. 我已经为此工作了几个小时,却无法正常工作。 I'm using kohana 3.3 and kostache. 我正在使用kohana 3.3和kostache。

So here's the controller. 这是控制器。

Controller 调节器

APPATH/classes/controller/album.php APPATH /类/控制器/album.php

public function action_create()
    {
                $view = Kostache_Layout::factory();
                $layout = new View_Pages_Album_List();
                $album = ORM::factory('Album_Information');
                $album_name = $this->request->post('inputAlbum');           
                $artist = $this->request->post('inputArtist');  
                $album->Album_Name = $album_name;
                $album->Artist = $artist;

                try
                {
                    $album->save();
                    HTTP::redirect('album');
                }
                catch(ORM_Validation_Exception $e)
                {
                    $layout->errors = $e->errors('models');
                }
            }   

        $this->response->body($view->render($layout));
    }

Templates 模板

APPATH/templates/pages/album/list.mustache APPATH /模板/页面/专辑/列表。小胡子

<h3>Add A New Album</h3>
<form action="album/create" method="post">

<label for="inputAlbum">Album Name:</label>
<input id="inputAlbum" type="text" name="inputAlbum" /><br />
<label for"inputAlbum" class="error">{{#errors}}{{inputAlbum}}{{/errors}}</label>

<label for="inputArtist">Album Artist:</label>
<input id="inputArtist" type="text" name="inputArtist" /><br />
<label for="inputArtist" class="error">{{#errors}}{{inputArtist}}{{/errors}}</label>

<input type="submit" name="submit" value="Add" />
</form>

Model Rules 示范规则

APPATH/classes/model/album/information.php APPATH / classes / model / album / information.php

class Model_Album_Information extends ORM
{
    protected $_primary_key = 'ID';
    protected $_table_name = 'album_information';

    public function rules()
    {
        return array(
                'inputAlbum' => array(
                    array('not_empty'),
                    ),
                'inputArtist' => array(
                    array('not_empty'),
                    ),
        );
    }

Messages 留言内容

APPATH/messages/models/album.php APPATH /消息/模型/album.php

return array(
        'inputAlbum' => array(
            'not_empty' =>  ':You must provide Album Name',     
            ),
        'inputArtist' => array(
            'not_empty' => ':You must provide Album Artist',
        ),
    );

The errors are showing when there's no input on the input field when i hit on the submit button, no problem with that, but even there's an input the errors are still being shown. 当我按下“提交”按钮时,在输入字段上没有输入时,就会显示错误,这没有问题,但是即使有输入,错误仍然会显示出来。 So catch is always being called. 所以总是被追赶。 When i remove try and catch I can easily save data to the database but there's no validation. 当我删除try and catch时,我可以轻松地将数据保存到数据库中,但是没有验证。

Thank you and more power. 谢谢您,还有更大的力量。

You expect the ORM class to magically know the value of $album->Album_Name came from a HTTP form input named inputAlbum . 您希望ORM类神奇地知道$album->Album_Name的值来自于名为inputAlbum的HTTP表单输入。 It won't. 不会的

Create rules for the Album_Name and Artist properties of the ORM object itself. 为ORM对象本身的Album_NameArtist属性创建规则。 Not the possible input method. 没有可能的输入法。

The Controller knows what data to pass to models. 控制器知道要传递给模型的数据。 The Model is only concerned with the data it received. 该模型仅与接收到的数据有关。 Not where it came from. 不是它来自哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM