Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on load models in CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 583
    Comment on it

    Hi Reader's,
    Welcome to FindNerd,today we are going to discuss on load model in CakePHP.

    Basically model is a very important feature in a CakePHP web application because it is manage everything regarding our data, So in other words, we can say that model is used for representing data and access data from our CakePHP web application.
    If we want to load models in our CakePHP application so we have to follow below process because in CakePHP there are 3 ways to load model.

     

    1- App::import(): This is first step to load model in our web application. It find the name and file path details.

    let see how it is uses?

    <?php 
       //here User is model name
       App::import('Model', 'User');
       $this -> User = new User();
    
    ?>

     

    2- ClassRegistry::init(): This is another way for loading model on cakePHP web application and it is the better way to load model because it  adds the instance to the object map and returns the instance.

    let see how it is used?

    <?php 
       //here User is model name
       $this->User = ClassRegistry::init('User');
       $this->User->find('all');
    
    ?>

     

    3- Controller::LoadModel(): This is the last and best way to load the model in our CakePHP web application. is mostly used in CakePHP web application for loading model.This method works with only controller.

    let see how it is work?

    <?php 
     //here User is a model   
     $this->loadModel('User');
    ?>
    

     

    If we want to load more than two models in our web application then our code will like below.

    <?php 
      //here user, counrty and cirt are models
      public $uses = array('User', 'Cuntry','City'); 
     ?>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: