/////////////////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'
];
}