php-activerecord 异常

发布于 2024-12-13 07:42:28 字数 1831 浏览 0 评论 0原文

我似乎无法找到一种方法来使用 METHOD:2

METHOD:1 捕获 ActiveRecord 异常 这按预期工作,对对象有足够的了解来记录错误!

$model = model::find(id);
if($model->delete()){
echo 'yay';
}else{
throw new \ActiveRecord\ActiveRecordException($model->errors())
}

//METHOD:2 这没有信息来记录错误

if(Model::create(attributes)){
echo 'yay'
}else{
throw new \ActiveRecord\ActiveRecordException('where do I pull error from now?')
}

// 我的真实世界示例

public function create(){

    //grab the validation data from $this->validation_rules
    $this->form_validation->CI = & $this;
    $this->form_validation->set_rules($this->validation_rules);

    //run the validation
    if ($this->form_validation->run($this)) {

        //loop through $_POST data and change any $key[values] where needed
         foreach($this->input->post() as $k => $v){
             if($k == 'status'){
                 $v = ($v === 'publish') ? 1 : 0;

             }
             // recomplie $_POST with modified values into temp array and break out of loop
              $tmp[$k] = $v;
             continue;
         }

        //try to create a new page ( throw exception : build )  
        try{
            if(!Page::create($tmp)){
                throw new \ActiveRecord\ActiveRecordException('Arrrgh custom error required with some guess work?');
            }else{
                $this->session->set_flashdata('success', 'Page Successfully Created');
                redirect('admin/pages');
            }
        }catch(\ActiveRecord\ActiveRecordException $e){
            //log any errors thrown 
            log_message('error', $e->getMessage());
        }
        // validation failed, show form again
    }else{
        $this->create_form();
    }
}

I cant seem to figure out a way catch ActiveRecord Exceptions using METHOD:2

METHOD:1 This works as expected, knows enough about the object to record errors!

$model = model::find(id);
if($model->delete()){
echo 'yay';
}else{
throw new \ActiveRecord\ActiveRecordException($model->errors())
}

//METHOD:2 This has no information to record an error

if(Model::create(attributes)){
echo 'yay'
}else{
throw new \ActiveRecord\ActiveRecordException('where do I pull error from now?')
}

// my real world example

public function create(){

    //grab the validation data from $this->validation_rules
    $this->form_validation->CI = & $this;
    $this->form_validation->set_rules($this->validation_rules);

    //run the validation
    if ($this->form_validation->run($this)) {

        //loop through $_POST data and change any $key[values] where needed
         foreach($this->input->post() as $k => $v){
             if($k == 'status'){
                 $v = ($v === 'publish') ? 1 : 0;

             }
             // recomplie $_POST with modified values into temp array and break out of loop
              $tmp[$k] = $v;
             continue;
         }

        //try to create a new page ( throw exception : build )  
        try{
            if(!Page::create($tmp)){
                throw new \ActiveRecord\ActiveRecordException('Arrrgh custom error required with some guess work?');
            }else{
                $this->session->set_flashdata('success', 'Page Successfully Created');
                redirect('admin/pages');
            }
        }catch(\ActiveRecord\ActiveRecordException $e){
            //log any errors thrown 
            log_message('error', $e->getMessage());
        }
        // validation failed, show form again
    }else{
        $this->create_form();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

八巷 2024-12-20 07:42:28

您使用 Model::create() 是否有原因?

我认为如果您使用,您的问题就不存在

$model = new Model($attributes);

Is there a reason you use Model::create()?

I assume your problem isn't present if you use

$model = new Model($attributes);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文