2012-03-01 166 views
1

我正在使用Yii框架,我想創建一個註冊頁面。我創建了所需的所有東西,當我註冊用戶時,我沒有收到任何錯誤,但是在數據庫中找不到記錄。你能幫助我嗎?這裏是我的代碼:Yii無法完成任務,完全沒有任何錯誤

SiteController.php:

public function actionRegister() 
{ 
    $model=new RegisterForm; 

    // uncomment the following code to enable ajax-based validation 

    if(isset($_POST['ajax']) && $_POST['ajax']==='register-form') 
    { 
     echo CActiveForm::validate($model); 
     Yii::app()->end(); 
    } 

    if(isset($_POST['RegisterForm'])) 
    { 
     $model->attributes=$_POST['RegisterForm']; 
     if($model->validate()) 
     { 
      $user = new User; 
      $user->username = $_POST['RegisterForm']['username']; 
      $this->password = $_POST['RegisterForm']['password']; 
      $this->salt = Security::GenerateSalt(128); 
      $user->password = hash('sha512', $password.$salt); 
      $user->question = $_POST['RegisterForm']['question']; 
      $user->answer = $_POST['RegisterForm']['answer']; 
      $user->salt = $salt; 
      $user->email = $_POST['RegisterForm']['email']; 
      $user->fullname = $_POST['RegisterForm']['fullname']; 
      $user->birth = $_POST['RegisterForm']['dd'].'/'.$_POST['RegisterForm']['mm'].'/'.$_POST['RegisterForm']['yy']; 
      $user->avatar = CUploadedFile::getInstance($model,'image'); 
      $user->about = $_POST['RegisterForm']['about']; 
      $user->sighnup = date('d/m/y'); 
      $user->login = date('d/m/y'); 
      if($user->save()) 
      { 
       $model->image->saveAs('images/users/localFile.jpg'); 
       // redirect to success page 
      } 

      $activation = new Activation; 
      $record = User::model()->findByAttributes(array('username'=>$_POST['RegisterForm']['username'])); 
      $activation->user = $record->id; 
      $activation->code = Security::ActivationCode(32); 
      $activation->save(); 
     } 
    } 
    $this->render('register',array('model'=>$model)); 
} 

RegisterForm.php驗證模型:

class RegisterForm extends CFormModel 
{ 
    public $username; 
    public $password; 
    public $password2; 
    public $question; 
    public $answer; 
    public $email; 
    public $fullname; 
    public $avatar; 
    public $about; 
    public $dd; 
    public $mm; 
    public $yy; 
    public $verifyCode; 

    public function rules() 
    { 
     return array(
      array('username, password, password2, question, answer, email, fullname, dd, mm, yy', 'required'), 

      array('username','length','min'=>4), 
      array('username','length','max'=>16), 
      array('username', 'filter', 'filter'=>'strtolower'), 
      array('username', 'ext.alpha', 'allowSpaces'=>'flase', 'allowNumbers'=>'true', 'allAccentedLetters'=>'false'), 
      array('username', 'unique', 'attributeName'=> 'username', 'className'=>'User' ,'caseSensitive' => 'false'), 

      array('password', 'length', 'min' =>6), 
      array('password', 'length', 'max' =>32), 
      array('password2', 'compare', 'allowEmpty' => 'false', 'compareAttribute' => 'password'), 

      array('question', 'length', 'min' =>10), 
      array('question', 'length', 'max' =>128), 

      array('answer', 'length', 'min' =>4), 
      array('answer', 'length', 'max' =>64), 

      array('email', 'length', 'min' =>8), 
      array('email', 'length', 'max' =>128), 
      array('email', 'email'), 
      array('email', 'unique', 'attributeName'=> 'email', 'className'=>'User' ,'caseSensitive' => 'false'), 

      array('fullname','length','min'=>6), 
      array('fullname','length','max'=>64), 
      array('fullname', 'ext.alpha', 'allowNumbers'=>'false', 'allowSpaces'=>'true', 'allAccentedLetters'=>'false'), 

      array('avatar', 'file', 
           'types'=>'jpg, gif, png', 
           'maxSize'=>1024 * 1024 * 2, 
           'tooLarge'=>'The file was larger than 2MB. Please upload a smaller file.', 
           'wrongType'=>'Please upload only images in the format jpg, gif, png', 
           'tooMany'=>'You can upload only 1 avatar', 
         'on'=>'upload'), 

      array('about','length','max'=>384), 

      array('dd', 'numerical', 'allowEmpty'=>'false', 'integerOnly'=>'true', 'min'=>'1', 'max'=>'31'), 
      array('dd', 'FilterDay'), 
      array('mm', 'numerical', 'allowEmpty'=>'false', 'integerOnly'=>'true', 'min'=>'0', 'max'=>'12'), 
      array('mm', 'FilterMonth'), 
      array('yy', 'numerical', 'allowEmpty'=>'false', 'integerOnly'=>'true', 'min'=>'1900', 'max'=>'2008'), 
      array('yy', 'FilterYear'), 

      array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')), 
     ); 
    } 

