Hi,
I’m hoping a member of the Yii forums may be able to shed some light on an issue I’m facing.
OBJECTIVE: Build a user ‘Signup’ form, which adds a new company and user to the application database.
At the moment I am trying this approach:
In controller action:
-
Take form input.
-
Create new Company model.
-
Assign Company model values using $company->attributes = $_POST[‘Company’];
-
Save Company.
-
Create new User model.
-
Add the new company ID to the input array: $_POST[‘User’][‘company_id’] = $company->id;
-
Assign User model values using $user-> attributes = $_POST[‘User’];
-
Save the User.
At the moment this is not working because when I run $user->save(), the model is not trying to save a value for ‘company_id’ into the database at all, dispute this being a required column.
I’ll post my code below because I’m not sure I’ve explained that very well, if anyone can see any obvious problems or knows of a tutorial which covers this please let me know.
Sorry about the massive first post!
- Controller Action
public function actionIndex()
{
$user = new User;
$company = new Company;
if($_POST){
$company->attributes = $_POST['Company'];
$user->attributes = $_POST['User'];
$_POST['User']['activation_code'] = 'test';
$_POST['User']['role_id'] = 1;
$_POST['User']['company_id'] = 1; // Temporary company ID to pass validation
if($company->validate() && $user->validate()){
if($company->save()){
$_POST['User']['company_id'] = $company->id; // Update the false company ID with a real one
if($user->save())
$this->redirect(array('confirmation', 'id'=>$user->id));
}
}else{
var_dump($company->getErrors());
var_dump($user->getErrors());
}
}
$this->render('index',array(
'user'=>$user,
'company'=>$company,
));
}
- Form
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'signup-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($user); ?>
<?php echo $form->errorSummary($company); ?>
<div class="row">
<?php echo $form->labelEx($user,'title'); ?>
<?php echo $form->textField($user,'title',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($user,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'email'); ?>
<?php echo $form->textField($user,'email',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($user,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($company,'title'); ?>
<?php echo $form->textField($company,'title',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($company,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'first_name'); ?>
<?php echo $form->textField($user,'first_name',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($user,'first_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'last_name'); ?>
<?php echo $form->textField($user,'last_name',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($user,'last_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'password'); ?>
<?php echo $form->passwordField($user,'password',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($user,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'password_repeat'); ?>
<?php echo $form->passwordField($user,'password_repeat',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($user,'password_repeat'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Signup!'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
- User Model
<?php
/**
* This is the model class for table "users".
*
* The followings are the available columns in table 'users':
* @property integer $id
* @property string $title
* @property string $email
* @property string $first_name
* @property string $last_name
* @property string $password
* @property string $salt
* @property string $activtion_code
* @property integer $status
* @property integer $company_id
* @property integer $role_id
*
* The followings are the available model relations:
* @property Company $company
* @property Roles $role
*/
class User extends CActiveRecord
{
public $password_repeat;
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'users';
}
/**
* @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('title, email, first_name, last_name, password, password_repeat', 'required'),
array('status, company_id, role_id', 'numerical', 'integerOnly'=>true),
array('title, email, first_name, last_name, password', 'length', 'max'=>45),
array('title, email', 'unique'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, title, email, first_name, last_name, status, company, activation_code', 'safe', 'on'=>'search'),
array('password', 'compare'),
array('password_repeat', 'safe'),
);
}
/**
* @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(
'company' => array(self::BELONGS_TO, 'Company', 'company_id'),
'role' => array(self::BELONGS_TO, 'Roles', 'role_id'),
);
}
}
- Company Model
<?php
/**
* This is the model class for table "Company".
*
* The followings are the available columns in table 'Company':
* @property integer $id
* @property string $title
* @property integer $owner_id
*
* The followings are the available model relations:
* @property Users[] $users
*/
class Company extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'Company';
}
/**
* @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('title', 'required'),
array('title', 'length', 'max'=>45),
array('title', 'unique'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, title', '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::HAS_MANY, 'Users', 'company_id'),
);
}
}