DB: question about use of $this |
Hey fellows! Another silly question from a noob...
From the CI4 documentation the way of select all from a table is: PHP Code: $db = \Config\Database::connect(); 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(); But it doesn't: PHP Code: return $this->where('id',$id)->update($news); 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...
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 = [ 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.
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...
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;
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). 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!
Recovering the wasted time...
|
Welcome Guest, Not a member yet? Register Sign In |