Welcome Guest, Not a member yet? Register   Sign In
Entities not uses by model with custom model method

Hy everyone,

I'm new on this forum, and totally novice on CI4. I've been working on CI2 for soooo manu years and on CI3 since 2019, but, with CI4, it's a new world opening to me !!!
I am discovering the Entities concept, it looks vers convenient but I'm really not comfortable with it even if I read the documentation about Model's and Entities. Here's my problem :
I've got a simple model "PaysModel" (countries), it works with Pays entity. When I call A find() or findAll() method, it works find. But, I need to modify the findAll() behavior : actually I need all countries with "France" in first position. 
So, in order to do that, I've tryied 2 things : 
- Create a findAll() method in my model
- Create a findAll2() (juste for the test) in my model.
Both of them works find but don't return Entities.

Could anybony explain me why and what to do ?
Actually I want my method to return an array of row where each rows in an entity of Pays.
This is my model : 
PHP Code:
namespace App\Models;

PaysModel extends Model

    protected $table    'ref_pays';
    protected $primaryKey  'id';

    protected $useAutoIncrement true;

    protected $returnType    'App\Entities\Pays';
    protected $useSoftDeletes true;

    protected $allowedFields = ["alpha2","langFR" ,'is_eu'];
    protected $useTimestamps true;

    public function findAll2()
        $sql    "SELECT * FROM ".$this->table." WHERE is_eu=1 ORDER BY FIELD(langFr,'France') DESC";
        $query  $this->db->query($sql);
        $result $query->getResult();

        return $result;

Here is my Entity :
PHP Code:
namespace App\Entities;

Pays extends Entity

    * Nom des champs
    * @var null[]
    protected $attributes = [
        "id" => null,
        "alpha2" => null,
        "langFR" => null,

    * permet de renommer les champs tels qu'on veut qu'ils apparaissent dans l'application
    * @var string[]
    protected $datamap = [
        "code"  => "alpha2",
        "nom"  => "langFR"

And here is the way my call in controller :
PHP Code:
use App\Models\PaysModel;
Users extends \CodeIgniter\Controller


   public function test()
     $Pays          = new PaysModel();
     $listePays $Pays->findAll2();


Your function doesn't return entities because you're calling the query builder. The functions that will return an Entity object are find() and findAll() from the Model class. You can use WHERE clauses with findAll(). I'm not sure about the FIELD() function...

PHP Code:
$listePays $Pays
->where( ['is_eu' => 1] )
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/

Theme © iAndrew 2016 - Forum software by © MyBB