CodeIgniter Forums
How to match a model field with a database field? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: How to match a model field with a database field? (/showthread.php?tid=91863)



How to match a model field with a database field? - kuzeyybora - 10-24-2024

I want the variables inside my model to automatically match the database fields. For example, when I write
Code:
public string $name;
, it should automatically correspond to the
Code:
name
field in the database. I want to avoid having to write
Code:
$name = "name"
. How can I achieve this, or does the model structure have support for such functionality?


RE: How to match a model field with a database field? - InsiteFX - 10-26-2024

The only way to do that is to read the database table and get all the field names
using DBForge.

Then you can name your variables.


RE: How to match a model field with a database field? - gosocial2 - 10-28-2024

Hi Kuzey,

You do not need to define (mapping of) attributes (or variables as you call them) corresponding to the column names in the DB table. The data returned by the model will automatically ensure those attributes are pulled from the database.

Example:

Code:
CREATE TABLE `tbl_products` (
  `uuid` char(36) COLLATE utf8_unicode_ci NOT NULL,
  `name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `slug` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `description` mediumtext COLLATE utf8_unicode_ci,

 PRIMARY KEY (`uuid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


PHP Code:
<?php // Or migration if you will: 

namespace App\Database\Migrations;

use 
CodeIgniter\Database\Migration;

class 
CreateProductsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'uuid' => [
                'type' => 'CHAR',
                'constraint' => 36,
                'null' => false
            
],
            'name' => [
                'type' => 'VARCHAR',
                'constraint' => 50,
                'null' => false
            
],
            'slug' => [
                'type' => 'VARCHAR'
                'constraint' => 100,
                'null' => true
            
],
            'description' => [
                'type' => 'MEDIUMTEXT',
                'null' => true
            
]
        ]);

        $this->forge->addKey('uuid'true);
        $this->forge->createTable('tbl_products'true);
    }

    public function down()
    {
        $this->forge->dropTable('tbl_products');
    }




PHP Code:
<?php // Model code

namespace App\Models;

use 
CodeIgniter\Model;

class 
ProductModel extends Model
{
    protected $table "tbl_products";

    protected $returnType 'object';

// end ProductModel.php 



PHP Code:
<?php // Controller Products.php

namespace App\Controllers;

use 
App\Models\ProductModel;
use 
CodeIgniter\Controller;

class 
Products extends Controller
{

    public function index() {
        $model model('ProductModel'); // same as = new ProductModel();
        $products $model->findAll();
        $viewData['products'] = $products;
        return view('products/list'$viewData);
    }
// end Products.php 


// app/Views/products/list.php :

PHP Code:
<h1>Products</h1>
<
table class="table">
    <thead>
        <tr>
            <th>UUID</th>
            <th>Name</th>
            <th>Slug</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($products as $product): ?>
            <tr>
                <td><?= esc($product->uuid?></td>
                <td><?= esc($product->name?></td>
                <td><?= esc($product->slug?></td>
                <td><?= esc($product->description?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>