Table of Contents
Let us learn Phalcon through examples
Insert data" >Insert data
Home Backend Development PHP Tutorial Functions you must know when entering Phalcon 'Phalcon Entry Guide Series 2'

Functions you must know when entering Phalcon 'Phalcon Entry Guide Series 2'

Jul 07, 2021 pm 04:07 PM
phalcon

Let us learn Phalcon through examples

  • Contents of this series
  • Preface
  • 1. Project structure
  • 2. Entry file
  • 3. Configuring Nginx
  • 4. Controller jump
  • 5. Database addition, deletion, modification and query
    • Insert data
    • Modify data
    • Delete data
  • 6. Code optimization
  • Summary

Directory of this series

1. Installing Phalcon on Windows "Phalcon Pit Guide Series 1"

Preface

The previous article introduced you to the installation of Phalcon and the use of Phalcon development tools Created projects, controllers, and models. Just did a few simple operations.

In this issue, we will continue to talk about the actual use of Phalcon.

1. Project structure

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

From As can be seen from the above figure, this directory structure is very similar to the TP framework. The corresponding directories will not be explained one by one. Let me tell you about the migrations directory.

This directory is just like the database migration in laravel. I won’t go into details on how to use it!

The framework structure is not fixed. Like ThinkPHP, you can register a namespace to modify the directory structure.

In the Phalcon framework, Kaka’s recent project was also developed using multiple modules. However, the directory structure is also different from the directory generated using the Phalcon development tool.

Everything remains the same, they all look the same.

2. Entry file

An essential file for each framework, index.php seems to be all Developer default.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Then it is also essential in the framework of Phalcon.

I won’t analyze the source code in detail about what is loaded here. It is not necessary. If you want to see the source code analysis, you can search for ThinkPHP framework source code analysis.

The general execution is to perform dependency injection first, and use /config/services.php to introduce some files. What you need to pay attention to is that the database connection is made here.

This file/config/router.phpYou will know what it is by looking at the name, routing! How to set up routing will be discussed later.

Get the configuration information after passing the first step of dependency injection.

The last line of code is include APP_PATH . '/config/loader.php';Register the directory obtained from the configuration information.

3. Configure Nginx

In the first issue of the article, the project was not configured. Let’s do a simple configuration next.

Phalcon provides three ways of configuration, let’s just use the simplest one first.

server {
        listen        80;
        server_name  www.kakaweb.com;
        root   "D:/phpstudy_pro/WWW/phalcon/public";
        index index.php index.html error/index.html;
	    location / {
	        try_files $uri $uri/ /index.php?_url=$uri&$args;
	    }

        
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9002;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
	
	    location ~ /\.ht {
	        deny all;
	    }}
Copy after login

The above is the configuration of Kaka. If you are also using PhpStudy, you can directly copy it and use it.

4. Controller jump

In the first article, the control was created using the phalcon development tool If you haven't created a project yet, you need to read the first article!

Let’s see how the visit goes first.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Code

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

You can see that in the index controller, another method kaka is also established.

Mainstream frameworks are configured with the index controller as the default access path. How to access this kaka is the same as other frameworks.

The access link is http://www.kakaweb.com/index/kaka.

is the domain name controller method name. It should be noted that the method name here does not need to include Action.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Practice the official case.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

You can see that the output result is a link

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

This link will jump directly to the Signup controller. Next, use the developer tools to generate this controller.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Then click the button just now and it will jump to the Signup controller.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Let’s talk about the controller first.

5. Database addition, deletion, modification and query

You can see that it is defined in advance in the model file Okay, two methods, no matter what they are, let’s try them first.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Write the following code directly in the controller

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

##Query results

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

It can be seen that

    find method is to get all the data
  • findFirst only takes the first piece of data
  • find(15) Query the data with id 15
  • find("type = 'mechanical'"); Conditional search

Insert data

实现代码

    public function holdAction ()
    {
        $user = new User();

        $phql = "INSERT INTO User (name, age, sex) VALUES (:name:, :age:, :sex:)";

        $status = $user->modelsManager->executeQuery($phql, array(
            'name' => "咔咔1",
            'age' => 24,
            'sex' => 1
        ));

    }
Copy after login

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

这里需要注意一下这个SQL语句$phql = "INSERT INTO User (name, age, sex) VALUES (:name:, :age:, :sex:)";

在这里User指的是模型,并不是数据库表名。

修改数据

实现代码

    public function modifyAction ()
    {
        $user = new User();

        $phql = "UPDATE User SET name = :name:, age = :age:, sex = :sex: WHERE id = :id:";

        $status = $user->modelsManager->executeQuery($phql, array(
            'id' => 20,
            'name' => "咔咔2",
            'age' => 25,
            'sex' => 2
        ));
    }
Copy after login

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

    public function deleteAction ()
    {
        $user = new User();

        $phql = "DELETE FROM User WHERE id = :id:";

        $status = $user->modelsManager->executeQuery($phql, array(
            'id' => 20
        ));

    }
Copy after login

可以看到已经没有结果了

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

这时你会发现,在检索数据的时候用的框架自带的方法,到增、删、改使用的类似于原生了。

