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

(This post was last modified: 06-25-2020, 02:52 PM by Leo.)

Definitely use 'em! It just make stuff faster\easier to develop. Here's my own stuff with like 90% of fields not included for easy-reading

Example:
PHP Code:
/////////////////stuff in controller
$model = new ProductsModel();
$product $model->find($product_id)

$name $product->name //you can get name like this
$array $product->array_stuff //if you have an array you get an auto converted array here with entities

$product->name 'New name';
$product->array_stuff $newArray //a new array will be auto converted to a string and put in your product
model->save($product//it is updated!!

//or maybe you want something like this (still in controller):

$input = [
'name' => $_POST['name'],
'array_stuff' => $newArray,
'slug' => url_title($_POST['url'])
];

$product = new Product(); //an empty entity object to be filled or worked with
$product->fill($input);
model->save($product//it is updated!!

///////////////////////////////stuff in model
<?php namespace App\Models;

use 
CodeIgniter\Model;
use 
CodeIgniter\Exceptions\PageNotFoundException;

class 
ProductsModel extends Model
{
protected 
$table 'products';
protected 
$returnType 'App\Entities\Product';
protected 
$useSoftDeletes false;
protected 
$allowedFields = [
'id''name''slug''short_description''array_stuff '
];

protected 
$validationRules = [
'name' => ['label' => 'name''rules' => 'required|max_length[30]|is_unique[products.name]'],
'short_description' => ['label' => 'short description''rules' => 'permit_empty|max_length[80]'],
'slug' => ['label' => 'SEO URL''rules' => 'permit_empty|valid_url|is_unique[products.slug]']
];

public function 
productPage($slug)
{
$product $this->where('slug'$slug)->first();
if(empty(
$product)) {
throw 
PageNotFoundException::forPageNotFound();
}
return 
$product;
}
}

//////////////////////////////stuff in Entities
<?php namespace App\Entities;

use 
CodeIgniter\Entity;

class 
Product extends Entity
{
protected 
$casts = [
'name' => 'string',
'short_description' => '?string',
'slug' => '?string',
'array_stuff ' => '?array'
];

You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
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