Welcome Guest, Not a member yet? Register   Sign In
Update using WHERE
#11

We had better clarify everything. 

So, in your controller : 


Code:
$activity_id = xyz-whatever;
$data = [
   'activity_id' => NULL
];

$this->YourModel->lets_update_it($data, $activity_id);

Your model : 


Code:
public function lets_update_id($data, $activity_id){
$db      = \Config\Database::connect();
$builder = $db->table('your_table_name');
$query = $builder->where('activity_id', $activity_id)
->update($data);
}

That should work.
Reply
#12

(This post was last modified: 01-20-2022, 01:30 PM by sevmusic.)

(01-20-2022, 08:38 AM)BilltheCat Wrote:
(01-20-2022, 08:02 AM)sevmusic Wrote: 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?
https://codeigniter.com/user_guide/model...your-model
$allowedFields


There's an easy way to prove/disprove your theory....
PHP Code:
echo $this->where('activity_id'$activity_id)->getCompiledSelect(); 


It gave me this:

Quote:SELECT * FROM `billing_charges_2` WHERE `activity_id` = 8583

(01-20-2022, 11:53 AM)demyr Wrote: We had better clarify everything. 

So, in your controller : 


Code:
$activity_id = xyz-whatever;
$data = [
   'activity_id' => NULL
];

$this->YourModel->lets_update_it($data, $activity_id);

Your model : 


Code:
public function lets_update_id($data, $activity_id){
$db      = \Config\Database::connect();
$builder = $db->table('your_table_name');
$query = $builder->where('activity_id', $activity_id)
->update($data);
}

That should work.


YOU NAILED IT!

This query builder has been the most confusing part of Ci4. All my model functions (except for this one) works as expected.

I don't care why at this point.

THANK YOU!!!!!
Reply
#13

(This post was last modified: 01-20-2022, 02:10 PM by demyr.)

Glad to see that it worked @savemusic. Happy for you.

Actually, it is not so confusing. Try to do most of your work not in modals. Controllers or Libraries are better places to prepare your data, then send them to modals for the final touch.
Reply
#14

This code worked.
Result: UPDATE `test` SET `activity_id` = NULL WHERE `activity_id` = 100

Model:
PHP Code:
<?php
namespace App\Models;
use 
CodeIgniter\Model;

class 
TestModel extends Model
{
    protected $table         'test';
    protected $allowedFields = ['activity_id'];

    public function set_activity_id_null($activity_id false)
    {
        if ($activity_id !== false) {
            $this->where('activity_id'$activity_id)
                ->set('activity_id'null)
                ->update();

            return true;
        }

        return false;
    }


Controller:
PHP Code:
<?php
namespace App\Controllers;

class 
Home extends BaseController
{
    public function index()
    {
        $model = new \App\Models\TestModel();
        $model->set_activity_id_null(100);
        echo $model->getLastQuery();
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB