Save Model Values Without Null - mysql

Everytime i try attempt to update a row i receive an error which says "something is required". In codeigniter you can update rows without the need to set everything to null in the mysql tabel settings.
I just want to update one value not the entire row.
Is this possible?
if ($users->save() == false) {
echo "Umh, We can't update the user right now: \n";
foreach ($users->getMessages() as $message) {
echo $message, "<br>";
}
$this->flash->error("Error in updating information.");
$this->response->redirect('user/profile');
} else {
echo "Great, a new robot was saved successfully!";
$this->flash->success("Member has been updaed successfully.");
//$this->response->redirect('user/profile');
}

Your isseue happens because you have already filled table and not yet properly defined model. Phalcon is validating all fo model data BEFORE trying to save it. If you define your model with all defaults, skips etc. properly, updates will be fired on single columns as you wish.
If you have definitions, that does not allow nulls, but you need an empty or default value there anyway, you may want to use 'beforeCreate' actions in model implementations. Also if there are things with defaults to set on first insert, you may wanto to use skipAttributes method.
More information is in documentation: Working with Models. So far best bit over internet I've found.
Also, below is an example for nullable email column and NOT NULL DEFAULT '0' 'skipped' column from my working code:
public function initialize() {
$this->skipAttributesOnCreate(['skipped']);
}
public function validation()
{
if($this->email !== null) {
$this->validate(
new Email(
array(
'field' => 'email',
'required' => true,
)
)
);
if ($this->validationHasFailed() == true) {
return false;
}
}
}
You do want errors of "something is required". All you're missing are just proper implementations of defaults over models. Once you get used to those mechanics, you should find them easy to handle and with more pros than cons.

What you are doing is called an insert. To set a column to a different value in a pre-existing row is called an update.
The latter is flexible, the former in not.
I highly recommend not treating a database like this is what i feel like
Put all the data in. Null is your enemy

Related

Failed DB update requests with Laravel

