CodeIgniter Forums
Get data in type from DB - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Get data in type from DB (/showthread.php?tid=87198)



Get data in type from DB - motoroller - 03-24-2023

class Wages {

protected BaseConnection $db;

public function __construct() {
$this->db = Database::connect();
}

public function get (): array {

return $this->db->table('wage')
->where('office', 2)
->orderBy('title')
->get()
->getResultArray();

}

}

I have model like this, how can i get data by types, not like string

I should do like this

<?php namespace App\Entities;

use CodeIgniter\Entity\Entity;

class Wage extends Entity {

protected $casts = [
'id' => 'integer',
'title' => 'string',
'office' => 'integer'
];

}


RE: Get data in type from DB - iRedds - 03-24-2023

Type conversion depends on the capabilities of the DBMS driver.
As far as I know, only mysql native driver (mysqlnd) can provide data types from the database.
Actually, this is why all frameworks use their own type conversion.

Why don't you like using Entity?
Why an array?


RE: Get data in type from DB - motoroller - 03-24-2023

(03-24-2023, 02:58 AM)iRedds Wrote: Type conversion depends on the capabilities of the DBMS driver.
As far as I know, only mysql native driver (mysqlnd) can provide data types from the database.
Actually, this is why all frameworks use their own type conversion.

Why don't you like using Entity?
Why an array?


OK how can i get any SQL query in cast?

Like this one?

$this->db->table('wage')
->where('office', 2)
->orderBy('title')
->get()
->getResultArray();

and how if i have few tables?


$this->db->table('wage as w, kinds as k')
->where('office', 2)
->where('w.kind = k.id')
->orderBy('title')
->get()
->getResultArray();


RE: Get data in type from DB - kenjis - 03-24-2023

See https://codeigniter4.github.io/CodeIgniter4/database/results.html#getting-an-array-of-custom-object