这可能很简单,但我很难过。这是我正在尝试将 csv 文件加载到实体中的操作。我找到了一个 php 插件,但不知道如何安装,所以我下载了插件并使用脚本将 csv 文件读入我的包 Acme\StoreBundle 并实际将脚本 (sfCsvReader.php) 放在实体文件夹中当我尝试在网络上运行它时捆绑我收到以下错误
致命错误:在第 26 行的 /home/jan/symfonysandbox/src/Acme/StoreBundle/Controller/LoadCSVController.php 中找不到类 'Acme\StoreBundle\Controller\sfCsvReader'
我的控制器文件如下
<?php
namespace Acme\StoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Acme\StoreBundle\Entity\Congress;
use Acme\StoreBundle\Entity\sfCsvReader;
use Symfony\Component\HttpFoundation\Response;
class LoadCSVController extends Controller
{
/**
* @Route("/load/{name}")
* @Template()
*/
public function indexAction()
{
$congress = new Congress();
$path = '~/Documents/congress.csv';
$reader = new sfCsvReader($path);
$reader->setSelectColumns('column_A, column_B');
$reader->open();
while ($data = $reader->read())
{
$congress->setTitle($data['column_A']);
$congress->setName($data['column_B']);
$em = $this->getDoctrine()->getManager();
$em->persist($congress);
$em->flush();
return new Response('Created congress id '.$congress->getId() .'for'.$congress->getTitle().'for'.$congress->getName());
}
$reader->close();
}
}
它的调用脚本是
sfCsvReader
<?php
namespace Acme\StoreBundle\Controller;
/**
* sfCsvReader
* by Carlos Escribano <carlos@markhaus.com>
*
* $csv = new sfCsvReader("/path/to/a/csv/file.csv");
* $csv->open();
* $csv->setSelectColumns('field_A, field_B'); // or array('field_A', 'field_B')
* while ($data = $csv->read())
* {
* // do something with $data['field_A'] and $data['field_B']
* }
* $csv->close();
*
* --- Alternative ---
* $csv = new sfCsvReader("/path/to/a/csv/file.csv");
* $csv->setSelectColumns('field_A, field_B'); // or array('field_A', 'field_B')
* $csv->open();
* ...
* --- Alternative: NUMERICAL INDEXES ---
* $csv = new sfCsvReader("/path/to/a/csv/file.csv");
* $csv->open();
* while ($data = $csv->read())
* {
* // do something with $data[0] and $data[1]
* }
* $csv->close();
*
*
* --- CHARSET ---
* $to = 'ISO-8859-1';
* $from = 'UTF-8';
* $csv->setCharset($to);
* $csv->setCharset($to, $from);
*/
class sfCsvReader
{
private
$header;
private
$file,
$path,
$initialized;
private
$length,
$delimiter,
$enclosure,
$to,
$from;
/**
* @param string $path Path to the file to read
* @param char $delimiter Character delimiting fields (Optional)
* @param char $enclosure Character enclosing fields (Optional)
* @param integer $length PHP's fgetcsv length parameter (Optional)
*/
function __construct($path, $delimiter = ',', $enclosure = '"', $length = null)
{
$this->path = $path;
$this->length = $length;
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
$this->header = array(
'map' => null,
'selected' => array()
);
$this->initialized = false;
}
public function close()
{
if ($this->file !== null)
{
fclose($this->file);
$this->file = null;
$this->initialized = false;
}
}
public function open($path = null)
{
$this->close();
if ($path !== null)
{
$this->path = $path;
}
if (!($this->file = fopen($this->path, "r")))
{
throw new Exception("File can not be opened (".$this->path.").");
}
$this->initialized = true;
$this->map();
}
public function setSelectColumns($selected = array())
{
$this->test = true;
if (!is_array($selected))
{
$selected = explode(',', preg_replace('/\s+/', '', $selected));
}
$this->header['selected'] = $selected;
$this->map();
}
public function clearSelectColumns()
{
$this->header['selected'] = array();
$this->map();
}
private function map()
{
if ($this->initialized)
{
$this->all = false;
$x = count($this->header['selected']); // N. of selected columns
$y = 0; // N. of real columns
$z = 0; // N. of matching columns
if ($x == 0)
{ // No selection. All fields.
$this->header['map'] = null;
}
else
{
$this->header['map'] = array();
fseek($this->file, 0, SEEK_SET);
if ($line = fgetcsv($this->file, $this->length, $this->delimiter, $this->enclosure))
{
$y = count($line);
if ($y > 0)
{
$common = array_intersect($line, $this->header['selected']);
$z = count($common);
if (($y < $x) || (($x > $z) && ($z > 0)))
{ // More columns in selection than in file or less common columns than selection
throw new Exception("Too much columns or non existing columns in selection (LINE: $y, SEL: $x, COMMON: $z).");
}
if ($z == 0)
{ // Relaxed Mapping: 0 matches found, numerical.
foreach ($this->header['selected'] as $i => $name)
{
$this->header['map'][$name] = $i;
}
fseek($this->file, 0, SEEK_SET);
}
else if ($z == $x)
{ // Absolute Mapping: First line is header.
foreach ($line as $i => $name)
{
$this->header['map'][$name] = $i;
}
}
} // Has columns
} // Read line
} // N columns selected
} // Initialized
}
public function read()
{
if (!$this->initialized)
{
throw new Exception('sfCsvReader is not ready.');
}
if ($line = fgetcsv($this->file, $this->length, $this->delimiter, $this->enclosure))
{
if (is_array($this->header['map']))
{
$res = array();
foreach ($this->header['selected'] as $name)
{
if ($this->to !== null)
{
$res[$name] = $this->encode($line[$this->header['map'][$name]], $this->to, $this->from);
}
else
{
$res[$name] = $line[$this->header['map'][$name]];
}
}
return $res;
}
else
{
return $line;
}
}
else
{
return null;
}
}
private function encode($str, $to, $from = null)
{
if ($from === null)
{
$from = mb_detect_encoding($str);
}
if (function_exists('iconv'))
{
return iconv($from, $to, $str);
}
else
{
return mb_convert_encoding($str, $to, $from);
}
}
public function setCharset($to, $from = null)
{
$this->to = $to;
$this->from = $from;
}
function __destruct()
{
$this->close();
}
}