Szevasztok
Elkezdtem egy oldalt készíteni yii-ben megcsináltam a regisztrációt meg a login oldalt, de van egy probléma.
Amikor ékezetes karaktert írok bele a textfield-be akkor hibát kapok és nem menti ez az adatbázisba.
Nyílván Rule-ban lehet szabályozni hogy ne lehessen ékezetest beírni, de én ott pont akarok ékezetest is írni.
Mutatom a hibát:
CDbCommand hibába ütközött az SQL parancs végrehajtása közben: SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xE3\xA1\xE3\xA1\xE3\xA1...' for column 'question' at row 1. The SQL statement executed was: INSERT INTO `user` (`username`, `password`, `email`, `question`, `answer`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4)
Mysql adatbázis van uf8_general_ci-vel
a fileokat BOM nélkül mentem.
de ezt a hibát szerintem a model->save(); generálja ahol rosszúl menti az adatot.
Valaki találkozott már ilyen problémával,és mi volt a megoldás?
Page 1 of 1
Yii ékezetes karakterek mentése
#2
Posted 26 March 2012 - 12:53 AM
Ilyen hibát nem tapasztaltam eddig Yii-ben. Alapvetően AR mintát használtam és kevésbé hívtam meg a CDbCommand parancsot, de ha igen, akkor sem volt gond. Talán másoljál be valami kódrészletet, mert lehet máshol lesz a hiba. Adatbázis kapcsolat és az érdekelne valamivel részletesebben, hogy segíteni tudjak.
#3
Posted 26 March 2012 - 01:03 AM
ez van a protected/config/main.php-ben
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=legjobbfilm',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
SiteController-ben meg ez
public function actionRegister()
{
$model=new User;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='register-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->save())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('register',array('model'=>$model));
}
User model-t meg gii vel legyártottam.
A form pedig így néz ki
<?php
$this->pageTitle=Yii::app()->name . ' - Regisztráció';
$this->breadcrumbs=array(
'Regisztráció',
);
?>
<h1>Regisztráció</h1>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'register-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<p class="note">A <span class="required">*</span> jelzett mezők kitöltése kötelező!.</p>
<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 $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password2'); ?>
<?php echo $form->passwordField($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,'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>
<?php if(extension_loaded('gd')): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
</div>
<?php endif; ?>
<div class="row buttons">
<?php echo CHtml::submitButton('Regisztráció'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=legjobbfilm',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
SiteController-ben meg ez
public function actionRegister()
{
$model=new User;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='register-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->save())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('register',array('model'=>$model));
}
User model-t meg gii vel legyártottam.
A form pedig így néz ki
<?php
$this->pageTitle=Yii::app()->name . ' - Regisztráció';
$this->breadcrumbs=array(
'Regisztráció',
);
?>
<h1>Regisztráció</h1>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'register-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<p class="note">A <span class="required">*</span> jelzett mezők kitöltése kötelező!.</p>
<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 $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password2'); ?>
<?php echo $form->passwordField($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,'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>
<?php if(extension_loaded('gd')): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
</div>
<?php endif; ?>
<div class="row buttons">
<?php echo CHtml::submitButton('Regisztráció'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
#4
Posted 26 March 2012 - 01:29 AM
Szerintem ezek a kódok teljesen rendben vannak. A Gii által legyártott model-re gyanakszom. Légyszi azt is írd meg!
Érdemes használnod a kódkiemelést itt a fórumon sokkal jobban olvashatóak így az üzeneteid.
Érdemes használnod a kódkiemelést itt a fórumon sokkal jobban olvashatóak így az üzeneteid.
<?php echo "így érdemes a kódokat beszúrni..." ?>
#5
Posted 26 March 2012 - 01:33 AM
class User extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return User the static model class
*/
public $password2;
public $verifyCode;
public $rememberMe;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'user';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array(array('username','password','password2'), 'match', 'pattern' => '/^[A-Za-z0-9]+$/u', 'message' => Yii::t('default', 'Ékezetes betű nem engedélyezett!')),
array('username','length','max'=>32),
// convert username to lower case
array('username', 'filter', 'filter'=>'strtolower'),
array('username','length','max'=>10, 'min'=>3),
array('password','length','max'=>64, 'min'=>6),
array('password2','length','max'=>64, 'min'=>6),
// compare password to repeated password
array('password2', 'compare', 'compareAttribute'=>'password'),
array('email','length','max'=>256),
// make sure email is a valid email
array('email','email'),
// make sure username and email are unique
array('username, email', 'unique'),
array('question','length','max'=>256),
// convert question to lower case
array('question', 'filter', 'filter'=>'strtolower'),
array('answer','length','max'=>128),
// convert answer to lower case
array('answer', 'filter', 'filter'=>'strtolower'),
array('username, password, password2, email, question, answer, verifyCode', 'required'),
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'username' => 'Felhasználónév',
'password' => 'Jelszó',
'password2' => 'Jelszó ismét',
'email' => 'Email',
'question' => 'Biztonsági kérdés',
'answer' => 'Válasz',
'verifyCode' => 'Megerősítő kód',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
//$criteria=new CDbCriteria;
//$criteria->compare('id',$this->id);
//$criteria->compare('username',$this->username,true);
//$criteria->compare('password',$this->password,true);
//$criteria->compare('email',$this->email,true);
//$criteria->compare('question',$this->question,true);
//$criteria->compare('answer',$this->answer,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public function beforeSave()
{
// Check if question has a ? at the end
$last = $this->question[strlen($this->question)-1];
if($last !== "?")
{
$this->question .= '?';
}
$pass = md5(md5($this->password).Yii::app()->params["salt"]);
$this->password = $pass;
return true;
}
}
#6
Posted 26 March 2012 - 01:58 AM
A beforeSave metódus nem a Gii része. 
Ha UTF-8-ban dolgozol, akkor az mb_**** parancsok használata javallott. Azaz írtál egy ilyen részt:
A második elgondolkoztató kérdés, hogy az alapértelmezett Rule, hogy boldogul a magyar karakterekkel. Érdemes írnod egy új Rule-t.
<?php array('username', 'filter', 'filter'=>'strtolower'), // .. ?>
A rule-ban a gyári Gii kódhoz képest változtattál. Arra gyanakszom, hogy ott lesz a hiba, bár nem láttam most elsőre. Azt alaposabban nézd át!
Ha UTF-8-ban dolgozol, akkor az mb_**** parancsok használata javallott. Azaz írtál egy ilyen részt:
...
<?php
// strlen($this->question)-1
mb_strlen($this->question)-1 // -->
?>
...
A második elgondolkoztató kérdés, hogy az alapértelmezett Rule, hogy boldogul a magyar karakterekkel. Érdemes írnod egy új Rule-t.
<?php array('username', 'filter', 'filter'=>'strtolower'), // .. ?>
A rule-ban a gyári Gii kódhoz képest változtattál. Arra gyanakszom, hogy ott lesz a hiba, bár nem láttam most elsőre. Azt alaposabban nézd át!
#7
Posted 26 March 2012 - 02:13 AM
Nekem volt hasonló problémám, de nem a Yii oldalon keresendő a hiba.
Db-ben mindent erre állíts át(táblát és kapcsolatot is): utf8_hungarian_ci
Üdv!
Db-ben mindent erre állíts át(táblát és kapcsolatot is): utf8_hungarian_ci
Üdv!
#8
Posted 27 March 2012 - 12:44 AM
Meglett a probléma .. A yii-ben minden jó volt csak a szerver programot kellett újra telepítenem.
Gondolom mysql-ben valamikor valamit elállítottam valamiért
Gondolom mysql-ben valamikor valamit elállítottam valamiért
Share this topic:
Page 1 of 1

Help












