【问题标题】:Yii can't do the task, with getting no errors at allYii 无法完成任务,完全没有错误
【发布时间】:2012-03-01 17:00:53
【问题描述】:

我正在使用 Yii 框架,我想制作一个注册页面。我已经创建了所有需要的东西,当我注册用户时,我没有收到任何错误,但我在数据库中找不到记录。所以你能帮帮我吗?这是我的代码:

站点控制器.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 在数据库中插入新数据。

有什么想法吗?

【问题讨论】:

  • 您是否尝试从config/main.php 访问博客。我相信它会帮助你解决这个问题。
  • 尝试添加var_dump($user-&gt;getErrors());
  • 为什么要在模型中声明变量?
  • protected/runtime/application.log 文件中没有错误?
  • 还发布了用户模型代码...我在 RegisterForm 模型中看不到任何名为 image 的变量。无论如何,这不应该阻止 user->save() 发生

标签: php database post frameworks yii


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多