Welcome Guest, Not a member yet? Register   Sign In
How to match a model field with a database field?
#1
Question 

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?
Reply
#2

(This post was last modified: 10-27-2024, 10:12 PM by InsiteFX. Edit Reason: Spelling error )

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.
What did you Try? What did you Get? What did you Expect?

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

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> 

CodeIgniter Wizard (CRUD code generator for Mac) instantly scaffolds Bootstrap-based web applications with an administrative interface (admin templates include Bootstrap5)

Reply




Theme © iAndrew 2016 - Forum software by © MyBB