php - 如何在codeigniter中调用自定义助手的自定义同级方法并使用数据库

标签 php codeigniter codeigniter-3 crud grocery-crud

编辑

<?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。 还有其他方法吗?

return $CI->db->update($table_name, array('flag'=>'0'), array('id'=>$primary_key)); when using these line of codes in controller ($CI will be $this in controller) I am able to set the flag to 0 but here in helper its not happening

最佳答案

也许以下应该有效

<?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 ;
    }
}

关于php - 如何在codeigniter中调用自定义助手的自定义同级方法并使用数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59778193/

相关文章:

php - 使用 PHP 和 SQL 进行多重连接没有结果

codeigniter - 如何在 CodeIgniter 中获取表结构

php - 在 Codeigniter 中上传 - 不允许您尝试上传的文件类型

javascript - 提交触发器不起作用

mysql - 我想要在单个模型函数中包含一组查询,并在 Codeigniter 中进行分页

php - 在 Codeigniter 中 hook 的目的是什么?我们为什么要创造?

php - 为什么这个 php 脚本不给我发邮件?

php - 如何通过长文本并将其转换为 MySQL 的 Insert 语句

PHP 面向对象编程 : Providing Domain Entities with "Identity"

php - 如何连接两个表形成一个SQL查询?