Codeigniter $ this-> db-> join is used with $ this-> db-> update

I just realized that you cannot use:

$this->db->join() 

with

 $this->db->update() 

It seems that the "join" is executed using codeigniter in order, but is not used in the query observed after the update in the base table obtained with: $ this-> db-> last_query ();

I saw that there was no connection. Then I tried updating in the joined table, believing that the connection would only be used if necessary, but I didnโ€™t work and told me error 1054 "Unknown XXX column in the where section".

Is there a way to force codeigniter? The way I built my software, I DO NOT really want to create all the different parts of the queries (join, where) and calling $ this-> db-> query () myself.

NOTE. I saw these links:

Operation of updating an active Codeigniter record with attachment

Is it possible to UPDATE a JOINed table using an active Codeigniter record?

codeigniter - database: how to update multiple tables with a single update request

but if someone knows a cleaner way, it would be good because these solutions do not work with my case, since I used the same connections in the preProcessing () method, which uses joins to preview changes, then the same "preProcessing method () "is used to replace

+4
source share
1 answer

Well, I managed to find a "clean" solution using a code connection, install, etc. So it's great that you will have all the benefits of CI when using $ this-> db-> join (), $ this-> db-> join (), etc., for example, escaping and adding quotes.

So first do all your CI stuff:

 $this->db->join(..) // Set all your JOINs $this->db->set(..) // Set your SET data $this->db->where(..) // Set all your WHEREs 

Then you can create a query using the predefined, cleared, and shielded Active Record query elements:

 // JOIN $sql = "UPDATE $this->baseTable "; $sql .= implode(' ', $this->db->ar_join); // SET $sql .= ' SET'; $setArray = array(); foreach ($this->db->ar_set as $column=>$newValue) array_push($setArray, " $column = $newValue"); $sql .= implode(',', $setArray); // WHERE $sql .= ' WHERE '.implode(' ', $this->db->ar_where); $this->db->query($sql); 

If anyone has a better solution, I will gladly accept it and use it

+1
source

Source: https://habr.com/ru/post/1484548/


All Articles