Codeigniter ->使用empty_table的函数

yyhrrdl8  于 2023-04-09  发布在  其他
关注(0)|答案(4)|浏览(156)

我试图清空table,但我想知道我得到的功能是否正确?

型号:

public function removeQuote()
    {
        $this->db->empty_table('companyDetails,hostingDetails,layoutDetails');
    }

控制器:

public function submit()
{
        $data['companyContact'] = $this->quote->getCompanyDetails()->companyContact;

        $this->load->view('submit',$data);

        $this->quote->removeQuote();
}

错误:

Table '_quote.companyDetails,hostingDetails,layoutDetails' doesn't exist

DELETE FROM `companyDetails,hostingDetails,layoutDetails`
6za6bjd0

6za6bjd01#

/**
 * Empty Table
 *
 * Compiles a delete string and runs "DELETE FROM table"
 *
 * @param   string  the table to empty
 * @return  object
 */
public function empty_table($table = '')

显然你不能这么做

$this->db->empty_table('companyDetails,hostingDetails,layoutDetails');

相反,您需要调用empty_table三次:

$this->db->empty_table('companyDetails');
$this->db->empty_table('hostingDetails');
$this->db->empty_table('layoutDetails');

您可以随时破解CodeIgniter DB_active_rec.php文件,以便它符合您的需求。

ttvkxqim

ttvkxqim2#

在你的控制器中,你必须首先加载模型(如果它不是自动加载的)

$this->load->model('quote'); // Assuming your model name is 'quote'

在使用该模型中的函数之前,请按如下所示在控制器中使用该函数

$data['companyContact'] = $this->quote->getCompanyDetails()->companyContact;

并在所有代码执行完后加载视图,即使在以下行之后

$this->quote->removeQuote();

刚刚签入CI文档empty_table不接受多个表名。

v1uwarro

v1uwarro3#

方案一

$this->db->truncate('companyDetails');
$this->db->truncate('hostingDetails');
$this->db->truncate('layoutDetails');

方案二

function emptytablesbycomma($stringoftables) {
            $array_tablenames = explode(",", $stringoftables);
            if (!empty($array_tablenames)) {
                foreach ($array_tablenames as $tablename) {
                    $this->db->truncate($tablename);
                }
            }
        }

用途

$stringoftables='companyDetails,hostingDetails,layoutDetails';
$this->emptytablesbycomma($stringoftables);
a8jjtwal

a8jjtwal4#

$tablelist = array("companyDetails,hostingDetails,layoutDetails");
        foreach ($tablelist as $able) {
            $this->db->truncate($able);
        }

相关问题