Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Yii] Wstawienie danych do bazy - rzuca wyjątek
cod3r
post
Post #1





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 4.03.2011
Skąd: Gdańsk

Ostrzeżenie: (0%)
-----


Witam,

Framework na którym operuje to Yii jak widać w tytule. Problem dotyczy wstawienia danych do bazy, które są przesłane z formularza. A więc tak mam formularz register.php (widok)
  1. <h1>Registration</h1>
  2.  
  3. <?php if(Yii::app()->user->hasFlash('register')): ?>
  4.  
  5. <div class="flash-success">
  6. <?php echo Yii::app()->user->getFlash('register'); ?>
  7. </div>
  8.  
  9. <?php else: ?>
  10.  
  11.  
  12. <div class="form">
  13.  
  14. <?php $form=$this->beginWidget('CActiveForm', array(
  15. 'id'=>'register-form-register-form',
  16. 'enableAjaxValidation'=>true,
  17. )); ?>
  18.  
  19. <p class="note">Fields with <span class="required">*</span> are required.</p>
  20.  
  21. <?php echo $form->errorSummary($model); ?>
  22.  
  23. <div class="row">
  24. <?php echo $form->labelEx($model,'username'); ?>
  25. <?php echo $form->textField($model,'username'); ?>
  26. <?php echo $form->error($model,'username'); ?>
  27. </div>
  28.  
  29. <div class="row">
  30. <?php echo $form->labelEx($model,'password'); ?>
  31. <?php echo $form->textField($model,'password'); ?>
  32. <?php echo $form->error($model,'password'); ?>
  33. </div>
  34.  
  35. <div class="row">
  36. <?php echo $form->labelEx($model,'email'); ?>
  37. <?php echo $form->textField($model,'email'); ?>
  38. <?php echo $form->error($model,'email'); ?>
  39. </div>
  40.  
  41.  
  42. <div class="row buttons">
  43. <?php echo CHtml::submitButton('Submit'); ?>
  44. </div>
  45.  
  46. <?php $this->endWidget(); ?>
  47.  
  48. </div><!-- form -->
  49.  
  50. <?php endif; ?>


następnie dane przesyłane są do SiteController.php (kontrolera) w którym odbywa się walidacja
  1. <?php
  2.  
  3. class SiteController extends Controller
  4. {
  5.  
  6. //inne metody
  7. .
  8. .
  9. .
  10.  
  11. //rejestracja
  12. public function actionRegister()
  13. {
  14.  
  15. $model=new RegisterForm;
  16.  
  17. if(isset($_POST['RegisterForm']))
  18. {
  19. $model->attributes=$_POST['RegisterForm'];
  20. if($model->validate())
  21. {
  22. //wywolanie metody wykonujacej zapytanie
  23. $model->wstaw();
  24. Yii::app()->user->setFlash('register','dziekujemy za rejestracje.');
  25. $this->refresh();
  26. }
  27. }
  28. $this->render('register',array('model'=>$model));
  29. }
  30.  
  31. }


i wywoływane jest metoda wstaw z RegisterForm.php (modelu), która ma za zadanie wstawić dane do bazy.
  1. class RegisterForm extends CActiveRecord
  2. {
  3. //inne metody
  4. .
  5. .
  6. .
  7.  
  8. public function wstaw()
  9. {
  10. $connection=Yii::app()->db;
  11. $sql="INSERT INTO tbl_user (username,password,email) VALUES(:username,:password,:email)";
  12.  
  13.  
  14. $command=$connection->createCommand($sql);
  15. $command->bindParam(":username",$username,PDO::PARAM_STR);
  16. $command->bindParam(":password",$password,PDO::PARAM_STR);
  17. $command->bindParam(":email",$email,PDO::PARAM_STR);
  18. $command->execute();
  19.  
  20. }
  21. }


Niestety otrzymuję wyjątek CDbException o treści.
Cytat
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'username' cannot be null. The SQL statement executed was: INSERT INTO tbl_user (username,password,email) VALUES(:username,:password,:email)


Nie bardzo wiem jak to ugryźć, na pierwszy rzut oka wydaje się być wszystko w porządku. Może ktoś bardziej doświadczony posłuży radą (IMG:style_emoticons/default/smile.gif) .


EDIT:

Rozwiązałem to w inny sposób, a dokładniej za pomocą konstruktora zapytań:
  1. $command = Yii::app()->db->createCommand()
  2. ->insert('tbl_user', array(
  3. 'username'=>$this->username,
  4. 'password'=>MD5($this->password),
  5. 'email'=>$this->email,
  6. ));



Natomiast chętnie dowiem się dlaczego nie działa poprzedni kod.

Ten post edytował cod3r 28.09.2012, 22:50:10
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 01:49