0
我正在开发这个网站,要求我在一个视图中合并两个模型,他们之间有一对多的关系。模型名称是家庭和图像意义家有很多图像,但图像只有一个家。在Yii更新中获取多个记录
我已经管理结合在一起的观点,但我遇到的问题是获得所有的图像。例如,我有6张图片想要展示,或者我有5张图片想展示。
家庭控制器UpdateMethod
public function actionUpdate($id)
{
$home=$this->loadModel($id);
$image=Image::model()->findByAttributes(array('homeId'=>$home->id));
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Home'],$_POST['Image'])){
$home->attributes=$_POST['Home'];
$image->attributes=$_POST['Image'];
$valid=$home->validate();
$valid=$image->validate() && $valid;
if($valid){
if($home->save()){
$image->save();
}
}
}
$this->render('update',array(
'home'=>$home,
'image'=>$image,
));
}
我_form.php这个加入他们一起
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'home-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($home); ?>
<div class="row">
<?php echo $form->labelEx($image,'imageUrl'); ?>
<?php echo $form->textField($image,'imageUrl',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($image,'imageUrl'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($home,'recentEvents'); ?>
<?php echo $form->textField($home,'recentEvents',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($home,'recentEvents'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($home,'introduction'); ?>
<?php echo $form->textArea($home,'introduction',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($home,'introduction'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($home->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
更新我所以现在它返回了FindByattribues,而不是在模型FindAllByAttribues数组。现在如何在视图中处理该数组?
如果你建立了模型关系,你应该也可以使用'$ home-> images'(或者你调用关系)。这个关系应该是像这样的''images'=>数组(self :: HAS_MANY,'Image','homeId')',它应该在home中定义。因此,在视图中,您还可以使用'$ home-> images作为$ image'或使用'$ images = $ home-> images'在控制器中定义图像。并不是说你知道的方式是错的,只是想把它扔到那里。 http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship – Jeroen 2014-09-08 14:32:41
好的,谢谢你的建议! – Steve 2014-09-08 14:34:00