Welcome Guest, Not a member yet? Register   Sign In
Use Where in updateBatch (CI 4.3.3)
#1

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
Reply
#2

See https://codeigniter4.github.io/CodeIgnit...pdatebatch
Reply
#3

(This post was last modified: 04-14-2023, 02:14 AM by brex74.)

(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...
Reply
#4

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?
Reply
#5

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 = [
    [
        'course_id'  => '22',
        'question_id' => '34',
        'min_score'  => 80,
    ],
    [
        'course_id'  => '22',
        'question_id' => '55',
        'min_score'  => 90,
    ],
    [
        'course_id'  => '22',
        'question_id' => '72',
        'min_score'  => 120,
    ],
];

$builder->table('test')
  ->where('company_id'$company_id)
  ->where('reg_date >='$from_date)
  ->updateBatch($post_score, [course_idquestion_id]); 


/*
* 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'
*/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB