save HAS_MANY

Well,

I have two tables : tbl_users from the excellente extension yii_user ans the table subscription with the relation ‘subscription’=>array(self::HAS_MANY, ‘Subscription’, ‘user_id’)

subscription.php :


<?php




class Subscription extends CActiveRecord

{

	

	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(

			'users' => array(self::BELONGS_TO, 'Users', 'users_id'),

		);

	}


	


        public static function add_date($day=0,$mth=0,$yr=0)

        {

            if ($day != 0) {$finaldate =strtotime('+'.$mth.' day',time());}

            if ($mth != 0) {$finaldate =strtotime('+'.$mth.' month',time());}

            if ($yr != 0) {$finaldate =strtotime('+'.$mth.' year',time());}


            return $finaldate;

        }

        

        protected function beforeSave()

        {


            if(parent::beforeSave())

            {

               $this->subscriptionstart=time();

               $this->subscriptionend=Subscription::add_date($day=0,$mth=Yii::app()->params['subcriptionperiodtry'],$yr=0);

                return true;

            }

            else

                return false;

        }

}

user.php :


<?php


class User extends CActiveRecord

{

	public function relations()

	{

		$relations = array(

			'profile'=>array(self::HAS_ONE, 'Profile', 'user_id'),

                        'subscription'=>array(self::HAS_MANY, 'Subscription', 'user_id'),

		);

		if (isset(Yii::app()->getModule('user')->relations)) $relations = array_merge($relations,Yii::app()->getModule('user')->relations);

		return $relations;

	}




          protected function afterSave() {

                if(parent::afterSave())

            {

                $this->subscription->user_id = $this->id;

                $this->subscription->Save();

                return true;

                }

            else

                return false;

        }


}

The user is created but not the subscription…could you help me ?

Thanks

It could be some problems with saving because not to pass validation rules by your record.

Thanks for your answer. Do you know how to debug this case ?

Please let me see method rules() from subscriptions model

Yes, and thanks :


<?php


/**

 * This is the model class for table "{{subscription}}".

 *

 * The followings are the available columns in table '{{subscription}}':

 * @property string $id

 * @property integer $users_id

 * @property integer $subscriptionstart

 * @property integer $subscriptionend

 * @property string $amount

 *

 * The followings are the available model relations:

 * @property Users $users

 */

class Subscription extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @return Subscription 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 '{{subscription}}';

	}


	/**

	 * @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('users_id, subscriptionstart, subscriptionend, amount', 'required'),

			//array('users_id, subscriptionstart, subscriptionend', 'numerical', 'integerOnly'=>true),

			//array('users_id', 'required'),

                    //array('users_id', 'numerical', 'integerOnly'=>true),

                    //array('users_id', 'default', 'value'=>Yii::app()->user->id),

                    //array('users_id', 'default', 'value'=>'2'),

                    

                    //array('amount', 'length', 'max'=>10),

                    //array('amount', 'default', 'value'=>Yii::app()->params['subcriptionamount']),

                        array('amount', 'default', 'value'=>Yii::app()->params['subcriptionamount']),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, users_id, subscriptionstart, subscriptionend, amount', '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(

			'users' => array(self::BELONGS_TO, 'Users', 'users_id'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'users_id' => 'Users',

			'subscriptionstart' => 'Subscriptionstart',

			'subscriptionend' => 'Subscriptionend',

			'amount' => 'Amount',

		);

	}


	/**

	 * 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,true);

		$criteria->compare('users_id',$this->users_id);

		$criteria->compare('subscriptionstart',$this->subscriptionstart);

		$criteria->compare('subscriptionend',$this->subscriptionend);

		$criteria->compare('amount',$this->amount,true);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}


        public static function add_date($day=0,$mth=0,$yr=0)

        {

            if ($day != 0) {$finaldate =strtotime('+'.$mth.' day',time());}

            if ($mth != 0) {$finaldate =strtotime('+'.$mth.' month',time());}

            if ($yr != 0) {$finaldate =strtotime('+'.$mth.' year',time());}


            return $finaldate;

        }

        

        protected function beforeSave()

        {


            if(parent::beforeSave())

            {

               $this->subscriptionstart=time();

               $this->subscriptionend=Subscription::add_date($day=0,$mth=Yii::app()->params['subcriptionperiodtry'],$yr=0);

                return true;

            }

            else

                return false;

        }

}