Models

Chisel creates model classes to help pull data throughout your application. A model class represents a single instance of the data it is related to. For catalogs, the model represents a single catalog entry and for repeatable sections a model represents one instance of that repeated item.

Getter methods will be included in the model for each field that was added via the workshop.



Location

Block models can be found here:

/application/blocks/my_block/models/

Catalog and Settings Page models will be located here:

/application/src/Models



Using the data


Blocks

The block view will be already set up and have all of your data pulled into the view.php file by default.

 

Catalogs and Settings Pages

Unlike blocks, there is no front-end view already asssociated with the catalog or settings page data. Since Chisel doesn't know how you intend to use this data, it leaves distributing it throughout your application up to you.

Models will still be generated for your catalogs and settings pages for use throughout your application. Your Model files will be located here.

/application/src/Models/Path/To/Page/ModelName

To use these models to pull your data, follow these 3 steps:

  1. Add the 'use' statement at the top of your controller.

    use Application\Src\Models\Path\To\Page\ModelName;
  2. Instantiate the class.

    $myModel = new ModelName();

    Note: for non-settings page models, the record id or the entire record itself must be passed into the constructor.

    $myModel = new ModelName($idOrRecord);
  3. Call the method for the piece of data you want. A getter method will be available for every field that was added via the workshop.

    $myModel->getData();

 

Creating a Catalog Library

Catalog entries will often be pulled in groups. It is recommended but not required to set up a library class for retrieving entries in bulk. Here are some simple instructions for setting up a catalog library class.

  1. Create the class file. The file should be created here:

    application/src/MyCatalogLibrary.php
  2. Add the following template to your file

    namespace \Application\Src;
    use Applcation\Src\Models\MyCatalogModel;
    use Database;
    
    class MyCatalogLibrary {
    
    	private static $catalogTable = \'MyCatalogTableName\';
    
    	public static function getEntries(){
    		$db = Database::get();
    
    		//get entries
    		$results = $db->GetAll(\'SELECT * FROM \'.self::$catalogTable.\' ORDER BY order_by_id\');
    
    		//put each record in model class
    		foreach($results as &$result){
    			$result = new MyCatalogModel($result);
    		}
    
    		return $results;
    	}
    }
    	
  3. Next, add the library's use statement in the file that entries should be pulled.

    use Application\Src\MyCatalogLibrary;
  4. Then, simply call the method for retrieving entries.

    $entries = MyCatalogLibrary::getEntries();