doupeizheng3918 2016-12-30 11:31
浏览 52
已采纳

Zend Framework 2 - 警告:SplFileObject :: setCsvControl():escape必须是一个字符

Hi I am trying to get the csv file data and display that data in my view .

Below is my controller function :

 public function indexAction()
    {  
      $delimiter = ',';
      $enclosure = '""';
      $escape = '//';
      $filename = 'data' . DIRECTORY_SEPARATOR . 'csv' . DIRECTORY_SEPARATOR . 'StoreDepartment.csv';;
      $this->file = new SplFileObject($filename);
      $this->file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);

      $this->file->setCsvControl($delimiter, $enclosure, $escape);
      $this->useFirstRecordAsHeader = true;

      $response = $this->getResponse();
      $headers  = $response->getHeaders();
      $headers->addHeaderLine('Content-Type', 'text/csv');
      $contents = $this->file->fread($this->file->getSize());
      $response->setContent($contents);
      $views = new ViewModel(array('text'=>$response));
      return $views;       
    }

I am getting all the data as csv. but i am getting below warning :

Warning: SplFileObject::setCsvControl(): escape must be a character

Also i want to convert this data into JSON or Array format is it possible ?

Main concept is i want to store this data into my database table. So what format is suitable to store data in database ? Please Help

How to resolve both the errors ?

  • 写回答

1条回答 默认 最新

  • drr25281 2016-12-30 13:55
    关注

    The warning is because your $escape variable is two characters not one.

    $escape = '/';
    

    or

    $escape = "//";
    

    Since you only need to escape backslashes when using double quotes.

    As for your question regarding how to store in database. It's very hard to say without knowing what you need to do and what data is in the CSV. However I would suggest having a one to one mapping between a CSV column and a database column. Though the number of tables you need would really depend on the data and use cases. There are also a few situations when you may want to just store the raw data but again more information would be required and it would still be an opinionated answer that would be given

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程