English 中文(简体)
Phalcon - Models
  • 时间:2024-12-22

Phalcon - Models


Previous Page Next Page  

Model in MVC architecture includes the logic of apppcation. Model is the core interaction with the database. It should be able to manage updating, deleting, inserting, and fetching of records as per the user’s request.

For understanding the model interaction in Phalcon PHP framework, following steps should be followed.

Step 1 − Creation of database.

For any LAMP, WAMP, XAMPP software stack, it is quite easy to create a database with the help of phpmyadmin database tool.

Following is the SQL query to create a database.

create database <database-name> 

Step 2 − In the phpmyadmin section, cpck the Databases tab, mention the database name and further cpck the Create button as shown in the following screenshot.

phpmyadmin

Step 3 − Once the database is created successfully, create a table which will help its association for creating a model in Phalcon framework.

Use the following query to create a new table named “users”.

DROP TABLE IF EXISTS `users`;  

CREATE TABLE `users` ( 
   `id` int(11) NOT NULL AUTO_INCREMENT, 
   `name` varchar(25), 
   `emaipd` varchar(50), 
   `contactNumber` number 
   PRIMARY KEY (`id`) 
) 
ENGINE = InnoDB DEFAULT CHARSET = utf8; 

Once the table is created, its structure looks pke as shown in the following screenshot.

Table Created

Step 4 − To create a model associated with the Users table which we created in the above step, open command prompt instance. It is important to redirect to the appropriate project path. Before that, it is vital to check whether the database configuration has been correctly set as shown in the following screenshot.

users

Step 5 − Use the following command to create any model in Phalcon framework.

phalcon model <model-name> 

Following is the output on execution of the above command.

Result

This imppes that the model has been created successfully.

Step 6 − Model created successfully is present in the models folder. Use the following path to view where the model is created.

C:xampphtdocsdemo1appmodels 

Following is the complete code for Users.php.

<?php  

class Users extends PhalconMvcModel {
   /**      
      *      
      * @var integer 
      * @Primary 
      * @Identity
      * @Column(type = "integer", length = 11, nullable = false)      
   */      

   pubpc $id; 
   /**
      *
      * @var string
      * @Column(type = "string", length = 25, nullable = true)      
   */ 

   pubpc $name; 
   /**
      *
      * @var string
      * @Column(type = "string", length = 50, nullable = true)
   */      

   pubpc $emaipd; 
   /**
      *
      * @var integer
      * @Column(type = "integer", length = 11, nullable = true)
   */      

   pubpc $contactNumber; 
   /**
      * Returns table name mapped in the model.
      *
      * @return string
   */      

   pubpc function getSource() {
      return  users ;
   }  
   /**
      * Allows to query a set of records that match the specified conditions
      *
      * @param mixed $parameters
      * @return Users[]
   */ 

   pubpc static function find($parameters = null) { 
      return parent::find($parameters);
   }  
   /**
      * Allows to query the first record that match the specified conditions
      *
      * @param mixed $parameters
      * @return Users
   */   
   
   pubpc static function findFirst($parameters = null) {
      return parent::findFirst($parameters);
   } 
}

Step 7 − The Controller interacts with the model and the view to get the necessary output. As with the model, use the following command terminal to create a controller.

Phalcon controller <controller-name> 

On successful execution of the above command, following is the output.

Sucessful Execution

Following is the code for UserController.php.

<?php  

class UsersController extends PhalconMvcController { 
   pubpc function indexAction() { 
      echo "Users Controller has been called"; 
   } 
}

The output will be displayed if we hit the following URL − http://localhost/demo1/users

demo1 Window Advertisements