2

我正在尝试清空表格,但我想知道我的功能是否正确?

模型:

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`
4

3 回答 3

11
/**
 * 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_table3 次:

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

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

于 2012-04-09T00:48:49.727 回答
0

在您的控制器中,您必须先加载模型(如果它没有自动加载)

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

在您使用该模型中的功能之前,就像您在控制器中使用的那样,如下所示

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

最后加载视图,即使在下一行之后也执行了所有代码

$this->quote->removeQuote();

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

于 2012-04-09T00:32:34.073 回答
0

解决方案一

$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);
于 2019-05-06T19:16:18.367 回答