0

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Decd
  • 65
  • 8
  • I think that the most evident way is to move the region_id into application table. So, there would be a direct relation between them and it would be much easier to perform what I need, especially considering that this hole application would be used as one - there is no need to use company and applicant's info separately. I'm doing that because I will have a huge amount of information with more related tables with 200+ fields. But I think it would be better to keep all company info in its own table. – Decd Apr 17 '14 at 13:49

1 Answers1

0

Ok, that's what I found Add this to Application model:

return array(
    'company_info' => array(self::HAS_ONE, 'AppCompany', 'applicant_id'),
    'region'=>array(
            self::HAS_ONE,'AppRegion',array('company_region_id'=>'id'),
                'through'=>'company_info'
        ),

And after that we can:

<?php $this->widget('bootstrap.widgets.TbDetailView', array(
'data'=>$application,
'type'=>'striped bordered condensed',
'attributes'=>array(
    'id',
    //
    'company_info.company_name',
    'company_info.date_of_foundation',
    'region.region_title_ru', // REGION NAME WOULD BE SHOWN HERE!

I also found out that it could be possible acting like:

$a->with('c.b')->find

But couldn't realize it yet in my project

Decd
  • 65
  • 8