Use Where in updateBatch (CI 4.3.3) |
I'm using CI 4.3.3 with builder for mySql.
I can't apply the where filter on the table to update with updateBatch method. I can only filter with join (onConstraint) and RawSql. It would be good to use the where method like for deleteBatch. Thanks for your great job (04-04-2023, 06:15 PM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgnit...pdatebatch Yes, I have seen the article and read the MySQLi Builder code. The where clause, in the updateBatch method, is not applied. Surely you can filter in another way...
The last sample does not meet your need?
$query = $this->db->table('user2') ->select('user2.name, user2.email, user2.country') ->join('user', 'user.email = user2.email', 'inner') ->where('user2.country', 'US'); $additionalUpdateField = ['updated_at' => new RawSql('CURRENT_TIMESTAMP')]; $sql = $builder->table('user') ->setQueryAsData($query, null, 'u') ->onConstraint('email') ->updateFields($additionalUpdateField, true) ->updateBatch(); I don't understand your use case. Can you show the SQL that you want?
It would be nice to apply the where clause directly to the table to be updated.
It would simplify the code in some cases. For example: PHP Code: $post_score = [ /* * IT COULD PRODUCE: * * UPDATE `test` * INNER JOIN ( * SELECT '22' `course_id`, '34' `question_id`, '80' `min_score` UNION ALL * SELECT '22' `course_id`, '55' `question_id`, '90' `min_score` UNION ALL * SELECT '22' `course_id`, '72' `question_id`, '120' `min_score` * ) `t` * ON `test`.`course_id` = `t`.`course_id` AND `test`.`question_id` = `t`.`question_id` * SET * `test`.`course_id` = `t`.`course_id`, * `test`.`question_id` = `t`.`question_id`, * `test`.`min_score` = `t`.`min_score` * WHERE `test`.company_id = '1' AND `test`.reg_date >= '2022-03-01' */
Just add the condition with onConstraint() it will accomplish the same thing.
/* * IT COULD PRODUCE: * * UPDATE `test` * INNER JOIN ( * SELECT '22' `course_id`, '34' `question_id`, '80' `min_score` UNION ALL * SELECT '22' `course_id`, '55' `question_id`, '90' `min_score` UNION ALL * SELECT '22' `course_id`, '72' `question_id`, '120' `min_score` * ) `t` * ON `test`.`course_id` = `t`.`course_id` AND `test`.`question_id` = `t`.`question_id` AND `test`.company_id = '1' AND `test`.reg_date >= '2022-03-01' * SET * `test`.`course_id` = `t`.`course_id`, * `test`.`question_id` = `t`.`question_id`, * `test`.`min_score` = `t`.`min_score` */ |
Welcome Guest, Not a member yet? Register Sign In |