Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| Ambdós costats versió prèvia Revisió prèvia | |||
| development:php:codeigniter:v4:entity [08/06/2026 00:45] – mate | development:php:codeigniter:v4:entity [08/06/2026 06:28] (actual) – mate | ||
|---|---|---|---|
| Línia 2: | Línia 2: | ||
| {{tag> | {{tag> | ||
| * [[https:// | * [[https:// | ||
| + | |||
| + | == crear | ||
| + | <code php> | ||
| + | namespace App\Entities; | ||
| + | |||
| + | use CodeIgniter\Entity\Entity; | ||
| + | |||
| + | class User extends Entity | ||
| + | { | ||
| + | // ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == usar | ||
| + | <code php> | ||
| + | namespace App\Models; | ||
| + | |||
| + | use CodeIgniter\Model; | ||
| + | |||
| + | class UserModel extends Model | ||
| + | { | ||
| + | protected $table | ||
| + | protected $allowedFields = [ | ||
| + | ' | ||
| + | ]; | ||
| + | protected $returnType | ||
| + | protected $useTimestamps = true; | ||
| + | } | ||
| + | </ | ||
| + | * save:< | ||
| + | $user = $userModel-> | ||
| + | |||
| + | // Display | ||
| + | echo $user-> | ||
| + | echo $user-> | ||
| + | |||
| + | // Updating | ||
| + | unset($user-> | ||
| + | |||
| + | if (! isset($user-> | ||
| + | $user-> | ||
| + | } | ||
| + | |||
| + | $userModel-> | ||
| + | |||
| + | // Create | ||
| + | $user = new \App\Entities\User(); | ||
| + | $user-> | ||
| + | $user-> | ||
| + | $userModel-> | ||
| + | </ | ||
| + | * fill: <code php> | ||
| + | $data = $this-> | ||
| + | |||
| + | # 1 | ||
| + | $user = new \App\Entities\User(); | ||
| + | $user-> | ||
| + | $userModel-> | ||
| + | |||
| + | # 2 | ||
| + | $user = new \App\Entities\User($data); | ||
| + | $userModel-> | ||
| + | </ | ||
| + | * ocultar atributos:< | ||
| + | namespace App\Entities; | ||
| + | |||
| + | use CodeIgniter\Entity\Entity; | ||
| + | |||
| + | class User extends Entity | ||
| + | { | ||
| + | protected $datamap = [ | ||
| + | ' | ||
| + | ]; | ||
| + | |||
| + | protected $attributes = [ | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ]; | ||
| + | } | ||
| + | |||
| + | $user = new User([' | ||
| + | |||
| + | echo ' | ||
| + | print_r($user-> | ||
| + | print_r($user-> | ||
| + | |||
| + | /** | ||
| + | * Output: | ||
| + | * | ||
| + | * Secure: Off | ||
| + | * Array | ||
| + | * ( | ||
| + | | ||
| + | | ||
| + | * ) | ||
| + | * Array | ||
| + | * ( | ||
| + | | ||
| + | | ||
| + | | ||
| + | * ) | ||
| + | */ | ||
| + | </ | ||
| + | * '' | ||
| + | * renombra propiedades por si ha habido cambios en la tabla (y no cambiar la lógica de la aplicación):< | ||
| + | |||
| + | use CodeIgniter\Entity\Entity; | ||
| + | |||
| + | class User extends Entity | ||
| + | { | ||
| + | protected $attributes = [ | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ]; | ||
| + | |||
| + | protected $datamap = [ | ||
| + | // property_name => db_column_name | ||
| + | ' | ||
| + | ]; | ||
| + | } | ||
| + | </ | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | * añadiendo lógica:< | ||
| + | |||
| + | use CodeIgniter\Entity\Entity; | ||
| + | use CodeIgniter\I18n\Time; | ||
| + | |||
| + | class User extends Entity | ||
| + | { | ||
| + | public function setPassword(string $pass) | ||
| + | { | ||
| + | $this-> | ||
| + | |||
| + | return $this; | ||
| + | } | ||
| + | |||
| + | public function setCreatedAt(string $dateString) | ||
| + | { | ||
| + | $this-> | ||
| + | |||
| + | return $this; | ||
| + | } | ||
| + | |||
| + | public function getCreatedAt(string $format = 'Y-m-d H: | ||
| + | { | ||
| + | // Convert to CodeIgniter\I18n\Time object | ||
| + | $this-> | ||
| + | |||
| + | $timezone = $this-> | ||
| + | |||
| + | $this-> | ||
| + | |||
| + | return $this-> | ||
| + | } | ||
| + | }</ | ||
| + | * para acceder a un atributo usando funciones con lógica, se ha de respetar el formato **set** o **get** + **variable** en PascalCase. Cuando hagamos una llamada al atributo, se llamaran automaticamente. | ||
| + | * si hemos " | ||