0

我有一个模型website和一个模型url

每个url data都附加到一个website data. A通过 aurl与 a 相关。websitewebsite_id

在我的网络应用程序上,我需要在接受数据之前检查数据。

我想在屏幕上看到关于 a 的全部数据url data,我设法做到了。

现在,我还想更新屏幕上列出的全部数据。

在屏幕上我有整个url dataand website data,但是当我尝试保存数据时,website data是空的。

我的逻辑是向url模型添加一个属性:public $website;

url模型中,一个方法aftersave

protected function afterSave() {
    $w = null;
    $w = Website::model()->findByAttributes(array('id' => $this->website_id));
    //echo '<pre>';
    //print_r($w);
    print_r($this->website);
    $w->link = $this->website['link'];
    $w->domain = $this->website['domain'];
    $w->description = $this->website['description'];
    $w->save(false);
    die;
    return parent::afterSave();
}

这是_form文件中的重要代码:

<div class="row">
    <?php echo $form->labelEx($model,'will_expire'); ?>
    <?php echo $form->dropDownList($model,'will_expire',array(0=>'No',1=>'Yes')); ?>
    <?php echo $form->error($model,'will_expire'); ?>
</div>

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

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

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

2 回答 2

1

有几个问题:

  1. 模型中定义的关系url和现有 url 是否为您提供了有效的网站记录?website$url->website
  2. 你的表格显示,你没有使用任何 website_id,意味着你正在为每个 URL 创建一个新的网站记录,意味着$w = Website::model()->findByAttributes(array('id' => $this->website_id));总是返回 NULL

我假设你已经website在你的URL模型中定义了一个关系。根据我的理解建议的功能:

protected function afterSave() {
    //data is already assigned by the caller
    $w = null;
    $w = Website::model()->findByAttributes(array('id' => $this->website_id));
    if($w)
    {
        Yii::log("updating existing website data","info");
        $this->website->save();
    }
    else
    {
        Yii::log("creating new website data","info");
        $this->website->save();
        $this->website_id = $this->website->website_id;
        $this->update(array('website_id')); //save the relation
        Yii::log("created new website {$this->website_id}","info");
    }
    return parent::afterSave();
}
于 2013-03-26T10:03:05.840 回答
0

原来是小错误,比如wW对象属性代替数组;

好的,让我教你如何做到这一点;

我有 2 个模型:UrlWebsite.

Urlmodel 有一个外键website_id和一个与 model 的关系Website

Url模型中,我有一个名为的属性website,我将其声明为public $website = array();

bookmarklet数据是通过使用 a添加的,API因此当管理员来更新/检查数据时,数据就会在那里;

Url模型中我有一个方法afterSave

protected function afterSave() {
    $w = null;
    $w = Website::model()->findByAttributes(array('id' => $this->website_id));
    if($w)
    {
    $w->link = $this->website['link'];
    $w->domain = $this->website['domain'];
    $w->description = $this->website['description'];
    $w->save();
    }
    return parent::afterSave();
}

$this->website像这样填充UrlController的地方actionUpdate

public function actionUpdate($id, $type = 'update') {
    $model = $this->loadModel($id);

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if (isset($_POST['Url'])) {
        $model->attributes = $_POST['Url'];
        $model->website = $_POST['Website'];
        if ($model->save())
            if ($type == 'update')
                $this->redirect(array('view', 'id' => $model->id));
            else
                $this->redirect(array('/admin/url/approvePublicLink'));
    }

    $model_website = Website::model()->findByAttributes(array('id'=>$model->website_id));

    $this->render('update', array(
        'model' => $model,
        'model_website' => $model_website,
    ));
}

afterSave并稍后传递给该方法;

这是_update视图:

<div style="padding:20px 20px;">
<h1>Update Url, Website, Keywords ...</h1>
<?php echo $this->renderPartial('_form', array(
'model'=>$model,
'model_website' => $model_website,
)); ?>
</div>

这是_form部分呈现的视图:

<div class="form">

<?php

$form=$this->beginWidget('CActiveForm', array(
    'id'=>'url-form',
    '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,'website_id'); ?>
        <?php echo CHtml::link($model->relation_website->domain,$model->relation_website->domain,array('class'=>'avia','target'=>'_blank')); ?>
        <?php echo $form->error($model,'website_id'); ?>
    </div>

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

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

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

    <div class="row">
        <?php echo $form->labelEx($model,'important'); ?>
        <?php echo $form->dropDownList($model,'important',array(0=>'Normal',1=>'Important')); ?>
        <?php echo $form->error($model,'important'); ?>
    </div>

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

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

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

    <div class="row">
        <?php echo $form->labelEx($model,'will_expire'); ?>
        <?php echo $form->dropDownList($model,'will_expire',array(0=>'No',1=>'Yes')); ?>
        <?php echo $form->error($model,'will_expire'); ?>
    </div>

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

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

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

    <div class="row">
        <?php echo $form->labelEx($model,'status'); ?>
        <?php echo $form->dropDownList($model,'status',array(-1=>'Banned',0=>'Normal',1=>'Active')); ?>
        <?php echo $form->error($model,'status'); ?>
    </div>

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

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

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

我希望它有所帮助。

于 2013-03-26T19:16:44.930 回答