Update using WHERE |
I searched for this topic and didn't get any hits.
I am trying to update multiple rows with the query helper. There is a column called activity_id which is NOT the primary key. I am trying to make all the rows that have a specific activity_id to be null so they are unassigned in my program. Here is a function in my model PHP Code: public function set_activity_id_null($activity_id = FALSE) What I am expecting to happen: UPDATE `billing_charges_2` SET activity_id = null WHERE activity_id = 8518 But codeigniter is telling me "no data to update" I know I am missing something.
Like this?
PHP Code: public function set_activity_id_null($activity_id = FALSE) this explodes codeigniter even worse. Thanks for trying though. As a temporary solution I made it a simple query PHP Code: $this->db->query('UPDATE '.$this->table.' SET activity_id = null WHERE activity_id = '.$activity_id); Seems to work. (01-11-2022, 02:47 PM)iRedds Wrote: https://codeigniter.com/user_guide/model...aving-data Thanks, but again nothing on there explains my situation. Closest thing is this: PHP Code: $userModel Where you can set multiple rows, but you have to pass all the ids in. In my situation I would have to query the db for all the rows where activity_id = X Then convert that to an array of the ids and then do the above query. Thanks though.
01-11-2022, 08:06 PM
(This post was last modified: 01-11-2022, 08:08 PM by BilltheCat. Edit Reason: Removed the unused $data array )
From your expectation above;
Code: UPDATE `billing_charges_2` Have you tried it this way? PHP Code: public function set_activity_id_null($activity_id = FALSE)
I was so sure that was going to work. Totally makes sense... but... same error:
"There is no data to update" Wild. I appreciate your efforts! Thank you.
Did you set $allowedFields in your model?
https://codeigniter.com/user_guide/model...your-model $allowedFields
Yes. All fields are in $allowedFields.
Thanks for helping. I really think Ci4 update() only wants to update with a primary key. Anything outside of that, it explodes. (01-14-2022, 05:09 PM)kenjis Wrote: Did you set $allowedFields in your model?
(01-20-2022, 08:02 AM)sevmusic Wrote: Yes. All fields are in $allowedFields. There's an easy way to prove/disprove your theory.... PHP Code: echo $this->where('activity_id', $activity_id)->getCompiledSelect(); |
Welcome Guest, Not a member yet? Register Sign In |