Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Objects mapping: UPDATE not saved! #900

Open
Padandish opened this issue Apr 19, 2020 · 7 comments
Open

Objects mapping: UPDATE not saved! #900

Padandish opened this issue Apr 19, 2020 · 7 comments

Comments

@Padandish
Copy link

Padandish commented Apr 19, 2020

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
Copy link

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

@jisheng100
Copy link

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

@enrybisco
Copy link

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

@avbdr
Copy link
Collaborator

avbdr commented Jul 22, 2021 via email

@enrybisco
Copy link

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
Copy link

BruTru commented Nov 10, 2021

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
Copy link

thatwill commented Nov 29, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants