2

I am trying to empty the tables but I was wondering have I got the function correct?

Model:

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

Controller:

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

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

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

Error:

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

DELETE FROM `companyDetails,hostingDetails,layoutDetails`

4 Answers 4

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 = '')

Apparently you can't do this

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

Instead you will have to call empty_table three times:

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

You can always hack CodeIgniter DB_active_rec.php file so that it fits your needs.

Sign up to request clarification or add additional context in comments.

1 Comment

What if I don't want to delete the auto increment value?
0

In your controller you have to load the model first (if it's not auto loaded)

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

before you use the function from that model as you used in your controller as follows

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

and load the view at last, after all code has been executed even after following line

$this->quote->removeQuote();

Just checked in CI doc empty_table doesn't accept multiple table names.

4 Comments

maybe he first loaded it in his __construct (), is there really a method called empty_tables () in the Database class of CodeIgniter?
I have got my model auto loaded and please see here: codeigniter.com/user_guide/database/active_record.html#delete
Ok, didn't know about that! but please show us your error log
Does empty_table accepts multiple table names, i didn't see it in the doc ?
0

SOLUTION ONE

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

SOLUTION TWO

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

Usage

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.