如何在 codeigniter 中调用自定义助手的自定义兄弟方法并使用数据库
How to call a custom sibling method of custom helper in codeigniter and use database
编辑
<?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');
function getCrud($table_name, $subject)
{
// some code
}
function oneToMany()
{
$CI =&get_instance();
$crud = $CI->getCrud($table_name, $subject);
return $crud ;
}
试试这个
可能以下应该有效
<?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 ;
}
}
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;
}
}
编辑
<?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');
function getCrud($table_name, $subject)
{
// some code
}
function oneToMany()
{
$CI =&get_instance();
$crud = $CI->getCrud($table_name, $subject);
return $crud ;
}
试试这个
可能以下应该有效
<?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 ;
}
}
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;
}
}