对于这个问题,如果你是新手建议会那种方式就用那种方式,因为工期可不等你。

Kaka will also talk to you about the method of using framework modifications. Don’t worry about this. The next article will be published!

6. Code Optimization

In Section 5, did you find this problem?

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

In all methods, the User model is instantiated, and this is OK.

But think about it, if you use this method for a full project in the early stages of the project, and find that you need to change the name in the middle, what would you do?

Search the User keyword globally and change it to the modified name?

To be honest, few programmers dare to do this kind of operation because you don’t know where the problem will occur.

So Kaka will tell you a method for unified management of these models.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

You can declare the model in your own way.

Then initialize in the controller and instantiate the model here.

At this point you are thinking that if the table name is changed, do we only need to modify the name in the initialization method?

Summarize

This article introduces you to the necessary functions when using a framework.

Although a native-like method is used in the process of adding, deleting, modifying, and checking, this method is rarely used in any framework.

But no matter which way it is, it’s all code, right? Don't sneer at it, the framework functions can change at will, but these SQL statements will never change.

#Persistence in learning, persistence in writing, and persistence in sharing are the beliefs that Kaka has always adhered to since his career. I hope that Kaka’s articles on the huge Internet can bring you a little bit of help. I’m Kaka, see you next time.

The above is the detailed content of Functions you must know when entering Phalcon 'Phalcon Entry Guide Series 2'. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
How to use Phalcon5 framework in php? How to use Phalcon5 framework in php? Jun 03, 2023 pm 12:21 PM

As an open source scripting language, PHP is widely used because of its portability, cross-platform, concise and easy-to-read code, fast development speed, and strong scalability. In PHP, using frameworks can make it easier to organize code, improve code quality and development efficiency. Phalcon5 is an excellent framework in PHP. This article will introduce how to use the Phalcon5 framework for web development. 1. Install the Phalcon5 framework. Before you start using the Phalcon5 framework, you need to install it first.

How to use database transactions (Transactions) in Phalcon framework How to use database transactions (Transactions) in Phalcon framework Jul 28, 2023 pm 07:25 PM

How to use database transactions (Transactions) in the Phalcon framework Introduction: Database transactions are an important mechanism that can ensure the atomicity and consistency of database operations. When developing using the Phalcon framework, we often need to use database transactions to handle a series of related database operations. This article will introduce how to use database transactions in the Phalcon framework and provide relevant code examples. 1. What are database transactions (Transactions)? data

How to use PHP-FPM optimization to improve the performance of Phalcon applications How to use PHP-FPM optimization to improve the performance of Phalcon applications Oct 05, 2023 pm 01:54 PM

How to use PHP-FPM to optimize and improve the performance of Phalcon applications. Introduction: Phalcon is a high-performance PHP framework. Combining with PHP-FPM can further improve the performance of applications. This article will introduce how to use PHP-FPM to optimize the performance of Phalcon applications and provide specific code examples. 1. What is PHP-FPMPHP-FPM (PHPFastCGIProcessManager) is a PHP process independent of the web server

PHP development: Use Phalcon to develop high-performance web applications PHP development: Use Phalcon to develop high-performance web applications Jun 15, 2023 pm 04:41 PM

With the continuous development of the Internet, Web application development has become an indispensable part of all walks of life. As a popular server scripting language, PHP has also become one of the mainstream languages ​​​​for Web application development. However, the performance and scalability issues of the PHP language itself inevitably limit its development in the field of Web development. In order to solve these problems, Phalcon emerged as a new PHP framework, committed to providing a high-performance, easy-to-expand, easy-to-use and reliable

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

How to use ORM (Object Relational Mapping) in Phalcon framework? How to use ORM (Object Relational Mapping) in Phalcon framework? Jun 03, 2023 pm 09:21 PM

With the continuous development of web applications, corresponding web development frameworks are also emerging. Among them, the Phalcon framework is favored by more and more developers because of its high performance and flexibility. Phalcon framework provides many useful components, among which ORM (Object Relational Mapping) is considered one of the most important. This article will introduce how to use ORM in the Phalcon framework and some practical application examples. What is ORM First, we need to understand what ORM is. ORM is Object-Rel

Symfony vs Phalcon: Which framework is better for developing large-scale social media applications? Symfony vs Phalcon: Which framework is better for developing large-scale social media applications? Jun 18, 2023 pm 10:09 PM

As social media applications continue to grow, more and more developers are starting to focus on which framework is best for building such applications. Symfony and Phalcon are two very popular PHP frameworks with mature communities and powerful development tools. But if you need to develop large-scale social media applications, which framework is better? Symfony is a mature PHP framework that provides rich features and tools to help you quickly build large applications. Symfon

Slim vs Phalcon: Which microframework is better for large projects? Slim vs Phalcon: Which microframework is better for large projects? Jun 01, 2024 am 11:21 AM

For large projects, performance is crucial, and Phalcon, with its C language extensions, outperforms pure PHP's Slim in terms of processing speed. In addition, Phalcon provides a wider range of functions (including ORM, validation, caching) and has a more powerful dependency injection container for easy expansion. Therefore, Phalcon is more suitable for large projects in terms of performance, functionality and scalability.

See all articles