最近在學(xué)習(xí)php 一個(gè)流行的框架yii,聽說封裝得很好,但是在學(xué)習(xí)過程中也遇到了一些問題,先總結(jié)如下:
?
(1)The table "{{tbl_user}}" for active record class "User" cannot be found in the database.
詳細(xì)錯(cuò)誤:
?錯(cuò)誤原因:實(shí)體類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 。
解決方法:
方法一:去掉兩邊的花括號(hào),改為:
/** * @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,但是實(shí)際上$password,$this->password 是相等。
(3)php中調(diào)用成員變量時(shí)使用->,而不是.
有java工作經(jīng)驗(yàn)的開發(fā)者,在使用yii框架時(shí)非常容易使用. 來引用成員變量,這是java的語法,不是php的語法。
?
(4)對(duì)于注冊(cè)頁面,在對(duì)應(yīng)的controller的action方法中,會(huì)使用$_POST['RegisterForm'] 來獲取表單輸入項(xiàng)的值,那么$_POST中的屬性名(如RegisterForm)是由什么決定的呢?
是由render方法的第二個(gè)參數(shù)決定的
?
(5)如何區(qū)分場景
比如用戶注冊(cè)時(shí)需要輸入email和repassword,而用戶登錄時(shí)不需要輸入email和repassword。這就是不同的場景校驗(yàn)的要素不同。如何實(shí)現(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”場景中才需要校驗(yàn)。
那么如何選擇場景呢?
在controller 中new RegisterForm 時(shí)指定
$model=new RegisterForm('register');
?
例如
array('username, password', 'required', 'on'=>'login, register'),
array('email', 'required', 'on'=>'register'),
如上所示, username 和password 特性在login 場景中是必填項(xiàng)。而username, password 和email 特性在register 場
景中是必填項(xiàng)。于是,如果我們?cè)趌ogin 場景中執(zhí)行塊賦值,就只有username 和password 會(huì)被塊賦值。因?yàn)橹挥兴?br>們出現(xiàn)在login 的驗(yàn)證規(guī)則中。另一方面,如果場景是register ,這三個(gè)特性就都可以被塊賦值。
// 在登錄場景中
$model=new User('login');
if(isset($_POST['User']))
$model->attributes=$_POST['User'];
// 在注冊(cè)場景中
$model=new User('register');
if(isset($_POST['User']))
$model->attributes=$_POST['User'];
?
?(6)如何批量獲取表單的輸入值
$model->attributes=$_POST['RegisterForm'];
?在一個(gè)類的實(shí)例被創(chuàng)建后,我們通常需要用最終用戶提交的數(shù)據(jù)填充它的特性。這可以通過如下塊賦值( massive
assignment)方式輕松實(shí)現(xiàn):
$model=new LoginForm;
if(isset($_POST['LoginForm']))
$model->attributes=$_POST['LoginForm'];
最后的表達(dá)式被稱作塊賦值(massive assignment) ,它將$_POST['LoginForm'] 中的每一項(xiàng)復(fù)制到相應(yīng)的模型特性
中。這相當(dāng)于如下賦值方法:
foreach($_POST['LoginForm'] as $name=>$value)
{
if($name 是一個(gè)安全的特性)
$model->$name=$value;
}
?
(7)表單中的* 是怎么產(chǎn)生的
是$model 中的rules() 方法決定的.
?我們看到郵箱沒有*,那是因?yàn)樵赗egisterForm 的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'), // 在注冊(cè)場景中,密碼repassword 必須和password 一致。 array('repassword', 'compare', 'compareAttribute'=>'password', 'on'=>'register'), ); }
?
?
(8)include(authenticate.php): failed to open stream: No such file or directory
有問題的代碼:
?
原因:沒有authenticate()方法
把30注釋掉就好了.