我有多次退货时该怎么办?

时间:2015-02-14 23:50:17

标签: php codeigniter class return

我有一个问题,我的一个elseif语句有2个返回值。请看一下我的代码:

class Connection_model extends CI_Model {
    public function establish($target = 'both') {
        if ($target == 'sender' || $target == 'receiver' || $target == 'both') {
            $this->validate($target);
            if ($this->validated) {

                if ($target == 'sender') {
                    $this->propertySetter('sender');
                    return $this->load->database($this->define_database('sender'), TRUE);
                }
                elseif ($target == 'receiver') {
                    $this->propertySetter('receiver');
                    return $this->load->database($this->define_database('receiver'), TRUE);
                }
                elseif ($target == 'both') {
                    $this->propertySetter();
                    return $this->load->database($this->define_database('sender'), TRUE);
                    return $this->load->database($this->define_database('receiver'), TRUE);
                }
            }
            else {
                echo 'Oops, there is an error! For some reason the property "validated" is not returning true (Connection_model.php)';
            }
        }
        else {
            echo 'Error: illegal parameter. Please use sender, receiver or both instead.';
        }
    }
}

我想做这样的事情:

$connect = new Connection_model();
$query = $connect->establish('sender')->select('first_name, last_name')->get('person');
var_dump($query);

我不知道如何以这种方式使用它,当$ connect-> establish('both')有2个返回值时。

如果你无法理解我想要做什么,这里是完整的代码:

<?php
class Connection_model extends CI_Model {
    private $validated;
    private $sender_db;
    private $sender_host;
    private $sender_user;
    private $sender_pw;
    private $receiver_db;
    private $receiver_host;
    private $receiver_user;
    private $receiver_pw;


    private function define_database($target) {
        if ($target == 'sender') {
            $db['sender'] = array(
                    'dsn'   => '',
                    'hostname' => $this->sender_host,
                    'username' => $this->sender_user,
                    'password' => $this->sender_pw,
                    'database' => $this->sender_db,
                    'dbdriver' => 'mysqli',
                    'dbprefix' => '',
                    'pconnect' => FALSE,
                    'db_debug' => TRUE,
                    'cache_on' => FALSE,
                    'cachedir' => '',
                    'char_set' => 'utf8',
                    'dbcollat' => 'utf8_general_ci',
                    'swap_pre' => '',
                    'autoinit' => TRUE,
                    'encrypt' => FALSE,
                    'compress' => FALSE,
                    'stricton' => FALSE,
                    'failover' => array(),
                    'save_queries' => TRUE
            );
            return $db['sender'];
        }
        elseif ($target == 'receiver') {
            $db['receiver'] = array(
                    'dsn'   => '',
                    'hostname' => $this->receiver_host,
                    'username' => $this->receiver_user,
                    'password' => $this->receiver_pw,
                    'database' => $this->receiver_db,
                    'dbdriver' => 'mysqli',
                    'dbprefix' => '',
                    'pconnect' => FALSE,
                    'db_debug' => TRUE,
                    'cache_on' => FALSE,
                    'cachedir' => '',
                    'char_set' => 'utf8',
                    'dbcollat' => 'utf8_general_ci',
                    'swap_pre' => '',
                    'autoinit' => TRUE,
                    'encrypt' => FALSE,
                    'compress' => FALSE,
                    'stricton' => FALSE,
                    'failover' => array(),
                    'save_queries' => TRUE
            );
            return $db['receiver'];
        }
    }


    // Validate the connection(s)
    private function validate($target = 'both') {
        // Allowed parameters as string: sender, receiver, both 
        // Go through all parameters and define an array            
        if ($target == 'sender') {
            $sessions = array('connection', 'sender_db', 'sender_host', 'sender_user', 'sender_pw');
        }
        elseif ($target == 'receiver') {
            $sessions = array('connection', 'receiver_db', 'receiver_host', 'receiver_user', 'receiver_pw');
        }
        elseif ($target == 'both') {
            $sessions = array('connection', 'sender_db', 'sender_host', 'sender_user', 'sender_pw', 'receiver_db', 'receiver_host', 'receiver_user', 'receiver_pw');
        }
        else {
            echo 'Error: illegal parameter. Please use sender, receiver or both instead.';
        }

        // Check if all keys from the array are saved in session
        if (isset($sessions)) : 
            foreach ($sessions as $value) {
                if (key_exists($value, $this->session->get_userdata())) {                   
                    $this->validated = true;
                }
            }   
        endif;                  
    }


