Welcome Guest, Not a member yet? Register   Sign In
Need help about Query Bilder
#9

(This post was last modified: 09-01-2019, 01:21 PM by webdevron.)

(08-31-2019, 02:10 PM)dave friend Wrote:
(08-28-2019, 11:55 AM)webdevron Wrote: I have read through the CI 4 docs but still confused about the query builder. Following code shows my current CI4 model:

PHP Code:
<?php
namespace App\Models;
use 
CodeIgniter\Model;

class 
TestModel extends Model
{

    protected $db;

    public function __construct( )
    {
        $this->db db_connect();
    

Because you are extending CodeIgniter\Model you don't need any of the code above.
That code would only be used when you are creating a model that does not extend CodeIgniter\Model - which is a perfectly legitimate thing to do in CI4. 

CodeIgniter's Model class has a lot of built-in functionality that makes it easy to use and a logical choice much of the time. You probably know this, but for those readers that might not, the documentation for CI4 Models is HERE.

If you are going to extend CodeIgniter\Model there are a couple of properties items that are very important.

PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
TestModel extends Model
{
    protected $table 'my_table';
    protected $primaryKey 'id';



There are several other properties you can use to configure the model - see the docs.

When you want to use TestModel then in a controller simply make this call. (Remember to "use" the model's namespace)

PHP Code:
$testModel = new TestModel(): 

There is a lot built into CodeIgniter\Model including a method that does exactly what your getData() function does. It returns a single row where the primary key matches the value passed in as the first parameter.

PHP Code:
// in the controller
$testRow $testModel->find($someKeyValue); 

find() is going to use the table and primary key you set in the class definition.

But the really cool thing is you can mix the Model methods with Query Builder methods, e.g.

PHP Code:
// in the controller
$data $testModel->where('status''active')
                  ->orderBy('last_login''asc')
                  ->findAll(); 

The important thing to note about those last two examples is that the entire class declaration is the one shown above. We didn't need to add any other methods to the class just set a couple of properties in order to run those examples.

There is a full set of CRUD methods baked into CodeIgniter\Model too! The CRUD stuff is well documented, but at this time full documentation of the available methods is missing from the docs. Examine the core file /system/Model.php to see the other functionality of the class.

If you wanted to turn the last example into a method defined in TestModel you could do this

PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
TestModel extends Model
{
    protected $table 'my_table';
    protected $primaryKey 'id';

    public function getAllActive($sort 'ASC')
    {
        $builder $this->builder();
        $query $builder->where('status''active')
                         ->orderBy('last_login'$sort)
                         ->get();
        return $query->getResult();  // returns an array of objects
    }


The method Model::builder()  (i.e. $this->builder(); ) returns a builder instance.

Thank you very much. This is not just a "REPLAY" but a "Tutorial of CI 4 MODAL"
"Who thinks in code"
Perfectly describes who I am
mbparvez.me
Reply


Messages In This Thread
Need help about Query Bilder - by webdevron - 08-28-2019, 11:55 AM
RE: Need help about Query Bilder - by InsiteFX - 08-28-2019, 04:53 PM
RE: Need help about Query Bilder - by webdevron - 08-29-2019, 04:09 PM
RE: Need help about Query Bilder - by InsiteFX - 08-30-2019, 04:37 AM
RE: Need help about Query Bilder - by webdevron - 08-31-2019, 11:29 AM
RE: Need help about Query Bilder - by InsiteFX - 08-30-2019, 08:36 AM
RE: Need help about Query Bilder - by InsiteFX - 08-31-2019, 12:17 PM
RE: Need help about Query Bilder - by dave friend - 08-31-2019, 02:10 PM
RE: Need help about Query Bilder - by webdevron - 09-01-2019, 04:46 AM
RE: Need help about Query Bilder - by InsiteFX - 09-01-2019, 12:07 PM



Theme © iAndrew 2016 - Forum software by © MyBB