Doctrine只向DB保存一个结果

时间:2015-11-29 07:48:54

标签: php symfony zend-framework doctrine-orm doctrine

我有这段代码,

foreach ($res_arr as $key => $value) {
    $post = array(
        'details_id'=>$key,
        'classsubject_id'=>$cs_id,
        'term'=>$term,
        'marks_obtained'=>$value,
        'remarks'=>''
    );
    $marks->fromArray($post);
    $marks->save();
     print_r($post);
}   

但它只向数据库添加一个结果,当我在循环中打印结果时,它正在打印此数据

 Array
(
    [details_id] => 1
    [classsubject_id] => 10
    [term] => first
    [marks_obtained] => 23
    [remarks] => 
)
Array
(
    [details_id] => 6
    [classsubject_id] => 10
    [term] => first
    [marks_obtained] => 23
    [remarks] => 
)
Array
(
    [details_id] => 7
    [classsubject_id] => 10
    [term] => first
    [marks_obtained] => 23
    [remarks] =>


)

它只是将最后一个数组结果添加到数据库中 帮帮我一个人

1 个答案:

答案 0 :(得分:0)

所以我发现每次使用doctrine将数据添加到数据库时都必须创建新实例。因此,每次循环时,您都必须创建模型的新实例,而不是保存数据......