    public function attributeLabels() 
    { 
     return array(
      'username' => 'Username', 
      'password' => 'Passwrod', 
      'password2' => 'Verify Password', 
      'email' => 'Email', 
      'fullname' => 'Full Name', 
      'dd' => 'Day', 
      'mm' => 'Month', 
      'yy' => 'Year', 
     ); 
    } 
} 

Register.php一個視圖文件:

<div class="form"> 
<h1><?php echo($data); ?></h1> 
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'register-form', 
    'enableAjaxValidation'=>true, 
)); ?> 

    <p class="note">Fields with <span class="required">*</span> are required.</p> 

    <?php echo $form->errorSummary($model); ?> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'username'); ?> 
     <?php echo $form->textField($model,'username'); ?> 
     <?php echo $form->error($model,'username'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'password'); ?> 
     <?php echo CHtml::activePasswordField($model,'password'); ?> 
     <?php echo $form->error($model,'password'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'password2'); ?> 
     <?php echo CHtml::activePasswordField($model,'password2'); ?> 
     <?php echo $form->error($model,'password2'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'email'); ?> 
     <?php echo $form->textField($model,'email'); ?> 
     <?php echo $form->error($model,'email'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'fullname'); ?> 
     <?php echo $form->textField($model,'fullname'); ?> 
     <?php echo $form->error($model,'fullname'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'question'); ?> 
     <?php echo $form->textField($model,'question'); ?> 
     <?php echo $form->error($model,'question'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'answer'); ?> 
     <?php echo $form->textField($model,'answer'); ?> 
     <?php echo $form->error($model,'answer'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'avatar'); ?> 
     <?php echo CHtml::activeFileField($model, 'avatar'); ?> 
     <?php echo $form->error($model,'avatar'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'about'); ?> 
     <?php echo CHtml::activeTextArea($model, 'about'); ?> 
     <?php echo $form->error($model,'about'); ?> 
    </div> 

<?php 
function getYear($value1 = 1900, $value2 = 2008) 
{ 
    $data = array(); 

    for ($i = $value2; $i >= $value1; $i--){ 
     $data[$i] = (string)$i; 
    } 
    return $data; 
} 

function getMonth() 
{ 
    $data = array(); 

    for ($i = 1; $i <= 12; $i++){ 
     $data[$i] = (string)$i; 
    } 
    return $data; 
} 

function getDays() 
{ 
    $data = array(); 

    for ($i = 1; $i <= 31; $i++){ 
     $data[$i] = (string)$i; 
    } 
    return $data; 
} 
?> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'dd'); ?> 
     <?php echo CHtml::activeDropDownList($model,'dd', getDays()); ?> 
     <?php echo $form->error($model,'dd'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'mm'); ?> 
     <?php echo CHtml::activeDropDownList($model,'mm', getMonth()); ?> 
     <?php echo $form->error($model,'mm'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'yy'); ?> 
     <?php echo CHtml::activeDropDownList($model,'yy', getYear()); ?> 
     <?php echo $form->error($model,'yy'); ?> 
    </div> 

    <?php if(extension_loaded('gd')): ?> 
    <div class="simple"> 
     <?php echo CHtml::activeLabel($model,'verifyCode', array('style'=>'width:150px;')); ?> 
     <div> 
     <?php $this->widget('CCaptcha'); ?> 
     <?php echo CHtml::activeTextField($model,'verifyCode'); ?> 
     </div> 
     <p class="hint">Please enter the letters as they are shown in the image above. 
     <br/>Letters are not case-sensitive.</p> 
    </div> 
    <?php endif; ?> 

    <div class="row buttons"> 
     <?php echo CHtml::submitButton('Submit'); ?> 
    </div> 

<?php $this->endWidget(); ?> 

</div><!-- form --> 

我根本沒有任何錯誤,但事實是,根本沒有數據庫中的帖子,請幫助我

---------------------------------------------- ---------- 編輯 ----------------------------------- ----------------------------------

我發現了什麼問題。問題是我無法通過活動記錄在數據庫中插入信息。當我鍵入:

$user = new User; 
$user->username = 'irakli'; 
$user->save(); 

它運行沒有錯誤,但它不會在數據庫中插入任何內容。但事實是數據庫連接是激活的,因爲我可以通過Active Record從數據庫讀取原始數據,問題是我無法通過Active Record在數據庫中插入新數據。

那麼有什麼想法?

+0

你嘗試從'配置/ main.php'的博客上。我相信它會幫助你解決這個問題。 – 2012-03-01 17:13:33

+0

嘗試添加'var_dump($ user-> getErrors());' – Alex 2012-03-01 18:59:49

+0

爲什麼要在模型中聲明變量? – 2012-03-01 20:44:11

回答

1

試試這個:

$user = new User; 
$user->username = 'irakli'; 
if ($user->save()) 
{ 
    echo "user record saved to the db"; 
} 
else 
{ 
    var_dump($user->getErrors()); 
}