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

Would "update" inserts empty values? #25

Closed
elieobeid7 opened this issue Dec 29, 2017 · 10 comments
Closed

Would "update" inserts empty values? #25

elieobeid7 opened this issue Dec 29, 2017 · 10 comments

Comments

@elieobeid7
Copy link

To insert I do

$user = $db->user->create($_POST['data']);

Would that work for update too? I mean if there's an empty values in the form, would it override the existent values or would it ignore the empty values and insert what's not empty? How to update none empty values by doing the same thing as insert?

@oscarotero
Copy link
Owner

There's no distinction between empty values and non-empty values. If you update a row with empty values, that will be saved in the database.
If you want to update a row, just select the row and update the values. And to use only the non-empty values, you can filter the array:

$user = $db->user->select()
    ->one()
    ->byId($_POST['data']['id'])
    ->run();

if ($user) {
    $data = array_filter($_POST['data']); //discard empty values
    $user->edit($data); //edit the existing user
} else {
    $user = $db->user->create($_POST['data']); //create the user
}
$user->save(); //save data

@elieobeid7
Copy link
Author

elieobeid7 commented Dec 29, 2017

Great! what's the difference between $user->edit and $user->update ? I haven't seen that before.

Man each time I ask you a question; you show something new about this library, I wish I have the time to go through it line by line and write a complete documentation.

@oscarotero
Copy link
Owner

$user->edit() is a method of a row, and only edit its values (but does not save it in the database). It's the equivalent to:

foreach ($data as $field => $value) {
    $user->$field = $value;
}

$user->update() is a method of the user table, and generates a new update query (in the same way that you can generate a select, delete or insert query).

@elieobeid7
Copy link
Author

elieobeid7 commented Jan 21, 2018

if data is related to another field, you edit, should you do

$user->relate($company);

or is relate not important in update and edit since the data exists and already related?

And one more thing I keep getting

Fatal error: Uncaught SimpleCrud\SimpleCrudException: Query 'Edit' not found in D:\xampp\htdocs\web\diaspora\common\vendor\simple-crud\simple-crud\src\QueryFactory.php:43 Stack trace: #0 D:\xampp\htdocs\web\diaspora\common\vendor\simple-crud\simple-crud\src\Table.php(111): SimpleCrud\QueryFactory->get(Object(SimpleCrud\Table), 'Edit') #1 D:\xampp\htdocs\web\diaspora\edit_user_ctrl.php(51): SimpleCrud\Table->__call('edit', Array) #2 {main} thrown in D:\xampp\htdocs\web\diaspora\common\vendor\simple-crud\simple-crud\src\QueryFactory.php on line 43

Query not found, strange

@oscarotero
Copy link
Owner

If the data is already related, you don't need to relate it again. $user->relate($company) only fill the user_id field with the company id.

About the exception, you're executing edit() in a table and as said in previous comment, edit() is a row function, not a table query.

//You can do this:
$user = $db->user[4];
$user->edit($newData);
$user->save();

//Or this
$db->update()
  ->data($newData)
  ->byId(4)
  ->run();

@elieobeid7
Copy link
Author

elieobeid7 commented Jan 25, 2018

Just want to ask a small question, no need to create new issue. Is there a function that would throw an error if something goes wrong during the CRUD?

or is something like this enough?

if ($user) echo 'success';
else echo "something went wrong, please try to insert again";

The reason is I have to notify the user in case his data wasn't inserted, deleted whatever.

@oscarotero
Copy link
Owner

If something is wrong, an exception is throw. Isn't it enought?

@elieobeid7
Copy link
Author

Ok then I'll do it in a try catch. Thank you

@elieobeid7
Copy link
Author

I can use normal pdo exceptions? for instance to check if someone is trying to duplicates in unique field that would be

catch (Exception $e) {
    if ($e->errorInfo[1] == 1062) {
        //The INSERT query failed due to a key constraint violation.
    }
}

And where does Logger::log($message); writes the log file? The default logging path of php.ini? is it a part of the library the logger? Because it's not php's default logger.

@oscarotero
Copy link
Owner

I can use normal pdo exceptions? for instance to check if someone is trying to duplicates in unique field that would be

Why don't you check this before asking?

And where does Logger::log($message); writes the log file?

That is just an example of a hypothetical Logger. There's no logger provided in this package.

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

2 participants