


Some error solutions and suggestions in using PHP's Yii framework, yii framework_PHP tutorial
Some error solutions and suggestions in the use of PHP's Yii framework, yii framework
This article is intended to record solutions to minor problems in the Yii development process. It is not comprehensive and does not Authoritative, not a tutorial. I have written it myself, and I think it can solve the problem. It may be used in the future, so just remember it.
1. Introduction of Js and Css files in Yii.
Let’s start with the simplest question. It’s not a problem, it’s just grammar. Assume that our js files are placed in the js folder at the same level as protected, and the css files are placed in the css folder at the same level as protected. Well, the specification is like this... Then we can put it in the corresponding view The interface is written as follows. The parameters of the css and js functions are different... (It took an hour to adjust because of this..)
The second parameter of the registered js file is the location where the js is placed. There are three options: CClientScript::POS_HEAD is placed in the Head part. CClientScript::POS_BEGIN is placed at the beginning of the Body. CClientScript::POS_END is placed at the end of the Body. There are no special requirements. There is no need to fill it out... The second parameter of the registered Css file is media. Interested students can click here. For now, the default is fine...
For js like Jquery, using registerCoreScript will not cause inexplicable errors...
1 2 3 4 5 6 |
|
2. Yii isNewRecord fix
The isNewRecord attribute of Yii’s Model is very useful and can be discussed on a case-by-case basis based on this attribute. However, if we turn on the transaction mechanism or other circumstances, causing the data to be rolled back after being inserted, then the record will not be in the database, but isNewRecord is false, which means it is no longer a new record. The solution is to use the primary key to access the database to determine whether it is a new record. Before using this attribute, we must first process it as follows. The following Model is Post, and the primary key is id:
1 2 3 4 5 6 |
|
3.Yii generates hidden input field
Although it is easy to write an input field by yourself (isn’t it just display:none), sometimes you have to follow Yii’s form code format. Anyway, just one sentence...
1 2 |
|
4. Yii generates drop-down menu
Many times we need a drop-down menu in a form. At this time, Chtml’s listdata is very useful. If the fields in our database have only a few possibilities, such as 0 and 1, we can write it as follows:
1 |
|
At this time, what you see is the drop-down menu of Yes and No. When you select 'Yes' to submit, this field will be filled with 1, and 'No' will be 0. Of course, it's often not just that simple. We can add a function to the Model to generate an array of drop-down menus, and then call it in the view. The data for this function can be written by yourself or found in the database. Listdata is used below, which specifically means that the id in the model is the key and the name is the value.
1 2 3 4 5 6 7 8 9 10 |
|
5.Yii Enable transaction mechanism
When you save several records to the database at the same time, you may need to enable the transaction mechanism. It is easy to enable the transaction mechanism in Yii, just three sentences are enough.
1 2 3 4 5 6 7 8 9 10 11 |
|
A more complete one is like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
But I usually do it like this, please experience the benefits yourself...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
6. An error occurred when querying the same fields in the associated table.
Sometimes we build two tables, but the two tables have the same fields. When using CDbCriteria to perform a with related query search, if no additional settings are made, a query error will occur, which probably means that the Mysql statement is ambiguous. . At this time, we can just set an alias in the main table, and then pay attention to adding the name when querying the relevant fields.
For example: two Models, Post and User, have an id. We can write it like this:
1 2 3 4 5 6 7 8 9 |
|
7. File upload
Speaking of which, this is not Yii. It is basically native HTML and PHP. If I am too lazy, I will just put it here.
The following is HTML, the action is changed to your own url, and the id and name are also defined by you.
1 2 3 4 5 |
|
This is the code for the server to receive and save the file. The file is finally saved to the file folder of the attached folder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
The following is to move the existing files from the old_file path to the current date folder in attached/file. The mobile here uses rename
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
8.YIi Scenarios and Security Fields
View the current Model scene:
1 |
|
View the scene’s security fields. Safe fields mean that these data will not be filtered out by Yii when submitted by users. Once I found that some of the things submitted on the webpage were missing and some were missing. After adjusting for a long time, I found out that some of them were filtered in that scenario.
1 2 |
|
强制赋值避免 rule 规则过滤字段。用 setAttributes 可以强制取消 Yii 的安全过滤,只要第二个参数赋值为 false 就好。但是这也只能对这个 Model 生成时就拥有的字段生效,如果要对包括自己定义的所有字段不过滤,还是要定义场景然后在 rule 里指定安全字段比较好。
1 2 |
|
检查日期格式合法性
有时我们需要检验用户填写的日期是否合法,可以用下面的函数。
1 2 3 4 5 6 7 |
|
Yii 渲染多个 model
相信新手都有疑惑,_form 里面的表单都是渲染一个 model 然后提交给 controller 保存数据的,如果想要渲染多个 model 怎么办呢?
下面,我们假设有两个 model 类,分别叫做 Person 和 Addr,我们想要做的是在一个 Person 的 _form 里再渲染几个 Addr 的 model ,意思是一个人可以有几个地址。基本思路其实还是很简单,就是你在 controller 里定义要渲染的 model 然后传给 view 界面,最后依然在 controller 里接收 Post 过来的数据。主要是写法问题而已,我相信下面大家都能看懂,有疑问的童鞋再留言好了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
