0

I'm so confused...

This function runs OK and doesn't throw up any errors... but it doesn't update the row in my table.

Where have I gone wrong?

Controller:

    public function update()
    {     
        $model = new StoreModel();
      
        $model->where('merchant', 'Klip Shop')->set('availability', 'out of stock')->update();
        
    }

Model:

class StoreModel extends Model
{
    protected $table = 'shop';
}
seixwebdev
  • 111
  • 2
  • 4
  • 14

2 Answers2

1

It was an $allowedFields issue with the model, hence nothing was 'allowed' to be updated.

seixwebdev
  • 111
  • 2
  • 4
  • 14
0

Since $model->where('merchant', 'Klip Shop')->get() doesn't return anything, it means that you don't have any data you can update.

You need to insert a row first.

  1. you could use the insert() function, if there is no rows detected with the merchant name

or

  1. there is the save() function in CI 4.x which either inserts or updates on duplicate. but to get it to work, you need to set merchant to unique or use a column with an unique key

please read: Saving Data in CI 4.x

edit: you actually got me confused too:

only now I analized your MCV code:

you should have a controller

public function update()
{     
    $model = new StoreModel();
    $model->update();
}  

    

and a model:

class StoreModel extends Model
{
    $db      = \Config\Database::connect();
    $model= $db->table('shop');

    function update(){
        $model->where('merchant', 'Klip Shop')->set('availability', 'out of stock')->update();
    }
}
Vickel
  • 7,879
  • 6
  • 35
  • 56
  • [@Vickel](https://stackoverflow.com/users/2275490/vickel) THAT'S why I'm so confused... there's literally THOUSANDS of matching entries in my database to update. No spelling mistakes or anything. – seixwebdev Sep 16 '20 at 18:23
  • can you create a small sqlfiddle? something like: http://sqlfiddle.com/#!9/a6c585/1 – Vickel Sep 16 '20 at 18:30
  • [@Vickel](https://stackoverflow.com/users/2275490/vickel) This is it: http://sqlfiddle.com/#!9/bf443f Thank you for all your help! – seixwebdev Sep 16 '20 at 18:52
  • check http://sqlfiddle.com/#!9/52bc7f/1: the left hand panel now updates your existing merchant, if you change the merchant's name to one, which doesn't exist, it cannot update and give an error, therefor the CI 4.x save() function, which practically generates this SQL: https://stackoverflow.com/a/14383597/2275490 – Vickel Sep 16 '20 at 19:10
  • [@Vickel](https://stackoverflow.com/users/2275490/vickel) Thank you for all the time you’re spending helping me with this. It’s really appreciated! Isn’t what you’ve coded in MySQL the same as what I originally coded for my Controller though? What I’ve coded for my Controller isn’t updating the data but it looks like the same logic as yours... What do I need to change in my Controller to do the update? – seixwebdev Sep 16 '20 at 19:33
  • I've added the line `UPDATE shop SET availability = 'not in stock' WHERE merchant = 'Notino.co.uk'`at the left hand side, therefore if you run the query (Run SQL) you'll see the updated output. But yes it is following the sample you gave. Use `echo $model->getCompiledSelect();` and see the generated SQL – Vickel Sep 16 '20 at 19:39
  • [@Vickel]() Isn't this (your code): `UPDATE shop SET availability = 'not in stock' WHERE merchant = 'Notino.co.uk'` the same as (my code): `$model->where('merchant', 'Notino.co.uk')->set('availability', 'not in stock')->update();`? The latter isn't updating my database. I understand the MySQL code/logic behind what I'm trying to do, but I need to do it in a Query Builder Class function in a Controller as it's not a one-off task. – seixwebdev Sep 16 '20 at 19:54
  • no, because you query for "Klip Shop", with ` $model->where('merchant', 'Klip Shop')->set('availability', 'out of stock')->update();` and not for Notino.co.uk – Vickel Sep 16 '20 at 20:25