如何在 codeigniter 中调用自定义帮助程序的自定义同级方法并使用数据库

编辑


<?php   defined('BASEPATH') OR exit('No direct script access allowed');





 function getCrud($table_name, $subject)

{


    $CI =& get_instance();

    $CI->load->database();


    $crud = new grocery_Crud();

    $crud->set_table($table_name);

    $crud->set_subject($subject);

    $crud->unset_read();

    $crud->unset_clone();

    $crud->where(array($table_name.'.flag' => '1'));


    $crud->callback_delete(function ($primary_key) use ($table_name) {

        return $CI->db->update($table_name, array('flag'=>'0'), array('id'=>$primary_key));

    });


    return $crud;

}


 function oneToMany($table_name, $subject, $rel_table,$field='name')

{


    $crud = getGrocreyCrud($table_name, $subject);

    $crud->set_relation($rel_table.'_id', $rel_table, 'name', array('flag' => '1'));

    $output = $crud->render();

    return $output;

}

我无法使用ci get实例调用上述函数getCrud。还有其他方法吗?


返回 $CI->db->update($table_name, array('flag'=>'0'), array('id'=>$primary_key));当在控制器中使用这些代码行时($CI将在控制器中$this),我可以将标志设置为0,但在助手中它不会发生


函数式编程
浏览 151回答 3
3回答

繁星coding

&nbsp;class Gc_script&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public $CI;&nbsp; &nbsp; &nbsp; &nbsp; public function __construct()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->CI =& get_instance();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->CI->config->item('base_url');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public function getGrocreyCrud($table_name, $read=null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $crud = new grocery_Crud();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $crud->set_table($table_name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $crud->set_subject(ucwords(str_replace('_', ' ', $table_name)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $crud->callback_delete(function ($primary_key) use ($table_name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; $this->CI->db->update($table_name, array('flag'=>'0'), array('id'=>$primary_key));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $crud;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}

慕森卡

可能以下应该工作<?php&nbsp;defined('BASEPATH') OR exit('No direct script access allowed');if(!function_exists('getCrud')){&nbsp; &nbsp; function getCrud($table_name, $subject)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // some code&nbsp; &nbsp; }}if(!function_exists('oneToMany')){&nbsp; &nbsp; function oneToMany()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $CI =&get_instance();&nbsp; &nbsp; &nbsp; &nbsp; $crud = getCrud($table_name, $subject);&nbsp; &nbsp; &nbsp; &nbsp; return $crud ;&nbsp; &nbsp; }}

叮当猫咪

<?php&nbsp;&nbsp; &nbsp; defined('BASEPATH') OR exit('No direct script access allowed');&nbsp; &nbsp; function getCrud($table_name, $subject)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // some code&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;function oneToMany()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $CI =&get_instance();&nbsp; &nbsp; &nbsp; &nbsp; $crud = $CI->getCrud($table_name, $subject);&nbsp; &nbsp; &nbsp; &nbsp; return $crud ;&nbsp; &nbsp; }试试这个
打开App,查看更多内容
随时随地看视频慕课网APP