CodeIgniter Forums
$this->setTable() or $this->table not sticking - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: $this->setTable() or $this->table not sticking (/showthread.php?tid=78857)



$this->setTable() or $this->table not sticking - Kaosweaver - 03-19-2021

Ok, so, I've got a dynamic database setup where tickets are put in the tables and the tables can be created on the fly (as well as columns) - so I'm setting up the database model to handle this based upon incoming IDs.
Model code:
PHP Code:
    protected $table 'TN';
    protected $DBGroup 'protoEngine';

    public function getTickets(){
        echo $this->table.": enter<br>";
        $this->setTable($this->table);
        echo $this->table.": change 2<br>";
        $a$this->findAll(10,0);
        echo $this->table.": post query<br>";
        echo $this->getLastQuery();
        echo "<hr>";
        print_r($a);
        die();
    }

    public function changeTable($queueID) {

        echo $this->table.": start<br>";
        $this->setTable("ticketTypeData");
        echo $this->table.": change<br>";
        $row $this->where(['queueID'=>$queueID])->first();
        echo $this->table.": query<br>";
        $this->setTable($row['ticketIDprefix']);
        echo $this->table.": query change<br>";
        $this->table $row['ticketIDprefix'];
        echo $this->table.": direct assignment<br>";
        return ['ticketTypeName'=>$row['ticketTypeName'], 'ticketTypeDescription'=>$row['ticketTypeDescription']];
    
controller code:
PHP Code:
     $model2 = new Protoengine();
     $tableInfo $model2->changeTable(48); 
(the code is, obviously, peppered with debugging stuff)
The output is:

Code:
TN: start
ticketTypeData: change
ticketTypeData: query
PH: query change
PH: direct assignment
PH: enter
PH: change 2
PH: post query
SELECT * FROM `ticketTypeData` LIMIT 10

The model sees the change from TN to ticketTypeData to PH (as it should) - however the query in the getTickets() function should be calling the PH table and not the ticketTypeData table.

why?


RE: $this->setTable() or $this->table not sticking - InsiteFX - 03-19-2021

When working with multiple tables you should always call.

PHP Code:
$this->table->clear(); 

Before setting up a table again.


RE: $this->setTable() or $this->table not sticking - Kaosweaver - 03-22-2021

(03-19-2021, 11:21 AM)InsiteFX Wrote: When working with multiple tables you should always call.

PHP Code:
$this->table->clear(); 

Before setting up a table again.

Tried it two ways:
PHP Code:
$this->table->clear(); 

resulted in:
Code:
Call to a member function clear() on string
(which makes sense, $this->table is a string)

and:

PHP Code:
$this->clear(); 

resulted in:
Code:
BadMethodCallException

I looked for the clear() function in the database system folder, didn't find it. I found a clear() function in the email and the table array parts of the system folder, but nothing under database.

So, still looking for an answer to this if anyone has any ideas.

I added:

PHP Code:
$this->from($this->tabletrue); 

After the setTable for the new table and it did the job. The setTable function *should* be doing this in the set table in the framework as I can't see a logical reason to not have the table change when a setTable function is called.


RE: $this->setTable() or $this->table not sticking - InsiteFX - 03-29-2021

The clear method is part of the table library, you can find it there.


RE: $this->setTable() or $this->table not sticking - Marshall - 07-15-2022

@Kaosweaver
I have the same problem, did you find any solution?
Did not found any Database ‚clear‘ method - i guess thats not available. Worst case scenario is to query the results without any ‚magic‘ models.