Digital Owl's Prose

CodeIgniter 4 Query Builder set() function with update()

Advertisements

CodeIgniter 4 Query Builder class has an update() function used to process UPDATE Data Manipulation Language (DML) commands. Using update() as a standalone function call is perfectly valid. However, there is also a set() function used for setting column values as you would with the SET keyword in an SQL UPDATE statement. Used in conjunction with the Query Builder where() function, you can easily UPDATE column values for an individual row or multiple rows. Continue reading for more information…

Image by OpenIcons from Pixabay 

Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


Code, content, and community for developers.

The LAMP stack and the PHP technologies and frameworks it runs.

I’m using a simple ‘employees‘ table (MySQL) having this fictitious data for the examples:

All employee information.

I have this basic EmployeeModel with an all_emps() method which provides all the data from the ’employees’ table as shown in the screenshot above:

Query Builder set(), update, and where() functions

I’ve created this updateEmpLastName() method in the Model and use the Query Builder set(), update() and where() functions to edit an employee’s last name:

Then we can call the model updateEmpLastName() method in a Controller method I have created named, updateLastName():

You can see in the screenshot below, after calling the Controller updateLastName() method, employee ‘Jessica Rabbit’ now has the last name of ‘Jones’:

Updated the last_name column using Query Builder set(), where(), and update() functions.

If you find value in the content on my blog and want to support my work, you can toss some spare change in my Tip Jar. Thank you so much!!!


Query Builder set() function – multiple calls through chaining

Multiple calls to the Query Builder set() function are possible by chaining them one after the other, allowing you to set values on multiple columns at one time. In this next Model method, updateEmpFirstAndLastName(), I chain 2 Query Builder set() functions together; one for the ‘first_name’ column value and one for the ‘last_name’ column:

Again, focussing on fictional employee 3, ‘Jessica Jones’, I update both ‘first_name’ and ‘last_name’ columns with new data. Results are shown in the follow-up screenshot:

Updated the first_name and last_name columns using Query Builder set(), update(), and where() functions.

Are you a Medium member? If so, receive an email notification each time I publish a blog post if you prefer the Medium platform. Not a member? No worries! Use my sign-up link (I will get a commission at no extra cost to you) and join. I really enjoy reading all the great content there and I know you will too!!!


CodeIgniter 4 Query Builder informational resources

Visit the official CodeIgniter 4 documentation on any of the functions covered in the post:


As always, if you have any questions or see any mistakes in the code, please let me know via the comments. Constructive comments help me provide accurate blog posts and are much appreciated. Thank you for reading.

Closing

The Query Builder set() function can be used with insert() function calls as well and I plan to cover that use in a future blog post. Be sure to subscribe to my free newsletter, OpenLamp.tech, the newsletter for PHP/MySQL developers, and stay up to date on PHP and SQL-related content.

Thank you for reading this post. Please share it with someone else who would enjoy it as well.


Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.

Code, content, and community for developers.

The LAMP stack and the PHP technologies and frameworks it runs.

More ways I can help

Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

Code, content, and community for developers.

The LAMP stack and the PHP technologies and frameworks it runs.

Exit mobile version