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