Yii Framework Forum: Record not created, no exception thrown - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Record not created, no exception thrown Rate Topic: -----

#1 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 12 October 2009 - 12:49 PM

I am trying to create a user registration script, my user table looks like this
CREATE TABLE IF NOT EXISTS `user` (
  `id` int(100) NOT NULL AUTO_INCREMENT,
  `username` varchar(25) NOT NULL,
  `password` varchar(72) NOT NULL,
  `email` varchar(64) NOT NULL,
  `create_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `localeid` varchar(5) NOT NULL,
  `status` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`,`email`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


and the register action like
	public function actionRegister()
	{
		$model=new User;
		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			if($model->save())
				$this->redirect(array('site/index'));
		}
		$this->render('register',array('model'=>$model));
	}


now i believe the problem is what i have in my model. this is my method for validation rules
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, email', 'required'),
			array('username', 'length', 'max'=>25),
			array('password', 'length', 'max'=>72),
			array('email', 'length', 'max'=>64),
			array('create_time, update_time, localeid, status', 'safe'),
		);
	}


The record is created but if i uncomment this method i added
	public function beforeSave()
	{
		$this->localeid = Yii::app()->locale->getId();
		$this->status = 1;
	}


and submit the form then the form is just redisplayed and no error message is shown, yet the record will not be created in the DB. so is what i did in the beforeSave method wrong? if so, how can i set those fields in the model?
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#2 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 12 October 2009 - 12:58 PM

You should return true from beforeSave()

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 12 October 2009 - 01:05 PM

View Posttri, on 12 October 2009 - 12:58 PM, said:

You should return true from beforeSave()

/Tommy


Yep, figured it out just now, Thank you anyway.
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#4 User is offline   ps47r 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 18-September 09
  • Location:Turkey

Posted 12 October 2009 - 01:07 PM

i think the last statement in the beforeSave method should be this:
return true;


edit
sorry i think my reply was a little bit late :)
0

#5 User is offline   ECI 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-April 13

Posted 10 April 2013 - 10:26 AM

You could print the errors to see what happens:

	public function actionRegister()
	{
		$model=new User;
		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			if($model->save())
				$this->redirect(array('site/index'));
                        else
                                 print_r($model->getErrors()); exit;
		}
		$this->render('register',array('model'=>$model));
	}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users