E
E
EVOSandru62015-01-29 12:05:38
Yii
EVOSandru6, 2015-01-29 12:05:38

How to correctly make actionCreate for a model without links in yii?

Good afternoon!
Such problem, I do registration for users.
There is a User model , it has native fields from the table: CODE, NAME, PASSWORD, ROLE, EMAIL
I want the user to enter the password twice during registration so as not to make a mistake.
Added 2 fields to the User model :

// каптча
public $verifyCode;
// повторный пароль для регистрации
public $verifyPassword;

In UserController for captcha I wrote:
public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xFFFFFF,
            ),
        );
    }

It seems that there are no problems with this. The form is as follows (added a verification password and captcha):
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'reg-form',
    'enableClientValidation' => true,
    'clientOptions' => array(
        'validateOnSubmit' => true,
    ),
    'htmlOptions'=>array(
        'class'=>'form',
        'style' => 'padding:10px;'
    ),
    'action' => array('user/registration'), // когда форма показывается и в других контроллерах, не только 'site', то я в каждый из этих контроллеров вставил actionQuick, a здесь указал — array('quick'); почему-то не получается с array('//site/quick')
));?>

<div class="form">
    <p class="note"><span class="required">*</span> Поля обязательные для заполнения.</p>
    <?php echo $form->errorSummary($model); ?>
    <div class="row">
        <?php echo $form->labelEx($model,'NAME'); ?>
        <?php echo $form->textField($model,'NAME',array('size'=>60,'maxlength'=>100)); ?>
        <?php echo $form->error($model,'NAME'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'LOGIN'); ?>
        <?php echo $form->textField($model,'LOGIN',array('size'=>60,'maxlength'=>100)); ?>
        <?php echo $form->error($model,'LOGIN'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'PASSWORD'); ?>
        <?php echo $form->passwordField($model,'PASSWORD',array('size'=>32,'maxlength'=>32)); ?>
        <?php echo $form->error($model,'PASSWORD'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'verifyPassword'); ?>
        <?php echo $form->passwordField($model,'verifyPassword',array('size'=>32,'maxlength'=>32)); ?>
        <?php echo $form->error($model,'verifyPassword'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'EMAIL'); ?>
        <?php echo $form->textField($model,'EMAIL',array('size'=>60,'maxlength'=>100)); ?>
        <?php echo $form->error($model,'EMAIL'); ?>
    </div>
    <?php if(CCaptcha::checkRequirements()): ?>
        <div class="row">
            <?php echo $form->labelEx($model,'verifyCode'); ?>
            <div>
                <?//php $this->widget('CCaptcha'); ?>
                <?php $this->widget('CCaptcha',
                    array('captchaAction' => '/site/captcha')
                );?>
                <?php echo $form->textField($model,'verifyCode'); ?>
            </div>
            <div class="hint">
                <!--перевод-->
                <?php echo Translated::model()->findByAttributes(array('PARAM' => array('text_from_image')))->getName();?>
            </div>
            <?php echo $form->error($model,'verifyCode');?>
        </div>
    <?php endif; ?>
    <div class="row buttons">
        <?php echo CHtml::submitButton('Зарегистрироваться');
        // $model->isNewRecord ?
        ?>
    </div>
</div><!-- form -->
<?php $this->endWidget();?>

registration action Like this:
public function actionRegistration()
    {
        $model = new User();
        //$model->scenario = 'registration';
        if(isset($_POST['User']))
        {
            //My::printArr($_POST['User']);
            if($model->PASSWORD == $model->verifyPassword){
                $model->attributes=$_POST['User'];
                if($model->save())
                    Yii::app()->user->setFlash('success', 'Вы успешно зарегистрировались!');
                else
                    Yii::app()->user->setFlash('error', 'Не удалось зарегистрировать Вас');
            }
            else{
                Yii::app()->user->setFlash('error', 'Пароли не совпадают');
                $this->redirect(Yii::app()->request->requestUri);
            }
        }
        $this->redirect(Yii::app()->request->urlReferrer);
    }

At the moment when all the data is entered into the form and the register button is pressed, the following curse comes out:
CDbCommand не удалось исполнить SQL-запрос: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'. The SQL statement executed was: INSERT INTO `m_users` (`EXIST`, `NAME`, `LOGIN`, `PASSWORD`, `ROLE`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4)

Why do VALUES have such weird values? It $model->save()'s not being processed. Is it because there are 2 extra ones in the model, not from the property table and something breaks?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom, 2015-01-29
@EVOSandru6

Most likely you don't have auto_increment in your database for PK, and the default value '0' is duplicated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question