0

我在 symfony 1.4 和 Doctrine 中使用迁移将数据上传到数据库中。

我有一个 CSV 文件,我正在尝试将其批量加载到 sfGuardUser 表中,并将用户 ID 与 sfGuardUserGroup 组 ID 匹配,然后将其加载到 sf_guard_user 表中。

当我运行迁移时,它只从 CSV 加载第一条记录。加载的记录看起来格式正确(构建用户名和日期)。

我在将代码放入迁移之前对其进行了测试,并且它工作正常。

我这里有语法错误,还是没有设置为正确循环?

也许我只是把它完全搞砸了。

PHP

    public function up()
      {

          $fieldseparator = ",";
          $lineseparator = "\n";
          $csvfile = "web/uploads/batchUpload.csv";

          $file = fopen($csvfile,"r");
          $size = filesize($csvfile);

          $csvcontent = fread($file,$size);

          fclose($file);

          $lines = 0;
          $queries = "";
          $linearray = array();

          foreach(explode($lineseparator,$csvcontent) as $line) {

              //Clean it up
              $lines++;   
              $line = trim($line," \t");      
              $line = str_replace("\r","",$line);      
              $line = str_replace("'","\'",$line);  
              $linearray = explode($fieldseparator,$line);

              //build username
              if (!$linearray[3]) {
                  $username = $linearray[0];
                  $username = substr($linearray[0], 0, 1);
                  $username .= $linearray[1];
                  $username = strtolower($username);
                  $linearray[3] = $username;
              }

              //add sha1 algorithm
              if (!$linearray[4]) {
                  $linearray[4] = "sha1";
              }

              //add is_active
              if(!$linearray[7]) {
                  $linearray[7] = "1";
              }

              //add current date to login, created and updated
                  $now = getdate();           
                  if ($now['mday'] < 10) {
                      $now['mday'] = str_pad($now['mday'], 2, "0", STR_PAD_LEFT);
                  }           
                  if ($now['hours'] < 10) {
                  $now['hours'] = str_pad($now['hours'], 2, "0", STR_PAD_LEFT);
                  }       
                  if ($now['minutes'] < 10) {
                  $now['minutes'] = str_pad($now['minutes'], 2, "0", STR_PAD_LEFT);
                  }       
                  if ($now['seconds'] < 10) {
                  $now['seconds'] = str_pad($now['seconds'], 2, "0", STR_PAD_LEFT);
                  }
                  $linearray[10] = $now['year']. "-" .$now['mon']. 
                          "-".$now['mday']." ".$now['hours'].":".$now['minutes'].":"
                          .$now['seconds'];
                  $linearray[11] = $now['year']. "-" .$now['mon']. 
                          "-".$now['mday']. " ".$now['hours'].":".$now['minutes'].
                          ":".$now['seconds'];


             //load up sql  
             $linemysql = implode("','",$linearray);    
             $conn = Doctrine_Manager::getInstance()->connection();  
             $query = "INSERT INTO sf_guard_user VALUES ('', '$linemysql');";

             //$queries .= $query . "\n";   
             $conn->execute($query);

             //Add userID to group table
             $conn = Doctrine_Manager::getInstance()->connection();  
             $getFirstID = Doctrine::getTable('sfGuardUser')->orderBy('id DESC')
                  ->limit(1)->execute();
             $id = $getFirstID['id'];
             $query = "INSERT INTO sf_guard_user_group ('$id', 5, '$linearray[10]',          '$linearray[11]');";
             $conn->execute($query);
             $query = "INSERT INTO sf_guard_user_group ('$id', 14, '$linearray[10]', '$linearray[11]');";
             $conn->execute($query);
        }// close foreach split
} 

更新

如果我删除了我试图加载最后一个 id 并将其与 GuardUserGroup group_id 匹配的第二个查询,那么它就可以工作。所以我想我们可以缩小范围并说我的错误在该//Add userID to group table部分

4

1 回答 1

0

更新了我的查询以获取最新的 id 记录。我认为主要问题是 $id 在查询中仍然是一个数组。我是这样破解的:

//Add userID to group table
$getFirstID = Doctrine::getTable('sfGuardUser')->createQuery()->select('MAX(id) as id')->fetchArray();
**$id = $getFirstID[0];
$id = $id['id'];**
$q = "INSERT INTO sf_guard_user_group VALUES ('$id', 5, '$linearray[10]', '$linearray[11]');";
$conn->execute($q);
$query = "INSERT INTO sf_guard_user_group VALUES ('$id', 14, '$linearray[10]', '$linearray[11]');";
$conn->execute($query);
于 2012-07-18T18:07:42.877 回答