Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Example of Model method and the controller that calls it
  • The docs provide clear granular use cases of the DB class, but what I've had a hard time finding is one complete example. Seeing the DB as part of a complete example would be a huge plus. Can someone please show me what I'm doing wrong here?
    Model:
    <code>
    use Orm\Model; class Model_Sample extends Model
    {
    protected static $_table_name = 'beverages'
    protected static $_primary_key = array('id');
    protected static $_properties = array(
    'id',
    'beer',
    'taste',
    ); public static function get_beer()
    {
    $result = DB::select()->from('beverages')->where('beer', '=', 'delish')->execute();
    return $result; }
    </code>
    Controller:
    <code> <?php class Controller_Goodtimes extends Controller_Template
    {
    public function action_index()
    {
    $data = array();
    $data = Model_Sample::find(1); /*uses ORM and returns row with no problem */
    $data = Model_Sample::get_beer(); /*new method deserves Mr. Miyagi's crane kick to the jaw */ $this->template->title = 'Page Title';
    $this->template->content = View::forge('beverages/delishbeers', $data); } }
    </code> Thank you for your time,
    John
  • Why use the ORM model, and then write your own queries?
    public static function get_beer()
    {
        return static::find()->where('beer', '=', 'delish')->get();
    }
    

    And I would make it more generic, instead of hardcoding every possible where clause you can think of. I would pass the desired beer type as a parameter, and allow for passing an array to add multiple selections.
  • That is a simplified example. I fully intend to pass parameters to more complex queries. The bigger question is, is the method correct? Is that the correct way to return the result? Am I calling it correctly from the controller? Fuel has Orm, Orm Crud, Model Crud, DB Class, Query Builder, quite frankly its confusing, especially when the docs don't provide a complete example of any of them. Its difficult for a noob to put it all together. I'm just trying to get out of the gate with whatever is the best practice. It doesn’t matter to me what that is. If best practice is to use ORM, and add custom methods, where can I find a complete Model and supporting Controller that exemplifies that? If best practice is to use Model_Crud and custom methods, where is the complete example of that that shows the database interaction, how the result is returned, how the controller calls that specific example? Thank you for your help WanWizard, its scary to think where these forums, the IRC and Fuel would be without you.
  • Fuel has Orm, Orm Crud, Model Crud, DB Class, Query Builder What is ORM Crud? DB class and Query Builder are the same thing. Really, you have the Orm, Model_Crud or DB. Orm = full Object Relational Mapping, following the ActiveRecord patterns.
    Model_Crud = sites on top of DB class to do Crud for you, but doesn't do relationships.
    DB = DIY
  • You pick a solution based on the requirements that you have: Query Builder:
    - Use it if you want to write your own queries, either by coding SQL yourself, use by using the helper methods.
    - Create model classes as standard classes, no extension needed. Model_Crud:
    - Simple base model class. Provides common used model code to greatly reduce the need to write SQL.
    - Create model classes that extend \Model_Crud. ORM:
    - Full object oriented access to your data. Full support for relations between models.
    - Create model classes that extend ORM\Model.
  • The title and top of this page led me to believe Orm Crud was a thing http://fueldevdocs.exite.eu/packages/orm/crud.html Thank you for your replies. It clears up a lot.
  • CRUD is a common acronym for Create, Read, Update, Delete, a set of operations you do on a database to maintain the data. Both Model_Crud and ORM models provide methods that allow you to execute these functions, but in a different way, and with a different featureset.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion