Welcome Guest, Not a member yet? Register   Sign In
Help on entities
#5

(06-25-2020, 10:04 PM)Leo Wrote:
(06-25-2020, 03:14 PM)vinezof2 Wrote: $allowedFields is really necessary? i don't got it very well because all attributes where in the entity and this makes me confused
No, you can set this in your models $protectFields = false;
But, I usually fill them out because regardless if you write entity fields or not, $allowedFields is the one that handles "filtering" of data. Basically if you do this: $product->fill($_POST) - you may get stuff you don't need in there and with $allowedFields it cross-references what is "valid" and what gets left out.

Theres one example on entities I would like to see though.
How to handle business logic of a bunch of fields.
Currenty I do this: $product->fill($input) with $input variable holding an array of processed data I handled elsewhere in my controller.

I know I can do business logic in entities though, which would clean up my code somewhat, but I'm not sure how to go about this.
I want to actually do this: $product->fill($_POST)
And within entities I want to check each $_POST value - if it is '' set it to null, if it has a trailing space in the beginning or end i want to trim() (users tend to press the spacebar inadvertently). And for one of the $_POST fields - the url I want to do this: url_title($_POST['posted_url'])
Anyone got a good example on this?

I understand, is that sometimes I have very large tables and too lazy to fill the array.

About what you want to do, just create setter methods in your entity because the codeigniter will always try to find the set method first. example:
PHP Code:
<?php namespace App\Entities;

use \
CodeIgniter\Entity;

class 
MyClass extends Entity
{
    private 
$id;
    private 
$name;
    private 
$posted_url;
    
    public function 
setId(int $id)
    {
        
$this->id $id;
    }
    
    public function 
setName(string $name)
    {
        
$this->name trim($name);
    }
    
    public function 
setPostedUrl(string $posted_url)
    {
        
$this->posted_url url_title($posted_url);
    }


That way even if you make new MyClass($_POST) the set method is called.
i love cli
i love ci
Reply


Messages In This Thread
Help on entities - by MatheusCastro - 06-24-2020, 05:37 AM
RE: Help on entities - by Leo - 06-25-2020, 02:41 PM
RE: Help on entities - by vinezof2 - 06-25-2020, 03:14 PM
RE: Help on entities - by Leo - 06-25-2020, 10:04 PM
RE: Help on entities - by vinezof2 - 06-26-2020, 04:25 AM
RE: Help on entities - by Leo - 06-26-2020, 07:02 AM
RE: Help on entities - by vinezof2 - 06-26-2020, 10:24 AM
RE: Help on entities - by Leo - 06-26-2020, 01:32 PM



Theme © iAndrew 2016 - Forum software by © MyBB