参数太多的方法的替代方案

时间:2017-02-24 13:35:27

标签: php codeigniter

我知道这个问题已被问到不同的主题,我读了这么多,但似乎没有我想要的东西。如果您熟悉CodeIgniter,您可能知道在模型中需要一个带参数的方法,以便从控制器向数据库插入记录。在我的情况下,我想要插入太多的记录,参数似乎超出了参数的必要限制,我怀疑是否有任何但不管它看起来像一个肮脏的工作,我希望它看起来更好。如果您有任何建议或改进,请参阅下面的代码。我非常感激。谢谢。

B

提前再次感谢,因为我希望得到积极的结果。 再次感谢:)

更新:我将此public function create_cv($created_by, $fullname = '', $mobile = '', $email = '', $summary = '', $career_obj = '', $personal_skill = '',$experience1 = '', $position1 = '',$duration1 = '', $experience2 = '', $position2 = '', $duration2 = '', $experience3 = '', $position3 = '', $duration3 = '', $degree_course1 = '', $institution1 = '', $degree_duration1 = '', $degree_course2 = '', $institution2 = '',$degree_duration2 = '', $degree_course3 = '', $institution3 = '',$degree_duration3 = '', $skils = '', $linguistic_skills = '', $certificates = '', $hobbies = '', $refrees = '') { $data = array( 'created_by' => $created_by, 'fullname' => $fullname, 'mobile' => $mobile, 'email' => $email, 'summary' => $summary, 'career_obj' => $career_obj, 'perosnal_skill' => $personal_skill, 'experience1' => $experience1, 'position1' => $position1, 'duration1' => $duration1, 'experience2' => $experience2, 'position2' => $position2, 'duration2' => $duration2, 'experience3' => $experience3, 'position3' => $position3, 'duration3' => $duration3, 'degree_course1' => $degree_course1, 'institution1' => $institution1, 'degree_duration1' => $degree_duration1, 'degree_course2' => $degree_course2, 'institution2' => $institution2, 'degree_duration2' => $degree_duration2, 'degree_course3' => $degree_course3, 'institution3' => $institution3, 'degree_duration3' => $degree_duration3, 'skills' => $skils, 'linguistic_skills' => $linguistic_skills, 'certificates' => $certificates, 'hobbies' => $hobbies, 'refrees' => $refrees, ); $this->db->insert('users', $data); } 添加到代码段

更新:谢谢大家现在就把它处理好了。你的答案很有帮助。

3 个答案:

答案 0 :(得分:2)

你应该尝试那样。

public function create_cv($cvs) {
    $data = [
        'created_by' => $cvs['created_by'],
        'fullname'   => $cvs['fullname'],
        'mobile'     => $cvs['mobile']
    ];
}

就像所有记录一样。

答案 1 :(得分:1)

为什么你不能把它作为一个对象传递?

public function create_cv($obj) {
    $data = array('email' => $obj->email,
   ...);
}

这个方法被称为:

$obj = new stdClass();
// Assign properties (parameters) 
$obj->email = "test@mail.com"
// Call function
create_cv($obj);

答案 2 :(得分:0)

一种方法是

public function create_cv ( $args ) {
    $defaults = array (
         // your default values go here
        );
    $data = array_merge( $defaults, $args);
    // if you need single variables
    extract($data, EXTR_SKIP);
}