K
K
Ka3aHoBa2015-06-01 15:32:45
Yii
Ka3aHoBa, 2015-06-01 15:32:45

How to display a field in a model form to save data from it to a linked table?

Colleagues. hello.
Began to study Yii 1.1.16
Faced a problem:
There are two related tables (models) - owner and number.
The owner holds the record ID from the number table, i.e. one line owner can match several numbers.
How to display on the page for creating a new owner instead of numberid a field that will be saved to the associated table number?
You need to change the code in _form.php, but how?

<?php
/* @var $this OwnerController */
/* @var $model Owner */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
  'id'=>'owner-form',
  // Please note: When you enable ajax validation, make sure the corresponding
  // controller action is handling ajax validation correctly.
  // There is a call to performAjaxValidation() commented in generated controller code.
  // See class documentation of CActiveForm for details on this.
  'enableAjaxValidation'=>false,
)); ?>

  <p class="note">Fields with <span class="required">*</span> are required.</p>

  <?php echo $form->errorSummary($model); ?>

  <div class="row">
    <?php echo $form->labelEx($model,'owner'); ?>
    <?php echo $form->textField($model,'owner',array('size'=>40,'maxlength'=>40)); ?>
    <?php echo $form->error($model,'owner'); ?>
  </div>

  <div class="row">
    <?php echo $form->labelEx($model,'numberid'); ?>
    <?php echo $form->textField($model,'numberid'); ?>
        		<?php echo $form->error($model,'numberid'); ?>
  </div>

  <div class="row">
    <?php echo $form->dropDownList($model,'type', 
        Type::All(),
        array('empty' => '(Select a type)')
)       ;?>       
    <?php echo $form->error($model,'type'); ?>
  </div>

  <div class="row">
    <?php echo $form->labelEx($model,'InfoData'); ?>
    <?php echo $form->textField($model,'InfoData',array('size'=>60,'maxlength'=>60)); ?>
    <?php echo $form->error($model,'InfoData'); ?>
  </div>

  <div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
  </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

What and how to change in this paragraph?
<div class="row">
    <?php echo $form->labelEx($model,'numberid'); ?>
    <?php echo $form->textField($model,'numberid'); ?>
        		<?php echo $form->error($model,'numberid'); ?>
  </div>

I understand that it is necessary to add a function to the Number model and calculate it here, but how and what function?
It is necessary to replace after with id with a field with a value. So that the new value is stored in the Number model.
5f82c67c2403450f9c5a5332b3c04df9.PNG

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question