Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Database Tutorial code error

Tags:

symfony

I've got Symfony 2 successfully installed and set up and have been following the documentation through.

I'm currently up to http://symfony.com/doc/2.0/book/doctrine.html

Everything is fine until I get to this line:

php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Product

at which point I get the following error:

[RuntimeException]

The autoloader expected class "Acme\StoreBundle\Entity\Product" to be defined
in file "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\Symf
ony\app/../src\Acme\StoreBundle\Entity\Product.php". The file was found but the
class was not in it, the class name or namespace probably has a typo.

This has happened to me on both Linux and Windows machines.

The contents of Product.php is as per the tutorial:

// src/Acme/StoreBundle/Entity/Product.php
namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product
{
     /**
      * @ORM\Id
      * @ORM\Column(type="integer")
      * @ORM\GeneratedValue(strategy="AUTO")
      */
     protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;

    /**
     * @ORM\Column(type="decimal", scale=2)
     */
    protected $price;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;
}
like image 681
Thom Shutt Avatar asked Sep 20 '11 08:09

Thom Shutt


2 Answers

That message comes from DebugUniversalClassLoader.php:

public function loadClass($class)
{
    if ($file = $this->findFile($class)) {
        require $file;

        if (!class_exists($class, false) && !interface_exists($class, false)) {
            throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
        }
    }
}

So, the file must be there, and readable, otherwise the findFile and the require wouldn't have worked. All this check is doing is require()ing the file and then using the standard PHP class_exists() to see if the class is now there.

The only thing I can think of that could cause this message given those file contents: you've missed off the <?php at the start of the file. Now, I know this is going out on a bit of a limb, but I honestly can't think of anything else that wouldn't cause some other kind of error.

like image 108
Matt Gibson Avatar answered Nov 15 '22 10:11

Matt Gibson


I had the same error "The file was found but the class was not in it, the class name or namespace probably has a typo.". It's only because

<?php

is missing at the beginning of the file !! Argh, copy-paste from a tutorial... Bye !

like image 37
vinsse2001 Avatar answered Nov 15 '22 09:11

vinsse2001