Welcome Guest, Not a member yet? Register   Sign In
select only false
#1

(This post was last modified: 07-08-2021, 02:03 AM by lucavalentino.)

I am trying to recover data from Mysql but I always get false.
Model

PHP Code:
namespace App\Models;

use 
CodeIgniter\Model;
use 
App\Entities\Studente_e;


class 
Studente_m extends Model
{
protected 
$table      'tblstud';
protected 
$primaryKey 'ID';
protected 
$useAutoIncrement true;
protected 
$returnType 'App\Entities\Studente_e';
protected 
$useSoftDeletes true;

protected 
$allowedFields = ['name''email'];

/*protected $useTimestamps = false;
protected $createdField  = 'created_at';
protected $updatedField  = 'updated_at';
protected $deletedField  = 'deleted_at';*/

protected $validationRules    = [];
protected 
$validationMessages = [];
protected 
$skipValidation    false;
protected 
$useTimestamps true;

]public function 
__construct()
{
$this->db = \Config\Database::connect();
$this->db db_connect();
$this->builder $this->db->table($this->table);
}

  function check($id$email){

  $this->builder->select('ID, NAME, EMAIL');
$this->builder->from('tblstud');
  $this->builder->where('ID'$ID);
  $this->builder->where('EMAIL',$email);
  $query=$this->builder->get();
  var_dump($this->builder->get()); 
Reply
#2

You do not need this the model already has it intialized.

PHP Code:
// ] that will cause an error
]public function __construct()
{
    $this->db = \Config\Database::connect();
    $this->db db_connect();
    $this->builder $this->db->table($this->table);
}

// Just use this
public function __construct()
{
    parent::__construct();
}

// Example method:
public function getAllPosts() : PostModel
{
    $builder $this->builder();

    $builder->orderBy('created_at''desc');

    return $this;

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

I solved
Modeld

PHP Code:
namespace App\Models;

use 
CodeIgniter\Model;
use 
App\Entities\Studente_e;


class 
Studente_m extends Model
{
protected 
$table      'tblstud';
protected 
$primaryKey 'ID';
protected 
$useAutoIncrement true;
protected 
$returnType 'App\Entities\Studente_e';
protected 
$useSoftDeletes true;

protected 
$allowedFields = ['name''email'];

/*protected $useTimestamps = false;
protected $createdField  = 'created_at';
protected $updatedField  = 'updated_at';
protected $deletedField  = 'deleted_at';*/

protected $validationRules    = [];
protected 
$validationMessages = [];
protected 
$skipValidation    false;
protected 
$useTimestamps true;

//public function __construct(){ $this->db = \Config\Database::connect(); $this->db = db_connect(); $this->builder = $this->db->table($this->table); }

  function check($id$email){

  $this->select('ID, NAME, EMAIL');
$this->from('tblstud');
  $this->where('ID'$ID);
  $this->where('EMAIL',$email);
  $query=$this->builder->get()->getResultArray();
...

Now how do I use entities
Quote:
Code:
namespace App\Entities;

use CodeIgniter\Entity\Entity;

class Studente_e extends Entity
{
protected $datamap = [
'MATRICOLA'=>'ID'
...}


Reply
#4

I have examples on how to use models and entities in part 2 and part 5 of this series of tutorials. In these examples the code is in a library, but you can adapt the code to the functions in your model.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

I also recommend new users to read @includebeer tutorials well worth the read.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(07-12-2021, 01:38 AM)InsiteFX Wrote: I also recommend new users to read @includebeer tutorials well worth the read.
Thanks @InsiteFX  Cool
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#7

I tried to use entities, but it doesn't work

Model

PHP Code:
namespace App\Models;

use 
CodeIgniter\Model;
//use CodeIgniter\Database\ConnectionInterface;
use App\Entities\Stud_e;


class 
Stud_m extends Model
{
protected 
$table      'tblstud';
protected 
$primaryKey 'NUM_MATRIC';
protected 
$useAutoIncrement true;
protected 
$returnType 'App\Entities\Stud_e::class';
protected 
$useSoftDeletes true;

protected 
$allowedFields = ['NUM_MATRIC''E_MAIL'];

protected 
$useTimestamps true;
protected 
$createdField  'created_at';
protected 
$updatedField  'updated_at';
protected 
$deletedField  'deleted_at';

protected 
$validationRules    = [];
protected 
$validationMessages = [];
protected 
$skipValidation    false;

//protected $db;


public function __construct()
{
$stud_e = new \App\Entities\Studente_e();
$this->db = \Config\Database::connect();
$stud_e = new Stud_e();

function 
get_dati($matr){
$this->select('*');
$this->where('NUM_MATRIC'$matr);
return  $query$this->builder->get()->getResultArray($this->stud_e);


Entities

PHP Code:
namespace App\Entities;

use 
CodeIgniter\Entity\Entity;

class 
Studente_e extends Entity
{
protected 
$attributes  = [
'NUM_MATRIC'=>null,
'SURNAME'=>null,
....
];
protected 
$datamap = [
'ID'=>'NUM_MATRIC',
'NAME_SURNAME'=>'SURNAME',
...
];
public 
$stuD_e;
public function 
__construct (array $data null)
{
parent::__construct($data);
$this->stud_e = [];

Reply
#8

(This post was last modified: 07-19-2021, 02:06 AM by InsiteFX.)

Your class is wrong.
PHP Code:
// wrong
use CodeIgniter\Entity\Entity;

// should be
use CodeIgniter\Entity

Your class is wrong.
PHP Code:
// wrong
use CodeIgniter\Entity\Entity;

// should be
use CodeIgniter\Entity
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

@InsiteFX

CodeIgniter\Entity\Entity is correct. It is the new FQCN of the Entity class. The former is deprecated already.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB