CI4 $this-db->Table() inside foreach or outside |
I have the following code:
PHP Code: foreach($sales_taxes as $line => $sales_tax) This will reassign the $builder in each iteration of the loop. Is that needed? I don't fully understand how and when the $builder is reset. If I were to call the below code instead would it still run an insert in each iteration? PHP Code: $builder = $this->db->Table('sales_taxes');
11-04-2021, 06:59 AM
(This post was last modified: 11-04-2021, 07:00 AM by objecttothis. Edit Reason: clarity )
@manager, a quick followup. Am I correct in my understanding then that the $builder does not need to be reinstantiated unless the table that the operation is being performed against is being changed?
For example the following code doesn't need a new `$builder = $this->db->table();` reference because the delete() is being run against the same attribute_links table? PHP Code: $builder = $this->db->table('attribute_links');
I don't understand your code , but what i can say (if i understood you correctly) is when you execute insert, update and delete operations in query builder, by default after it resets all your applied data (for example join, where, orderBy, ignore).
All this operation (delete, insert ...) will return builder instance or false if failed.
(11-04-2021, 06:24 AM)manager Wrote: Second variant is right (11-04-2021, 11:10 AM)manager Wrote: I don't understand your code , but what i can say (if i understood you correctly) is when you execute insert, update and delete operations in query builder, by default after it resets all your applied data (for example join, where, orderBy, ignore). Sorry, it probably doesn't help that there's an error in the code. I'm converting this open source application from CI3 to 4. Let me simplify it a little. PHP Code: $builder = $this->db->table('foo_table'); Am I correct that if I wanted to run the $builder->delete() against another table I would just need to place $builder = $this->db->table('bar_table'); after the $builder->get() call and before the $builder->where()? (11-04-2021, 11:31 AM)objecttothis Wrote: Yes. It's default behavior. From your example: after get() method executed "select" part is clear again, BUT "from" part of your builder is still "foo_table". So if you planning to continue to work with foo_table, you can. (11-04-2021, 11:31 AM)objecttothis Wrote: YES. In your example "where" is a clause, so it's NEED key/value pair. So the first row of code is wrong. After "where" clause and delete() method execution builder will reset "where" clause part of query. (11-04-2021, 11:31 AM)objecttothis Wrote: Am I correct that if I wanted to run the $builder->delete() against another table I would just need to place $builder = $this->db->table('bar_table'); after the $builder->get() call and before the $builder->where()? If you need to work with another table, get query builder with another name of table like $barTableBuilder = $this->db->table('bar_table');
(11-04-2021, 10:19 PM)manager Wrote: In your example "where" is a clause, so it's NEED key/value pair. So the first row of code is wrong. https://codeigniter.com/user_guide/datab...html#where According to the QueryBuilder reference, the second parameter is optional. What I have is corrected and it's the equivalent of: PHP Code: $builder->where('item_id', null); It produces the SQL "WHERE item_id IS NULL" The only downside to using it as I have it, is that it's somewhat counter-intuitive that $builder->where('item_id') produces IS NULL and not IS NOT NULL.
(11-04-2021, 10:45 PM)objecttothis Wrote:(11-04-2021, 10:19 PM)manager Wrote: In your example "where" is a clause, so it's NEED key/value pair. So the first row of code is wrong. It works but I would personally add the null parameter just for clarity of your intention. Because like @manager said, it looks like it's missing a parameter.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
The where caluse can also accept an associated array or a custom string.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |