php-activerecord 异常
我似乎无法找到一种方法来使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用 Model::create() 是否有原因?
我认为如果您使用,您的问题就不存在
Is there a reason you use
Model::create()
?I assume your problem isn't present if you use