Can't even imagine how to realize the next feature. WHAT WE HAVE:
Table 1: application
id - name - //other columns
Table 2: app_company
id - company_name - region_id - applicant_id - //other columns
Table 3: app_region
id - region_name
I've established next relations: Application model (one-to-one relation):
return array(
'company_info' => array(self::HAS_ONE, 'AppCompany', 'applicant_id'),
);
AppCompany model:
return array(
'application' => array(self::BELONGS_TO, 'Application', 'applicant_id'),
'region' => array(self::BELONGS_TO, 'AppRegion', 'region_id',)
);
AppRegion model (by the way, do I really need this model?):
return array(
'company' => array(self::HAS_MANY, 'AppCompany', 'region_id',)
);
Here is my actionCreate of the ApplicationContoller:
public function actionCreate()
{
$application = new Application;
$company = new AppCompany;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Application'], $_POST['AppCompany']))
{
$application->attributes=$_POST['Application'];
$company->attributes=$_POST['AppCompany'];
$valid=$application->validate();
$valid=$company->validate() && $valid;
if($valid)
{
if($application->save(false))
{
$company->applicant_id = $application->id;
$company->save(false);
$this->redirect(array('view','id'=>$application->id));
}
}
}
$this->render('create',array(
'application'=>$application,
'company'=>$company,
));
}
My _form;
<?php echo $form->errorSummary(array($application, $company)); ?>
<!-- *** -->
<div class="row">
<?php echo $form->labelEx($company,'company_name'); ?>
<?php echo $form->textField($company,'company_name',array('size'=>60,'maxlength'=>75)); ?>
<?php echo $form->error($company,'company_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($application,'last_name'); ?>
<?php echo $form->textField($application,'last_name',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($application,'last_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($company,'company_region_id'); ?>
<?php echo $form->dropDownList($company,'company_region_id', CHtml::listData(AppRegion::model()->findAll(), 'id', 'region_title_ru'), array('empty'=>'Выберите область')); ?> // PAY ATTENTION HERE
<?php echo $form->error($company,'company_region_id'); ?>
</div>
So, while creating an application (AND WHILE APPLICATION is a parent-model), I can choose a region title by dropdown for AppCompany model. So, while i choose "Tashkent region", which is in app_region table, I insert this region's ID (for example, 7) into region_id column of app_company.
After that I have a view file:
<?php $this->widget('bootstrap.widgets.TbDetailView', array(
'data'=>$model,
'type'=>'striped bordered condensed',
'attributes'=>array(
'id',
//
'company_info.company_name',
'company_info.date_of_foundation',
'company_info.company_region_id' // HOW TO SHOW NO ID, BUT THE TITLE?
'last_name',
'first_name'
So, the question is: Is it possible to show the name of the region, which is located in the THIRD table, where the direct relations between them cannot be established?
Thank you all, I hope i could explaind clear what do I need.