dongtan8532 2013-08-29 17:58 采纳率: 0%
浏览 51

使用Propel ORM将多个条目插入Mysql数据库

I am writing a web application using PHP. For the first time I started using Propel ORM. I am reading values from array and sending them to a function which has Propel Insert Queries. The function is like this.

public function someFunction
{
    $nid=10;
    $sample_array = array("first","second");
        foreach($sample_array as $items){
            $this->saveTags($items,$nid);
        } 
}

public function saveTags($tags,$nid)
{
   error_log("SetTag: ".$this->tags->setTag($tags));
   error_log("SetNid: ".$this->tags->setNid($nid));
   error_log("Save: ".$this->tags->save());
}

When I execute the program, the first item "first" saves but immediately overwritten by second item i.e "second". Below are the apache error logs.

 SetTag: Tid: null
Nid: null
Tag: first

 SetNid: Tid: null
Nid: 30
Tag: first

 Save: 1
 SetTag: Tid: 11
Nid: 30
Tag: second

 SetNid: Tid: 11
Nid: 30
Tag: second

 Save: 1

As you can see the first item is replaced by second item! Any solution to prevent this?

Thanks

  • 写回答

1条回答 默认 最新

  • doutun1875 2013-08-30 19:07
    关注

    What is $this->tags ?

    I guess, since your are using $this->, that you use the object every time you are going inside the function saveTags().

    What you can do is to create a new tags every time, something like:

    public function saveTags($tags,$nid)
    {
       $tags = new Tags();
       $tags->setTag($tags);
       $tags->setNid($nid);
       $tags->save();
    
       return $tags;
    }
    

    This way, every time you will call saveTags it will create a new tag.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错