    // This method will set the correct properties
    private function propertySetter($target = 'both') {
        if ($target == 'sender') {
            $this->sender_db = $this->session->userdata('sender_db');
            $this->sender_host = $this->session->userdata('sender_host');
            $this->sender_user = $this->session->userdata('sender_user');
            $this->sender_pw = $this->session->userdata('sender_pw');                   
        }
        elseif ($target == 'receiver') {
            $this->receiver_db = $this->session->userdata('receiver_db');
            $this->receiver_host = $this->session->userdata('receiver_host');
            $this->receiver_user = $this->session->userdata('receiver_user');
            $this->receiver_pw = $this->session->userdata('receiver_pw');                   
        }
        elseif ($target == 'both') {
            $this->sender_db = $this->session->userdata('sender_db');
            $this->sender_host = $this->session->userdata('sender_host');
            $this->sender_user = $this->session->userdata('sender_user');
            $this->sender_pw = $this->session->userdata('sender_pw');           

            $this->receiver_db = $this->session->userdata('receiver_db');
            $this->receiver_host = $this->session->userdata('receiver_host');
            $this->receiver_user = $this->session->userdata('receiver_user');
            $this->receiver_pw = $this->session->userdata('receiver_pw');                       
        }
    }


    // Establish one or many connections
    public function establish($target = 'both') {
        if ($target == 'sender' || $target == 'receiver' || $target == 'both') {
            $this->validate($target);
            if ($this->validated) {

                if ($target == 'sender') {
                    $this->propertySetter('sender');
                    return $this->load->database($this->define_database('sender'), TRUE);
                }
                elseif ($target == 'receiver') {
                    $this->propertySetter('receiver');
                    return $this->load->database($this->define_database('receiver'), TRUE);
                }
                elseif ($target == 'both') {
                    $this->propertySetter();
                    return $this->load->database($this->define_database('sender'), TRUE);
                    return $this->load->database($this->define_database('receiver'), TRUE);
                }
            }
            else {
                echo 'Oops, there is an error! For some reason the property "validated" is not returning true (Connection_model.php)';
            }
        }
        else {
            echo 'Error: illegal parameter. Please use sender, receiver or both instead.';
        }
    }

}

2 个答案:

答案 0 :(得分:0)

您可能想要返回一个数组:

return array('sender' => $this->load->database($this->define_database('sender'), TRUE),
             'receiver' => $this->load->database($this->define_database('receiver'), TRUE)
);

通过电话,您将拥有$query['sender']$query['receiver']等数组:

$query = $connect->establish('sender')->select('first_name, last_name')->get('person');

或者您可以使用list

list($sender, $receiver) = $connect->establish('both')->select('first_name, last_name')->get('person');

答案 1 :(得分:0)

在您的示例中,elseif ($target == 'both')仅返回发件人。一旦你回来,你就不能再回来了!像AbraCadaver建议:返回一个数组。在您的示例中看起来像:

    public function establish($target = 'both') {
        if ($target == 'sender' || $target == 'receiver' || $target == 'both') {
            $this->validate($target);
            if ($this->validated) {
                if ($target == 'sender') {
                    $this->propertySetter('sender');
                    return $this->load->database($this->define_database('sender'), TRUE);
                } elseif ($target == 'receiver') {
                    $this->propertySetter('receiver');
                    return $this->load->database($this->define_database('receiver'), TRUE);
                } elseif ($target == 'both') {
                    $this->propertySetter();
                    return array(
                        $this->load->database($this->define_database('sender'), TRUE),
                        $this->load->database($this->define_database('receiver'), TRUE)
                    );
                }
            } else {
                echo 'Oops, there is an error! For some reason the property "validated" is not returning true (Connection_model.php)';
            }
        } else {
            echo 'Error: illegal parameter. Please use sender, receiver or both instead.';
        }
    }