-
lucavalentino
Junior Member
-
Posts: 31
Threads: 17
Joined: Dec 2015
Reputation:
-1
07-08-2021, 01:49 AM
(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());
-
InsiteFX
Super Moderator
-
Posts: 6,334
Threads: 291
Joined: Oct 2014
Reputation:
235
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 )
-
lucavalentino
Junior Member
-
Posts: 31
Threads: 17
Joined: Dec 2015
Reputation:
-1
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'
...}
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
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.
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
(07-12-2021, 01:38 AM)InsiteFX Wrote: I also recommend new users to read @includebeer tutorials well worth the read. Thanks @ InsiteFX
-
lucavalentino
Junior Member
-
Posts: 31
Threads: 17
Joined: Dec 2015
Reputation:
-1
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 = []; }
-
InsiteFX
Super Moderator
-
Posts: 6,334
Threads: 291
Joined: Oct 2014
Reputation:
235
07-19-2021, 02:06 AM
(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 )
-
paulbalandan
External Auditor
-
Posts: 285
Threads: 6
Joined: Jul 2020
Reputation:
24
@ InsiteFX
CodeIgniter\Entity\Entity is correct. It is the new FQCN of the Entity class. The former is deprecated already.
|