• logo
  • PHP
  • PHP OOPs
  • script
    • JavaScript
    • JQuery
    • Ajax
    • AngularJs
    • VueJs
    • NodeJs
    • ReactJs
  • wordpress
  • Codeigniter
  • Codeigniter 4
  • Laravel
  • Python
  • MySql
  • Json
  • C
  • C++
  • More ...
    • Cakephp Framework
    • MongoDb
    • MySqli PDO
    • .htaccess
    • HTML 5
    • CSS
    • SEO
    • DCA

Codeigniter 4 Tutorials

  • Codeigniter 4
  • Application Run
  • Application Structure
  • Configuration files
  • Errors
  • Controller
  • View
  • Model
  • How to remove index.php from URL
  • Route
  • CodeIgniter URLs
  • Helper
  • Session
  • Global Functions and Constants
  • Logging Information in CI4
  • Error Handling
  • Signin / SignUp
  • Login / Register
  • CRUD
  • Crud 1
  • Form Validation
  • Data Table
  • Pagination
  • File Upload
  • Show Data Using Ajax
  • Select2 AJAX Autocomplete Search
  • Curl Post
  • Rest API
  • Weblink
Home » Codeigniter 4  » 

What is Codeigniter Model?


Database Configuration

The config file is located at app/Config/Database.php. you set your database connection values (username, password, database name, etc.) in the database.php

You can also set database connection values in the .env file.

File name : index.php

The config settings are stored in a class property that is an array

public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => TRUE,
'DBDebug' => TRUE,
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'strictOn' => FALSE,
'failover' => [],
];


File name : index.php

Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might require a full DSN string to be provided. If that is the case, you should use the ‘DSN’ configuration setting,
//$default['DSN'] = 'pgsql:host=localhost;port=5432;dbname=database_name';

// Oracle
//$default['DSN'] = '//localhost/XE';

Configuring With .env File

You can also save your configuration values within a .env file with the current server’s database settings.

File name : index.php

database.default.username = 'root';
database.default.password = '';
database.default.database = 'ci4';

Initializing the Database Class

File name : index.php

$db = \Config\Database::connect();

Connecting to Database

File name : index.php

$db = db_connect();

Standard Query With Multiple Results

File name : index.php

$query = $db->query('SELECT name, title, email FROM my_table');
$results = $query->getResult();
foreach ($results as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
echo 'Total Results: ' . count($results);

getResult() function returns an array of objects.

Standard Query With Multiple Results (Array)

File name : index.php

$query = $db->query('SELECT name, title, email FROM my_table');
$results = $query->getResultArray();

foreach ($results as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['email'];
}
getResultArray() function returns an array of standard array indexes

Standard Query With Single Result

File name : index.php

$query = $db->query('SELECT name FROM my_table LIMIT 1');
$row = $query->getRow();
echo $row->name;

getRow() function returns an object.

Standard Insert

File name : index.php

$sql = "INSERT INTO itechxpert (title, name) VALUES (".$db->escape($title).", ".$db->escape($name).")";
$db->query($sql);
echo $db->affectedRows();

Query Builder Query

File name : index.php

$query = $db->table('table_name')->get();
foreach ($query->getResult() as $row)
{
echo $row->title;
}

get() function retrieves all the results from the supplied table.

Query Builder Insert

File name : index.php

$data = [
'title' => $title,
'name' => $name,
'date' => $date
];

$db->table('mytable')->insert($data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')

How to create a model in codeigniter 4?

The model is used for defining the schema that is an archetype of table values. So, we have to manifest a new UserModel.php file in the app/Models/ folder.

File name : index.php

<?php
namespace App\Models;
use CodeIgniter\Model;

class UserModel extends Model
{
protected $table = 'users';

protected $primaryKey = 'id';

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

File name : index.php


File name : index.php





Itechtuto

Connect Us Socially:

Quick Links

  • itech
  • About Us
  • Feedback
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.