After spending numerous hours on this and scouring the answers online, I'm royally stuck on a seemingly simple DB update with Laravel. Here is a (simplified) version of the flawed function in one of my controllers:
public function changeDetails(Request $request)
{
...
// It works to change member_id into another...
if (Request::input('recipient_id') != "null") {
DB::table('recipients')
->where('recipients.id','=',Request::input('recipient_id'))
->update([
'recipients.member_id' => Request::input('member_id')
]);
// ...but it won't let me change it to NULL.
} else {
DB::table('recipients')
->where('recipients.id','=',Request::input('recipient_id'))
->update([
'recipients.member_id' => null
]);
};
I initially thought that the issue had to do with the database or table, especially since the 'member_id' is a foreign key. However, I did two tests that prove otherwise. First, I made sure that the column is 'unsigned' and 'nullable'. Second, I manually inserted an integer into the "where" clause instead of the "Request::input('recipient_id')"... and it worked fine. I also confirmed that the value of "Request::input('recipient_id')" is indeed an integer, which should work within the appropriate column (that is 'bigint' type).
Any useful suggestions/observations? They would be greatly appreciated.
The code runs into else statement only when Request::input('recipient_id') is "null",
but your else statement also uses 'recipient_id' which is "null".
So your SQL statement will find a recipients where recipients.id = "null".
Obviously you don't have any "recipient_id" is "null", right?
...
else {
DB::table('recipients')
->where('recipients.id','=',Request::input('recipient_id')) // here the recipient_id is "null"
->update([
'recipients.member_id' => null
]);
};
...

Trying to update a column in database but NOT NULL attributes are not letting me update the values. I am using Laravel

I am trying to update the column with some other data but database table is not letting me update the table because of the NOT NULL constraints in it. I have this option of setting all the fields to NULL but I dont think that will be a good practice. Please I need a solution to it if anyone can help. I get the following error
Illuminate \ Database \ QueryException (HY000)
SQLSTATE[HY000]: General error: 1364 Field 'first_name' doesn't have a default value (SQL: insert into users (subject_id, updated_at, created_at) values (?, 2019-07-30 13:46:42, 2019-07-30 13:46:42))
Previous exceptions
SQLSTATE[HY000]: General error: 1364 Field 'first_name' doesn't have a default value (HY000)`
I have tried setting all the values to NULL and it worked but I want to work with some fields setting as NOT NULL and update the ones which are NULL and also if we can fetch or set the fields automatically to what we have ?
This is my controller where I am trying to update the field if this is required or help you understand my problem
public function deleteSubject($id) {
echo $id;
// die();
if(Auth::check()) {
$findSubject = Auth::user()->where('subject_id', $id);
$users = new User();
$users->subject_id = null;
$users->save();
// echo($findSubject);
// die();
Session::flash("message", "You subject has been deleted. You can add a new Subject now.");
return redirect('/subjects');
} else {
Session::flash("message", "Please sign in to access this page");
return redirect('/signup');
}
}
The following should work for your code. As it was said in the previous comment, it's because you try to create a new instance of a user without inserting value.
It look like you are trying to delete the subject associate with the authenticated user, so I suppose that you don't really need to create a new user, instead I think you should dissociate the user and the subject. So, the following should work for your code.
The purpose of that variant is to take the authenticated user and put a null value for the subject_id.
public function deleteSubject($id) {
echo $id;
// die();
if(Auth::check()) {
$user = User::where('subject_id', $id)->first(); // This will get the user that have the subect_id, but it's assuming that only one user have this subject_id.
// You can also do this just uncomment the first line below and comment the other one above.
// $user = User::find(Auth::user->id);
$user->subject_id = null;
$user->save()
Session::flash("message", "You subject has been deleted. You can add a new Subject now.");
return redirect('/subjects');
} else {
Session::flash("message", "Please sign in to access this page");
return redirect('/signup');
}
}
I think that you should take a look about how MVC work.
https://selftaughtcoders.com/from-idea-to-launch/lesson-17/laravel-5-mvc-application-in-10-minutes/
You should also take a look at relationship in Laravel: https://laravel.com/docs/5.8/eloquent-relationships
MVC and Eloquent-Relationships will help you understand some function in laravel to achieve this kind of goal really quickly.
If you get a User model and a Subject model, you can simply do something like this:
$user = User->find(Auth::user()->id);
$user->subjects()->dissociate($id);
I'm not sure, but I think the Auth facade let you use the user model function, so maybe this could work to:
Auth::user()->subjects()->dissociate($id);
You should also take a look at middleware: https://laravel.com/docs/5.8/middleware
With middleware, you can put rules like the one you are using to send a message to the user saying that he/she need to be log in to access the page into the middleware and reusing it whenever you need.

Laravel 5.4 save NULL to database not working

The following code results in an empty value inside the database and when I get it out of the database its an empty String.
$customer->update(['company' => NULL]);
You should consider 3 things:
1) Make sure company column in nullable in your table. If it's not it won't be possible to put null in there.
2) Make sure you have in $fillable property of Customer model column company
3) Verify you don't have any mutator in your Customer model - so verify you don't have setCompanyAttribute method that might change value automatically to empty string if it's set to null
SOLUTION
If you want a field inside the database to be null and you do not use a mutator everything is fine.
Are you using a mutator, in my case the following snippet.
public function setCompanyAttribute($value)
{
$this->attributes['company'] = ucfirst($value);
}
Then you need to check if $value is null and then set it manually to null otherwise it will be an empty string.
To do that automatically on every model you can set the empty string back to null inside your EventServiceProvider.
Event::listen('eloquent.saving: *', function ($eventName, array $data) {
$attributes = [];
foreach ($data as $model) {
foreach( $model->getAttributes() as $key => $value ) {
$attributes[$key] = (is_string($value) && $value === '') ? null : $value;
}
$model->setRawAttributes($attributes);
}
return true;
});
It's important to first check all attributes from the model and set it to a temporary array. Now inside the setAttribute() method on the model instance the checks are done if a mutator exists. But thats exactly what you do not want, because it would set all fields with a definied mutator to an empty sting. Instead use setRawAttribute so the old attributes are overwritten with the ones from the temporary array.
This is safe to do because if the checked value isn't an empty string the already existing value is taken for that field. Otherwise if it's an empty string then null is set.
Hope it helps!

Preventing malicious users update data at add action

