Php 当模型在MVC中的DB中找不到记录时,在何处传回消息?

Php 当模型在MVC中的DB中找不到记录时,在何处传回消息?,php,codeigniter,Php,Codeigniter,我想知道从模型向控制器传回成功或失败消息的最佳消息是什么?成功消息很简单,因为我们可以将数据传回。但是,对于失败,我们只能传递FALSE,而不能传递失败的回调结果 最好的方法是什么 这里是方法一: 模型如下: function get_pkg_length_by_id($data) { $this->db->where('id', $data['pkg_length_id']); $result = $this->db->get('pkg_lengths'

我想知道从模型向控制器传回成功或失败消息的最佳消息是什么?成功消息很简单,因为我们可以将数据传回。但是,对于失败,我们只能传递FALSE,而不能传递失败的回调结果

最好的方法是什么

这里是方法一:

模型如下:

function get_pkg_length_by_id($data) {
    $this->db->where('id', $data['pkg_length_id']);
    $result = $this->db->get('pkg_lengths');
    if($result->num_rows() > 0 ) {
        return $result->row();
    }
    else {
        return false;
    }
}
在控制器中,我将执行以下操作

function show() {
    if(get_pkg_length_by_id($data) { 
        //pass success message to view
    }
    else {
        //Pass failure message to view
    }
以下是第2版:

模型中

function get_pkg_length_by_id($data) {
    $this->db->where('id', $data['pkg_length_id']);
    $result = $this->db->get('pkg_lengths');
    if($result->num_rows() > 0 ) {
        $result['status'] = array(
            'status' => '1',
            'status_msg' => 'Record found'
        );
        return $result->row();
    }
    else {
        $result['status'] = array(
            'status' => '0',
            'status_msg' => 'cannot find any record.'
        );
        return $result->row();
    }
}
内部控制器

function show() {
$result = get_pkg_length_by_id($data);
    if($result['status['status']] == 1) { 
        //pass $result['status'['status_msg']] to view
    }
    else {
        //pass $result['status'['status_msg']] to view
    }

我不能肯定哪一个是最好的。我可以说,我经常使用选项2,在这里我从服务器传递错误,通常以特定的形式这样做,我的控制器的任何子类都可以解析并发送到视图

另外,在show函数中,else是无关的,一旦返回,您将中断函数,因此您可以执行以下操作:

if($result->num_rows() > 0 ) {
    $result['status'] = array(
        'status' => '1',
        'status_msg' => 'Record found'
    );
   //the condition is met, this will break out of the function
    return $result->row();
}
$result['status'] = array(
  'status' => '0',
   'status_msg' => 'cannot find any record.'
);
return $result->row();

我不能肯定哪一个是最好的。我可以说,我经常使用选项2,在这里我从服务器传递错误,通常以特定的形式这样做,我的控制器的任何子类都可以解析并发送到视图

另外,在show函数中,else是无关的,一旦返回,您将中断函数,因此您可以执行以下操作:

if($result->num_rows() > 0 ) {
    $result['status'] = array(
        'status' => '1',
        'status_msg' => 'Record found'
    );
   //the condition is met, this will break out of the function
    return $result->row();
}
$result['status'] = array(
  'status' => '0',
   'status_msg' => 'cannot find any record.'
);
return $result->row();

在模型页面中做这些事情总是一个很好的实践

我对您所做的做了以下几点更改:

function get_pkg_length_by_id($data) 
{
    $this->db->where('id', $data['pkg_length_id']);
    $query = $this->db->get('pkg_lengths');
    /*
        Just changed var name from $result to $query
        since we have a $result var name as return var
    */
    if($result->num_rows() > 0 ) {
        $result = $query->row_array();
        /*
            $result holds the result array.
        */
        $result['status'] = array(
            'status' => '1',
            'status_msg' => 'Record found'
        );
        //return $result->row();
        /*
          This will return $result->row() only which 
          doesn't include your $result['status']
        */
    }
    else {
        $result['status'] = array(
            'status' => '0',
            'status_msg' => 'cannot find any record.'
        );
        //return $result->row();
        /*
        This is not required.
        Returning just status message is enough.
        */
    }
    return $result;
}

在模型页面中做这些事情总是一个很好的实践

我对您所做的做了以下几点更改:

function get_pkg_length_by_id($data) 
{
    $this->db->where('id', $data['pkg_length_id']);
    $query = $this->db->get('pkg_lengths');
    /*
        Just changed var name from $result to $query
        since we have a $result var name as return var
    */
    if($result->num_rows() > 0 ) {
        $result = $query->row_array();
        /*
            $result holds the result array.
        */
        $result['status'] = array(
            'status' => '1',
            'status_msg' => 'Record found'
        );
        //return $result->row();
        /*
          This will return $result->row() only which 
          doesn't include your $result['status']
        */
    }
    else {
        $result['status'] = array(
            'status' => '0',
            'status_msg' => 'cannot find any record.'
        );
        //return $result->row();
        /*
        This is not required.
        Returning just status message is enough.
        */
    }
    return $result;
}

我认为show函数应该只返回一次。使用多重返回=>意大利面代码。。。最好在if/else中设置一个变量并返回该变量。个人喜好我想…@joe42是的,这是个人喜好的问题。尽管我发现有一堆其他的代码会让事情变得更像意大利面条一样,比如:-我认为show函数应该只返回一次。使用多重返回=>意大利面代码。。。最好在if/else中设置一个变量并返回该变量。个人喜好我想…@joe42是的,这是个人喜好的问题。虽然我发现拥有一堆其他的东西会让事情变得更像意大利面代码,比如:-