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

(This post was last modified: 06-26-2020, 10:27 AM by vinezof2.)

Quote:AHHH see thats the thing! I too have a lot of columns in my table and the way you wrote it just handles one column at a time, but I need a foreach loop or something to trim all the stuff I throw at it with a $product->fill($_POST)
or will
public function setName(string $name) $this->name = trim($name) }
take care of everything, with the $this->name being dynamic? like

Will this function work with fill? Or does it only work like this:
$product->name($name)?
$product->anotherfield($filed1);
$product->andAnotherField($field2);
$product->iHave_Like_40_Columns_In_MyTable_This_Will_BeHuge($and_This_Is_Only_field3);


para usar você pode usar assim:
PHP Code:
$product->setName($name);
$product->setValue($value); 
OR
PHP Code:
$product->name $name
$product
->value $value 

it will work the same because CodeIgniter's Entity class uses the magic methods of php __set() and __get(), whenever any value is assigned to some object property the function __set() is called and whenever any property is read the function __get() is called. If you don't want to create a get and set method for each property you can modify or override the __set() function and put some logic in it that clears your strings.

like this:
PHP Code:
public function __set(string $key$value null)
    {
        
$key $this->mapProperty($key);

        
// Check if the field should be mutated into a date
        
if (in_array($key$this->dates))
        {
            
$value $this->mutateDate($value);
        }

        
$isNullable false;
        
$castTo     false;

        if (
array_key_exists($key$this->casts))
        {
            
$isNullable strpos($this->casts[$key], '?') === 0;
            
$castTo     $isNullable substr($this->casts[$key], 1) : $this->casts[$key];
        }

        if (! 
$isNullable || ! is_null($value))
        {
            
// Array casting requires that we serialize the value
            // when setting it so that it can easily be stored
            // back to the database.
            
if ($castTo === 'array')
            {
                
$value serialize($value);
            }

            
// JSON casting requires that we JSONize the value
            // when setting it so that it can easily be stored
            // back to the database.
            
if (($castTo === 'json' || $castTo === 'json-array') && function_exists('json_encode'))
            {
                
$value json_encode($value);

                if (
json_last_error() !== JSON_ERROR_NONE)
                {
                    throw 
CastException::forInvalidJsonFormatException(json_last_error());
                }
            }
        }

        
// if a set* method exists for this key,
        // use that method to insert this value.
        // *) should be outside $isNullable check - SO maybe wants to do sth with null value automatically
        
$method 'set' str_replace(' '''ucwords(str_replace(['-''_'], ' '$key)));
        if (
method_exists($this$method))
        {
            
$this->$method($value);

            return 
$this;
        }

        
// Otherwise, just the value.
        // This allows for creation of new class
        // properties that are undefined, though
        // they cannot be saved. Useful for
        // grabbing values through joins,
        // assigning relationships, etc.
                
if (gettype($value) === 'string') {
                   
$this->attributes[$key] = trim($value);
                } else {
                   
$this->attributes[$key] = $value;
                }

        return 
$this;
    } 

this is default __set() method of Entity class of CodeIgniter with a small modification to string values use the trim() function **edit that way you don't need to create set methods for each attribute, just defining logics for a group of attributes
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