Loading Model Inside Another Model and Controller in CakePHP 3

Sometimes it necessary to load a model inside another model as well as another controller in CakePHP. This article will help you to load a model from another model & controller with CakePHP 3

Load From Model:
$anotherTable = TableRegistry::get('AnotherTable');
$data = $anotherTable->find('list');

CakePHP TableRegistry is a factory of the Table object. It comes with a lot of static function and get() one of them. TableRegistry::get() provides an instance of Table. You can see an example from above snippet.

Load From Controller:
$this->loadModel('AnotherModel');
$data = $this->AnotherModel->find('list');

If you need to load a model from the controller which does not default model of the controller. You can use loadModel() function to get an instance of another model.

How to develop your first page in CakePHP

Before you read this tutorial you have to know how to install CakePHP. If you do not know how to install CakePHP, you should read install CakePHP . To create your first page in CakePHP you have do couple of things, I an gonna show you.

Step 1 : Go to app/config folder and open routes.php file. You will see this code

<?php
    Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));

?>

You have to rename your controller name and action . (e.g your controller name sites and action name index).

<?php
    Router::connect('/', array('controller' => 'sites', 'action' => 'index', 'home'));

?>

That means you select your application home page. That means your application will be opened with SitesController’s index file.

Step 2 : Now you need to create a controller named like “SitesController.php” in your app/controller folder and you have to write following codes .

<?php
/**
* Created Sohel Rana
* User:  Rana
* Date: 2/20/13
*/
class  SitesController extends AppController{
    var $name = 'Sites';
    public function index(){
    }
}
?>

Step 3 : Your Controller has been ready now you need to create “View“ file. Go to app/View and create a folder with the same name of your Controller (e.g Sites).

Step 4 : You have created Site folder and now you have to create index.ctp file and write this code.

<h2>Welcome Page</h2>

Refresh you browser and see the result.

How to Install CakePHP

CakePHP is the most simpler, faster, powerful open source framework. it is written by PHP (Object Oriented)” . It’s follow the MVC (Model View Controller) pattern. Which is most interactive part of it. You can easily build your application require less coding. Let’s I am going to introduce how to Install CakePHP with some step.

Step 1 : Download latest version of CakePHP from cakephp.org and extract the downloaded file.

Step 2 : If you downloaded 2.2.3 then you will see cakephp-2.2.3 >> cakephp-2.2.3 this folder structure. You need to rename the second cakephp-2.2.3 with your desire name like cake.

Step 3 : Copy the folder and paste into your htdocs folder (your local server). and type in your browser “http://localhost/cake” (If you rename with cake name, otherwise write http://localhost/your_desire_name). Then you will see something like this image.

cake_install

Step 4 : After that you need to change the “Security Salt” and ”Security ChiperSeed”. To that go to app/config and open the core.php and you will see this code.

* A random string used in security hashing methods.
*/
    Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');

/**
* A random numeric string (digits only) used to encrypt/decrypt strings.
*/
    Configure::write('Security.cipherSeed', '76859309657453542496749683645');

You need to change those security.salt and security.CiperSeed. Like this

* A random string used in security hashing methods.
*/
    Configure::write('Security.salt', 'fesfsdfsdfsesfdlkfmma;lfm,as,mfcesR2G0FgaC9mi');

/**
* A random numeric string (digits only) used to encrypt/decrypt strings.
*/
    Configure::write('Security.cipherSeed', '7685645641654165416546549309657453542496749683645');

Step 5:  After that rename the file dsatbase.php.default to database.php . For doing that you have to go app/config and you will see the database.php.default

Step 6 : Then create a database and then open database.php file and you will see this code.

public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'user',
        'password' => 'password',
        'database' => 'database_name',
        'prefix' => '',
        //'encoding' => 'utf8',
    );

change the host,login,password,database name with your host,user, password,database name.

Step 7: That’s fine, you have done all things. Now refresh you browser and see you browser.