Skip to content

Conditional column update #2

@webbby

Description

@webbby

Hey there @kfirba first let me thank you for your effort to bring such an useful tool.

May I suggest to add basic support for conditional updated columns?
I've started using your tool but soon I realised that I had to edit the generated query myself.
Here's the case:

My table had updated_at column so in my insertOrUpdate values I had to set it with the current time before the db request. The problem is the query treats it as an update value when it actually should be set only when another of the columns is updated.
On the other hand you can't excluded it because you need it to show when the update has occurred.

So I had to add the following line

on duplicate key update 
    updated_at=IF(name=VALUES(name), updated_at, VALUES(updated_at) )

right after the on duplicate key update clause before all other updating columns so that it checks the value before name is set in order to compare the values between the new value and the one of the table record. As you see the updated_at receives the new value only when name is different than the current value of the table.
Which brings the idea for conditional update columns where a conditional column is checked against change of value among other given columns like

[ 'updated_at' => ['name', 'color', ....] ] 

where it generates the following line in the query

updated_at=IF(
   name=VALUES(name) && color=VALUES(color) && ...., 
   updated_at, VALUES(updated_at) 
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions