最近在學(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注釋掉就好了.