Yii Framework Forum: Active Record Problem - Yii Framework Forum

Jump to content

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

Active Record Problem Rate Topic: -----

#1 User is offline   nick121 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 03-December 12

Posted 03 December 2012 - 01:59 PM

I have a problem creating new records using active record.

I made a database connection to a MYSQL database which is working properly.

Next i made a user model class of the tbl_user table using gii tools.

<?php

/**
 * This is the model class for table "tbl_user".
 *
 * The followings are the available columns in table 'tbl_user':
 * @property integer $id
 * @property string $email
 * @property string $password
 * @property string $name
 * @property string $address
 * @property double $height
 * @property double $weight
 * @property string $birth
 * @property integer $sex
 * @property string $registered_at
 */
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 static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_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('email, password, name, address, height, weight, birth, sex, registered_at', 'required'),
			array('sex', 'numerical', 'integerOnly'=>true),
			array('height, weight', 'numerical'),
			array('email, password, name', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, email, password, name, address, height, weight, birth, sex, registered_at', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @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',
			'email' => 'Email',
			'password' => 'Password',
			'name' => 'Name',
			'address' => 'Address',
			'height' => 'Height',
			'weight' => 'Weight',
			'birth' => 'Birth',
			'sex' => 'Sex',
			'registered_at' => 'Registered At',
		);
	}

	/**
	 * 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('email',$this->email,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('height',$this->height);
		$criteria->compare('weight',$this->weight);
		$criteria->compare('birth',$this->birth,true);
		$criteria->compare('sex',$this->sex);
		$criteria->compare('registered_at',$this->registered_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}


In the SiteController i created a register action where i create a new user, insert the data and want to save it to the database using $user->save(); but i dont get the save option, i only get the functions defined in the user model class to show up.

        public function actionRegister()
	{
		$model=new RegisterForm;

		// 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['RegisterForm']))
		{
			$model->attributes=$_POST['RegisterForm'];
			// validate user input and redirect to the next page if valid
			if($model->validate())
                                $user=new User;
                                $user->email = $model->email;
                                $user->password = $model->password;
                                
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the register form
		$this->render('register',array('model'=>$model));
	}


So what am i missing here? Why isnt the ->save(); function showing up?
0

#2 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 603
  • Joined: 08-May 09

Posted 03 December 2012 - 05:14 PM

you mean, there is a problem with your IDE?
0

#3 User is offline   nick121 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 03-December 12

Posted 03 December 2012 - 07:50 PM

The problem is, i'm not sure what the problem is.

I'm using netbeans 7.2.1 right now, i'll look into it and maybe use another one.
0

#4 User is offline   nick121 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 03-December 12

Posted 04 December 2012 - 11:18 AM

Ok i fixed it by including the yii folder as a php include path

Thx for pointing me in the right direction.
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