Welcome Guest, Not a member yet? Register   Sign In
DB: question about use of $this
#1

(This post was last modified: 04-26-2020, 08:22 AM by YanKleber.)

Hey fellows! Another silly question from a noob...  Tongue

From the CI4 documentation the way of select all from a table is:

PHP Code:
$db = \Config\Database::connect();
$builder $db->table('news');
return 
$builder->get()->getResult(); 

I am superfine with it, works perfectly.

Now I have found a very short way of doing it by simply suppressing all the $builder thing and using $this instead:

PHP Code:
return $this->get()->getResult(); 

This is really great because CI picks the necessary DB and table info from .env file and the table name from $table definition in my Model class!

However I can NOT understand why the same principle won't work here. See, it works:

PHP Code:
$db = \Config\Database::connect();
$builder $db->table('news');
$builder->where('id',$id);
return 
$builder->update($news); 

But it doesn't:

PHP Code:
return $this->where('id',$id)->update($news);
// throws the error 'There is no data to update.' 

I don't see why it won't work. In both cases $news is the array containing the data and $id is the record primary key, so there IS data!

Any idea?

Thanks!
Recovering the wasted time...
Reply
#2

(This post was last modified: 04-26-2020, 10:54 AM by jreklund.)

The Model differs somewhat from the query builder, making things even easier.
https://codeigniter.com/user_guide/model...aving-data

This should be run from the Controller.
PHP Code:
$data = [
        
'username' => 'darth',
        
'email'    => '[email protected]'
];

$userModel = new UserModel();
$userModel->update($id$data); 

You don't need to make a custom update method. It's already built in. All those commands that I linked to should be run from the Controller or where you have initiated the Model, like in a library.

Have a look at save(), it takes care of both insert and update automatically, and are the preferred method.
Reply
#3

I see now the difference - I didn't realize that Model class have its own methods to handle DB.

However, the example you provided to me (that is the same in the documentation) didn't work. Now the error message is:

Code:
You must use the "set" method to update an entry.

I tried something like:

Code:
$modelo->set($dados)->update($this->request->getVar('id'),$dados);

But it still keep throwing the same error. I tried to search for a solution for this issue but I always fall on old CI3 threads.

Any idea?
Recovering the wasted time...
Reply
#4

Don't know what you are passing in, but I can update my table just fine. This are the complete code, that's not giving me any errors.

PHP Code:
<?php namespace App\Controllers;

use 
App\Models\WordsModel;

class 
Words extends BaseController
{
    public function 
index()
    {
        
$model = new WordsModel();
        
$model->update(1, ['word' => 'Update CodeIgniter']);
    }
}

<?
php namespace App\Models;

use 
CodeIgniter\Model;

class 
WordsModel extends Model
{
    protected 
$table 'words';
    protected 
$allowedFields = ['word'];

    public function 
getWords()
    {
        return 
$this->select('word')->distinct()->findAll();
    }

    public function 
getCount()
    {
        return 
$this->countAll();
    }

Reply
#5

Okay, I figured what was going on.

It was a stupid mistake. The code was alright, but the list of field names in $allowedFields array was with the first letter uppercase.

When I wrote down the field names I may have presumed that once mysql treats everything as lowercase CI would automatically parse the names and convert it to all lowercase (exactly how MySQL does when we try to create columns with anything that is not lowercase).

Confused

Anyway, I revised all my code and replaced the update and insert with save... yeah, too much better!

Also, I have to agree that is too much more straight forward to have the DB methods called directly from the controller although I have to confess that from MVC perspective it still looks wrong to me.

Another odd thing to me is that CI models are very loose looking more like config files. I think my confusion is because in classic OOP we are indoctrinated to declare all the entity attributes, getters & setters and the CRUD methods into the class before to be allowed to call it a MODEL!

Anyway, thanks for all your patience, I have learned several valuable stuff in CodeIgniter today!

Smile
Recovering the wasted time...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB