Need help about Query Bilder |
(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: 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; 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 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 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; The method Model::builder() (i.e. $this->builder(); ) returns a builder instance. @webdevron, Got your PM, but cannot reply. You need to change your settings to accept messages. |
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
|