Jess McKenzie
Jess McKenzie

Reputation: 8385

Codeigniter -> function using empty_table

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`

Upvotes: 2

Views: 13481

Answers (4)

Shivendra Kr. Sahu
Shivendra Kr. Sahu

Reputation: 1408

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

Upvotes: 0

joash
joash

Reputation: 2323

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);

Upvotes: 0

The Alpha
The Alpha

Reputation: 146191

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.

Upvotes: 0

jmic
jmic

Reputation: 897

/**
 * 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.

Upvotes: 11

Related Questions