2012-03-01 3 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(); 

エラーなしで実行されますが、データベースには何も挿入されません。しかし、実際には、アクティブレコードを介してデータベースからrawデータを読み取ることができるため、データベース接続がアクティブになっています。アクティブレコードを介してデータベースに新しいデータを挿入できないという問題があります。

だからアイデアはありますか?

+0

てみました。この問題を解決するのに役立つだろうと確信しています。 –

+0

'var_dump($ user-> getErrors()); 'を追加してみてください – Alex

+0

なぜモデルで変数を宣言しましたか? –

答えて

1

はこれを試してください:あなたはの `config/main.php`からウェする

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