ThingEngineer / PHP-MySQLi-Database-Class

Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared statements.
Other
3.29k stars 1.35k forks source link

Objects mapping: UPDATE not saved! #900

Open Padandish opened 4 years ago

Padandish commented 4 years ago

Hi dear, Thanks for nice Library.

I used Object mapping. after run code: $user = user::byId(1); $user->password = 'demo2'; $user->save();

But not saved!, I traced code, and problem at dbObject.php public function update ($data = null) { if (empty ($this->dbFields)) return false; ...}

$this->dbFields is empty and not defined.

"thingengineer/mysqli-database-class": "v2.9.3" PHP 7.4.4

Thanks

oscar-ol commented 4 years ago

the same happens to me with php 7.3. I have had to use the update method of MysqliDb.

jisheng100 commented 3 years ago

the same happens to me with php 7.1. I have had to use the update method of MysqliDb.

enrybisco commented 3 years ago

null value are not mantained by the class You can add my mods: https://github.com/ThingEngineer/PHP-MySQLi-Database-Class/pull/954

avbdr commented 3 years ago

Its explained in the readme. You need to use db->null() to insert null values

On Thu, Jul 22, 2021, 5:29 PM enrybi @.***> wrote:

null value are not mantained by the class You can add my mods:

954 https://github.com/ThingEngineer/PHP-MySQLi-Database-Class/pull/954

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ThingEngineer/PHP-MySQLi-Database-Class/issues/900#issuecomment-884959070, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZCL4XZVSP5P3ZAF64S63LTZATOHANCNFSM4ML2R2IA .

enrybisco commented 3 years ago

Here? https://github.com/ThingEngineer/PHP-MySQLi-Database-Class/blob/master/readme.md#insert-data I am not able to find that. Anyway w my mod you can send a raw insert query with ('') or ('null') when a html form input is empty. I am not deciding previously to send a null value.

BruTru commented 2 years ago

Hi Same for me. Has anyone ever seen the sample code work?

$user = user::byId(1); $user->password = 'demo2'; $user->save();

Because as @Padandish noted, the first instruction of update() is to test $this->dbFields which is not defined as a class variable and is not initialized anywhere.

The function save() can't work for an update

I started a development using this library because it looked simple.But if a function as basic as update doesn't work, I'll have to find something else.

thatwill commented 2 years ago

The example code as it stands does not work. This needs clarifying properly in the documentation, or dbFields should be made optional.

$dbFields needs to be created and set to an array of your database field names. The documentation mentions this in passing but it is easily missed, as it is not shown in the example code.

NOTE: All variables which are not defined in the $dbFields array will be ignored from insert/update statement.

It appears you do not need to set the validation rules in dbFields as suggested in the documentation - as far as I can tell, you can just set it to a flat array of field names. I might be wrong on that; I haven't tested thoroughly yet.

You can define dbFields in your class extending dbObject.

class user extends dbObject {  
  protected $dbTable='user';
  protected $dbFields=['id','password'];
}

There doesn't seem to be a way of doing this if you've defined your class using ::table() however, so it seems that save() won't work in that instance.