Here is a basic add action:
public function add()
{
$article = $this->Articles->newEntity();
if ($this->request->is('post')) {
$article = $this->Articles->patchEntity($article, $this->request->data);
if ($this->Articles->save($article)) {
$this->Flash->success('Success.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('Fail.');
}
}
$this->set(compact('article'));
}
If a malicious user injects at form a field with name id and set the value of this field to 2. Since the user do that the id value will be in $this->request->data so at $this->Articles->patchEntity($article, $this->request->data) this id will be patched and at $this->Articles->save($article) the record 2 will be updated instead of create a new record??
Depends.
Entity::$_accessible
If you baked your models, then this shouldn't happen, as the primary key field will not be included in the entities _accessible property, which defines the fields that can be mass assigned when creating/patching entities. (this behavior changed lately)
If you baked your models, then this shouldn't happen, as the primary key field(s) will be set to be non-assignable in the entities _accessible property, which means that these the fields cannot be set via mass assignment when creating/patching entities.
If you didn't baked your models and haven't defined the _accessible property, or added the primary key field to it, then yes, in case the posted data makes it to the patching mechanism, then that is what will happen, you'll be left with an UPDATE instead of an INSERT.
The Security component
The Security component will prevent form tampering, and reject requests with modified forms. If you'd use it, then the form data wouldn't make it to the add() method in the first place.
There's also the fieldList option
The fieldList option can be used when creating/patching entities in order to specifiy the fields that are allowed to be set on the entity. Sparse out the id field, and it cannot be injected anymore.
$article = $this->Articles->patchEntity($article, $this->request->data, [
'fieldList' => [
'title',
'body',
//...
]
]);
And finally, validation
Validation can prevent injections too, however that might be considered a little wonky. A custom rule that simply returns false would for example do it, you could create an additional validator, something like
public function validationAdd(Validator $validator) {
return
$this->validationDefault($validator)
->add('id', 'mustNotBePresent', ['rule' => function() {
return false;
}]);
}
which could then be used when patching the entity like
$article = $this->Articles->patchEntity($article, $this->request->data, [
'validate' => 'add'
]);

Is it possible to get the Model error code inside the controller in CakePHP?

I have a simple unsubscribe function in my Unsubscribed controller.
if ($this->Unsubscribe->save($this->data['Unsubscribes'])) {
// success
$this->Session->setFlash('Your email has been unsubscribed!');
$this->redirect('/unsubscribes/unsubscribe');
} else {
// error
$this->Session->setFlash('There was an error!');
$this->redirect('/unsubscribes/unsubscribe');
}
Here is the problem. I want to set the email address in the database as unique. So if someone enters the email address multiple times (or we already have it in our unsubscribe list), we are not populating the database with duplicate records. However, I want the visitor to know they have been added to the database (so they know they are unsubscribed).
Is there a way to detect the Duplicate entry error from the controller so I can equate that to a success? The caveat, I don't want to create a extended app_model. Any ideas? Can it be done? How is the best way to do this?
SOLUTION: Here is the final solution I implemented. I added the validation (as suggested by the chosen answer below) and I updated my controller as follows:
// error
if(isset($this->Unsubscribe->validationErrors['email'])){
$error = 'Your email has been unsubscribed!';
} else {
$error = 'Something went wrong. Please try again.';
}
$this->Session->setFlash($error);
$this->redirect('/unsubscribes/unsubscribe');
What about using the isUnique validation rule? Then just use the validation error to inform the user.
var $validate = array(
'login' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken.'
)
);
Stole this directly from the cookbook. Section 4.1.4.14 isUnique to be precise.
I think you can do it like this:
if ($this->Unsubscribe->find('count',array('conditions'=>array('email'=>$this->data['Unsubscribes']['email']))) > 0 )
{
$this->Session->setFlash('duplicate email!');
$this->redirect('/unsubscribes/unsubscribe');
}
//then do your stuff
It depends. Is there any other error that might occur that you want to display? Or is this the only error that may occur? In case of the latter, just don't check:
$this->Unsubscribe->save($this->data['Unsubscribes']);
// I don't care if that actually saved or not,
// unless something horrible happened the email is in the database
$this->Session->setFlash('Your email has been unsubscribed!');
$this->redirect('/unsubscribes/unsubscribe');
Otherwise, you can use the invalidFields() method to find out what went wrong.