为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
函数式编程 2022-08-05 16:05:50
编辑<?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,但在助手中它不会发生
查看完整描述

3 回答

?
繁星coding

TA贡献1797条经验 获得超4个赞

 class Gc_script

    {

        public $CI;

        public function __construct()

        {

            $this->CI =& get_instance();

            $this->CI->config->item('base_url');

        }


        public function getGrocreyCrud($table_name, $read=null)

        {

            $crud = new grocery_Crud();

            $crud->set_table($table_name);

            $crud->set_subject(ucwords(str_replace('_', ' ', $table_name)));


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

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

            });


            return $crud;

        }

   }


查看完整回答
反对 回复 2022-08-05
?
慕森卡

TA贡献1806条经验 获得超8个赞

可能以下应该工作


<?php 

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



if(!function_exists('getCrud'))

{

    function getCrud($table_name, $subject)

    {

        // some code

    }

}


if(!function_exists('oneToMany'))

{

    function oneToMany()

    {

        $CI =&get_instance();


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

        return $crud ;

    }

}


查看完整回答
反对 回复 2022-08-05
?
叮当猫咪

TA贡献1776条经验 获得超12个赞

<?php 

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



    function getCrud($table_name, $subject)

    {

          // some code

    }


     function oneToMany()

    {

        $CI =&get_instance();


        $crud = $CI->getCrud($table_name, $subject);

        return $crud ;

    }

试试这个


查看完整回答
反对 回复 2022-08-05
  • 3 回答
  • 0 关注
  • 150 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号