Phalcon Models

Model consists of information or data of the application. It manipulates the data by managing the rules. It is found under directory Phalcon\Mvc\Model which remain same for all basic application.

It provides following services:

  • Database Independence
  • CRUD functionality
  • Advanced finding capabilities
  • Ability to relate models

Below are the lists of some features which can be Enable/Disable specific feature:

OptionDescriptionDefault
astCacheEnables/Disables callbacks, hooks and event notifications from all the models.null
cacheLevel3
castOnHydratefalse
columnRenamingEnables/Disables the column renaming.true
disableAssignSettersAllow disabling setters in your model.false
enableImplicitJoinstrue
enableLiteralstrue
eventsEnables/Disables callbacks, hooks and event notifications from all the models.true
exceptionOnFailedSaveEnables/Disables throwing an exception when there is a failed save().false
lateStateBindingEnables/Disables late state binding of the Phalcon\Mvc\Model::cloneResultMap()method.false
notNullValidationsThe ORM automatically validate the not null columns present in the mapped tabletrue
parserCachenull
phqlLiteralsEnables/Disables literals in the PHQL parsertrue
uniqueCacheId3
updateSnapshotOnSaveEnables/Disables updating snapshots on save().true

Creating Models

Model is a class that extends Phalcon\Mvc\Model. In creating models, initialize() method is used which is called only once during the request.

 <?php  

namespace Phalcon\Tutorial;  

use Phalcon\Mvc\Model;  

class CarPrice extends Model  

{  

    public function initialize()  

    {  

        $this->setSource(car_price');  

    }  

}  

?> 

    But if we want to perform initialize task for every instance we uses onConstruct() method.

    <?php  
    
      
    
    namespace Phalcon\Tutorial;  
    
      
    
    use Phalcon\Mvc\Model;  
    
      
    
    class CarPrice extends Model  
    
    {  
    
        public function onConstruct()  
    
        {  
    
            // ...  
    
        }  
    
    }

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *