1
votes

Wondering if anybody else has run into this issue. In summary, we upgraded from Symfony 2.0 to Symfony 2.1. With Symfony 2.1 if a create a brand new bundle and then an entity to use with Doctrine I get a "not a valid entity or mapped super class". If I place the same exact entity class (with proper path changes) into an existing bundle (created before 2.1) it can be accessed with no problems, even from the new bundle.

Here are the steps.

$ php app/console generate:bundle --namespace="Core/TestBundle"--format=yml --no-interaction --structure=no --dir=src 
$ php app/console doctrine:generate:entity --entity="CoreTestBundle:Product" --fields="name:string(255) price:float description:text" 

here is the Product.php code produced

<?php

namespace Core\TestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Core\TestBundle\Entity\Product
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Product
{
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string $name
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @var float $price
 *
 * @ORM\Column(name="price", type="float")
 */
private $price;

/**
 * @var text $description
 *
 * @ORM\Column(name="description", type="text")
 */
private $description;


/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 * @return Product
 */
public function setName($name)
{
    $this->name = $name;
    return $this;
}

/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}

/**
 * Set price
 *
 * @param float $price
 * @return Product
 */
public function setPrice($price)
{
    $this->price = $price;
    return $this;
}

/**
 * Get price
 *
 * @return float
 */
public function getPrice()
{
    return $this->price;
}

/**
 * Set description
 *
 * @param text $description
 * @return Product
 */
public function setDescription($description)
{
    $this->description = $description;
    return $this;
}

/**
 * Get description
 *
 * @return text
 */
public function getDescription()
{
    return $this->description;
}
}

from here running

$ php app/console doctrine:generate:entities "Core/TestBundle/Entity/Product" 

or accessing Product from code runs into
[Doctrine\ORM\Mapping\MappingException] Class Core\TestBundle\Entity\Product is not a valid entity or mapped super class. running
$ php app/console doctrine:schema:update --force produces "Nothing to update"

On the other hand, if I use a pre-existing bundle such as ArticleBundle and run the same commands ie

$ php app/console doctrine:generate:entity --entity="CoreArticleBundle:Product" --fields="name:string(255) price:float description:text"
$ php app/console doctrine:generate:entities "Core/ArticleBundle/Entity/Product"
$ php app/console doctrine:schema:update --force 

creates the table fine Also, accessing the Product from the TestBundle as CoreArticleBundle:Product works fine. I tried to compare the bundle configurations etc but couldn't come up with anything.

I also run the db creation scripts as follows.

$ php app/console doctrine:database:drop --force 

Dropped database for connection named dev

$ php app/console doctrine:database:create 

Created database for connection named dev All the tables got created except for the Product one.

In addition I created a fixture with the Product object. Same thing. Works when in an existing bundle. Same error as above in the newly created bundle.

2
It is normal that you get the error if you don´t run schema:update. What is strange is that schema:update produces "nothing to update". Have you checked that the Entity is really created and that it looks OK? Have you tried running doctrine:schema:create? What is that "from here running"?Carlos Granados
Can you paste the code of the generated Product entity just after php app/console doctrine:generate:entity --entity="CoreTestBundle:Product" --fields="name:string(255) price:float description:text" ?Sybio
"from here running" is supposed to mean "the next step is ..."Victor
Have you tried running doctrine:schema:create?Carlos Granados
php app/console doctrine:database:create Could not create database for connection named <comment>dev</comment> SQLSTATE[HY000]: General error: 1007 Can't create database 'dev'; database exists. I can try dropping and creating the db?Victor

2 Answers

2
votes

Talked to a local expert. Turns out our application is using two ORM managers. In such a case each bundle using the ORM has to be registered in app/config/config.yml. ie

entity_managers:
        sql_write:
            connection: sql_write
            mappings:
                CoreUserBundle : ~
                CoreImageBundle : ~
                CoreTestBundle : ~
0
votes

A doctrine:schema:validate should help you finding errors... It will check your mapping, and check if the mapping is sync with the database.