PHP-MySQLi-Database-Class icon indicating copy to clipboard operation
PHP-MySQLi-Database-Class copied to clipboard

Objects mapping: UPDATE not saved!

Open Padandish opened this issue 4 years ago • 7 comments

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

Padandish avatar Apr 19 '20 15:04 Padandish

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

oscar-ol avatar Jul 16 '20 08:07 oscar-ol

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

jisheng100 avatar Nov 26 '20 05:11 jisheng100

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

enrybisco avatar Jul 22 '21 14:07 enrybisco

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 .

avbdr avatar Jul 22 '21 14:07 avbdr

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.

enrybisco avatar Jul 23 '21 07:07 enrybisco

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.

BruTru avatar Nov 10 '21 15:11 BruTru

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.

thatwill avatar Nov 29 '21 17:11 thatwill