Symfony2.1:获取以下错误致命错误:类'Acme \ ..........'未找到

时间:2013-01-16 22:54:38

标签: php symfony

这可能很简单,但我很难过。这是我正在尝试将csv文件加载到实体中。我找到了一个php插件,但无法弄清楚如何安装,所以我下载了插件,并将脚本读取到我的捆绑Acme \ StoreBundle中的csv文件,并实际将脚本(sfCsvReader.php)放在实体文件夹中捆绑当我尝试在网络上运行时,我收到以下错误

致命错误:第26行的/home/jan/symfonysandbox/src/Acme/StoreBundle/Controller/LoadCSVController.php中找不到类'Acme \ StoreBundle \ Controller \ sfCsvReader'

我的Controller文件如下

<?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();
  }
}

1 个答案:

答案 0 :(得分:0)

在调用脚本之前是否正在加载脚本?

尝试将以下代码添加到$loader->registerNamespaces(array(...数组声明中的 app / autoload.php

 'SfCsvReader' => __DIR__.'/../src/Acme/StoreBundle/Entity/sfCsvReader.php',

之后,将 sfCsvReader.php 文件中的命名空间重命名为namespace SfCsvReader\sfCsvReader;,并将控制器中请求的命名空间替换为use SfCsvReader\sfCsvReader;

这应该可以正常工作,但也许您应该将文件移动到更合适的目录,例如在项目的 vendor / sfCsvReader 目录中。

您可以按照Symfony 2.0文档中的建议操作。

自动加载信息位于以下链接The ClassLoader Component

有关命名空间的更多信息,请参阅php.net