Welcome Guest, Not a member yet? Register   Sign In
How to clean Entity fields before saving
#1

Hello, 
I find the Entity class very useful and I use them a lot, for every database table I have a Model and its Entity class. 
What I find also useful is to get all the POST params from a form and fill the Entity with that data:
PHP Code:
$data $this->request->getPost();

$user = new \App\Entities\User();
$user->fill($data);
$userModel->save($user); 
But here I have some problems that I want to solve. If I have an input field in the form that is not a field of the table as well, it fires an error. For example "Unknown column 'amount_str' in 'field list'".

Yes, he is right, I don't have an amount_str field in my table.

I'd like to "clean" my Entity fields ... or better, ignore all those fields that don't match with the table fields. 
A sort of
PHP Code:
$fields $this->getFieldNames($this->table);

$objectParams array_keys($entityObject);

foreach (
$objectParams as $key => $value
{
 
      
if(!in_array($value$fields))
        unset($entityObject[$value]);
}

return 
$entityObject



Just before saving in the database so I don't get the error. 
What do you think it's a better way to write this once for all? I think it's something to put in the Model. Extending the save method?!
Reply


Messages In This Thread
How to clean Entity fields before saving - by Jag81 - 12-16-2021, 04:57 AM



Theme © iAndrew 2016 - Forum software by © MyBB