Activerecord 保存相关模型的多个实例(有多个)

Activerecord 保存相关模型的多个实例(有多个),activerecord,yii,Activerecord,Yii,我有一个产品表,每种类型的员工(另一个表)都有一个价格 这就把我带到桌子上: # product # associate_types ## description # associate_prices ## product_id ## associate_type_id ## price 产品的模型关系: 'prices'=>数组(self::有许多'AssociatePrices','id\u product') 协会价格: 'associateType' => array(sel

我有一个产品表,每种类型的员工(另一个表)都有一个价格

这就把我带到桌子上:

# product

# associate_types
## description

# associate_prices
## product_id
## associate_type_id
## price
产品的模型关系:

'prices'=>数组(self::有许多'AssociatePrices','id\u product')

协会价格:

'associateType' => array(self::BELONGS_TO, 'AssociateTypes', 'id_associate_type'),
现在,在产品表单上,我显示了各种关联类型,以便用户填写所有关联类型的价格,我处理得很好:我获取关联类型,这个给定产品的价格已经插入,然后交叉

为了更新/创建这些条目,我在产品的afterSave()中有以下代码:

$this->pricesDup
是我刚做的一个属性,因为我不能玩
$this->prices

结果是,即使save()方法返回true,数据库也保持不变

我已经从yii框架论坛上读到了很多东西,但这些东西都不适用于我的案例。我做错了什么

非常感谢

编辑:产品模型还有一个beforeSave()函数,但它与此问题无关。即使我在这里张贴(一部分)的要求

/**
 * This function's mission is to handle the file upload.
 */ 
public function beforeSave() 
{
    parent::beforeSave();
    // File uploading
    if (is_object($this->image)) {
        $dir = PRODUCTS_FILES_PATH;

        $current = Yii::app()->getRequest()->getPost('currentImage');
        if (!empty($current))
            @unlink($dir.$current);

        $name = $this->image->name;
        $j = 1;
        while (file_exists($dir . $name)) {
            //....
        }
        $this->image->saveAs($dir.$name, true);
        $this->image = $name;
    }
    return true;
}

更新v2.0 您的新模型实例不正确。您没有编写new关键字来初始化模型

$this->pricesDup[$i] = AssociatePrices::model();
它必须是:

  $this->pricesDup[$i] = new AssociatePrices;
更新 创建新的模型实例,以便将其保存在数据库中:

    $pricesDup = new PriceDup;</strike>
$pricesDup=新的PriceDup;
你忘记分配属性了吗?这样地:
$model->attributes=$\u POST['attributes']

没有$此->价格上升[$i]->attributes=$price$价格来自$\u POST vars:)那么,新的模型实例在哪里呢?比如:检查更新的答案您在该控制器中有
beforeSave()函数吗?如果是,则也向我们显示。在保存之前有一个
,但它与此问题(价格)无关。即使我会把它贴出来。“创建一个新的模型实例,这样它就可以保存在数据库中”我在foreach中这样做。当你说“该控制器中的函数”时,你指的是模型,对吗?:)Thanks@Lu函数可以在控制器或模型中。无论如何,它不会妨碍保存。顺便说一句,您的新模型实例未被更正。检查更新的答案。
    $pricesDup = new PriceDup;</strike>