国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial php框架yii入門遇到的有關(guān)問題

php框架yii入門遇到的有關(guān)問題

Jun 13, 2016 pm 12:05 PM
array

php框架yii入門遇到的問題

最近在學(xué)習(xí)php 一個流行的框架yii,聽說封裝得很好,但是在學(xué)習(xí)過程中也遇到了一些問題,先總結(jié)如下:

?

(1)The table "{{tbl_user}}" for active record class "User" cannot be found in the database.

詳細錯誤:

?錯誤原因:實體類User 的方法tableName 返回的是“'{{tbl_user}}'”

/**	 * @return string the associated database table name	 */	public function tableName()	{		return '{{tbl_user}}';	}

?但是在應(yīng)用配置文件(D:\study\yii\yii\demos\shop_goods\protected\config\main.php)中,沒有指定tablePrefix 。

解決方法:

方法一:去掉兩邊的花括號,改為:

/**	 * @return string the associated database table name	 */	public function tableName()	{		return 'tbl_user';	}

?

方法二:在應(yīng)用配置文件(D:\study\yii\yii\demos\shop_goods\protected\config\main.php)中指定

tablePrefix

(2)使用CPasswordHelper::verifyPassword($password,$this->password); 有問題

return CPasswordHelper::verifyPassword($password,$this->password); 有問題,
返回false,但是實際上$password,$this->password 是相等。

(3)php中調(diào)用成員變量時使用->,而不是.

有java工作經(jīng)驗的開發(fā)者,在使用yii框架時非常容易使用. 來引用成員變量,這是java的語法,不是php的語法。

?

(4)對于注冊頁面,在對應(yīng)的controller的action方法中,會使用$_POST['RegisterForm'] 來獲取表單輸入項的值,那么$_POST中的屬性名(如RegisterForm)是由什么決定的呢?

是由render方法的第二個參數(shù)決定的

?

(5)如何區(qū)分場景

比如用戶注冊時需要輸入email和repassword,而用戶登錄時不需要輸入email和repassword。這就是不同的場景校驗的要素不同。如何實現(xiàn)呢?

在RegisterForm 的rules()方法中

public function rules()	{		return array(			// username and password are required			array('username, password', 'required'),			array('email,repassword', 'required','on'=>'register'),			// rememberMe needs to be a boolean			array('rememberMe', 'boolean','on'=>'login'),			// password needs to be authenticated			array('password', 'authenticate','on'=>'login'),		);	}

?'on'=>'register' 表示在“register”場景中才需要校驗。

那么如何選擇場景呢?

在controller 中new RegisterForm 時指定

$model=new RegisterForm('register');

?

例如

array('username, password', 'required', 'on'=>'login, register'),
array('email', 'required', 'on'=>'register'),
如上所示, username 和password 特性在login 場景中是必填項。而username, password 和email 特性在register 場
景中是必填項。于是,如果我們在login 場景中執(zhí)行塊賦值,就只有username 和password 會被塊賦值。因為只有它
們出現(xiàn)在login 的驗證規(guī)則中。另一方面,如果場景是register ,這三個特性就都可以被塊賦值。
// 在登錄場景中
$model=new User('login');
if(isset($_POST['User']))
$model->attributes=$_POST['User'];
// 在注冊場景中
$model=new User('register');
if(isset($_POST['User']))
$model->attributes=$_POST['User'];

?

?(6)如何批量獲取表單的輸入值

$model->attributes=$_POST['RegisterForm'];

?在一個類的實例被創(chuàng)建后,我們通常需要用最終用戶提交的數(shù)據(jù)填充它的特性。這可以通過如下塊賦值( massive
assignment)方式輕松實現(xiàn):
$model=new LoginForm;
if(isset($_POST['LoginForm']))
$model->attributes=$_POST['LoginForm'];
最后的表達式被稱作塊賦值(massive assignment) ,它將$_POST['LoginForm'] 中的每一項復(fù)制到相應(yīng)的模型特性
。這相當(dāng)于如下賦值方法:
foreach($_POST['LoginForm'] as $name=>$value)
{
if($name 是一個安全的特性)
$model->$name=$value;
}

?

(7)表單中的* 是怎么產(chǎn)生的

是$model 中的rules() 方法決定的.

?我們看到郵箱沒有*,那是因為在RegisterForm 的rules() 沒有約束email 是必須的.

public function rules()	{		return array(			// username and password are required			array('username, password', 'required','on'=>'register,login'),			array('repassword', 'required','on'=>'register'),			// rememberMe needs to be a boolean			array('rememberMe', 'boolean','on'=>'login'),			// password needs to be authenticated			array('password', 'authenticate','on'=>'login'),			// 在注冊場景中,密碼repassword 必須和password 一致。			array('repassword', 'compare', 'compareAttribute'=>'password', 'on'=>'register'),		);	}

?

?

(8)include(authenticate.php): failed to open stream: No such file or directory

有問題的代碼:

?

原因:沒有authenticate()方法

把30注釋掉就好了.

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 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)

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ??of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ??of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

Detailed explanation of PHP array_fill() function usage Detailed explanation of PHP array_fill() function usage Jun 27, 2023 am 08:42 AM

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

Introduction to how to use the PHP array_change_key_case() function Introduction to how to use the PHP array_change_key_case() function Jun 27, 2023 am 10:43 AM

In PHP programming, array is a frequently used data type. There are also quite a few array operation functions, including the array_change_key_case() function. This function can convert the case of key names in the array to facilitate our data processing. This article will introduce how to use the array_change_key_case() function in PHP. 1. Function syntax and parameters array_change_ke

What are the common causes of ArrayIndexOutOfBoundsException in Java? What are the common causes of ArrayIndexOutOfBoundsException in Java? Jun 24, 2023 pm 10:39 PM

Java is a very powerful programming language that is widely used in various development fields. However, during Java programming, developers often encounter ArrayIndexOutOfBoundsException exceptions. So, what are the common causes of this anomaly? ArrayIndexOutOfBoundsException is a common runtime exception in Java. It means that when accessing data, the array subscript exceeds the range of the array. Common reasons include

How to convert LinkedList to Array in Java? How to convert LinkedList to Array in Java? Aug 29, 2023 pm 11:09 PM

The toArray() method of the LinkedList class converts the current LinkedList object into an array of object type and returns it. This array contains all the elements in this list in correct order (from first element to last element). It acts as a bridge between array-based and collection-based APIs. So, convert LinkedList to array - instantiate LinkedList class. Populate it using the add() method. Call the toArray() method on the linked list created above and retrieve the array of objects. Converts each element of an array of objects to a string. Example Real-time demonstration of importjava.util.Arrays;importjava.uti

Solution to ArrayStoreException exception in Java Solution to ArrayStoreException exception in Java Jun 25, 2023 am 08:05 AM

In Java development, we often use arrays to store a series of data because of the convenience and performance advantages of arrays. However, in the process of using arrays, some exceptions will occur, and one of the common exceptions is ArrayStoreException. This exception is thrown when we store incompatible data types in the array. This article will introduce what an ArrayStoreException is, why it occurs, and how to solve it. 1. Arr

See all articles