0
votes

I want to visualize data from a database with Doctrine, but it doesn't seems to work...

Here's my class Organization:

<?php
 namespace AppBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;

/**Class Organization
 * @package AppBundle\Entity
 *
 * @ORM\Table(name="organization")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\
 * OrganizationRepository")
 *
 */
 class Organization
 {

/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;


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




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

/**
 * @param int $id
 */
public function setId($id)
{
    $this->id = $id;
}

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

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

}

Here's the error:

Doctrine\ORM\Mapping\MappingException: Class "AppBundle\Entity\Organization" is not a valid entity or mapped super class.

at vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php:346 at Doctrine\ORM\Mapping\MappingException::classIsNotAValidEntityOrMappedSuperClass('AppBundle\Entity\Organization') (vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php:91) at Doctrine\ORM\Mapping\Driver\AnnotationDriver->loadMetadataForClass('AppBundle\Entity\Organization', object(ClassMetadata)) (vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php:102) at Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass('AppBundle\Entity\Organization', object(ClassMetadata)) (vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:151) at Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(object(ClassMetadata), null, false, array()) (vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:332) at Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata('AppBundle\Entity\Organization') (vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:78) at Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata('AppBundle\Entity\Organization') (vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:216) at Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('AppBundle:Organization') (vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:281) at Doctrine\ORM\EntityManager->getClassMetadata('AppBundle:Organization') (vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php:44) at Doctrine\ORM\Repository\DefaultRepositoryFactory->getRepository(object(EntityManager), 'AppBundle:Organization') (vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:698) at Doctrine\ORM\EntityManager->getRepository('AppBundle:Organization') (src/AppBundle/Controller/OrganizationController.php:27) at AppBundle\Controller\OrganizationController->showInfoAction() at call_user_func_array(array(object(OrganizationController), 'showInfoAction'), array()) (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:153) at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1) (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:68) at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true) (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:171) at Symfony\Component\HttpKernel\Kernel->handle(object(Request)) (web/app_dev.php:29)

I have the OrganizationController and the OrganizationRepository.

2
In entity annotations after @ORM\Entity(repositoryClass you have line break and that might be the reason.Tomasz
@TomaszTurkowski That is because StackOverflow had some problems to put the complete line without breaking it, but in my code it's okey.HessianMad
@Vamsi I saw this link but didn't work... :(HessianMad
1-. Which version of php and Symfony are you using? 2.- Try a php app/console doctrine:mapping:info and see if it clears something. 3.- Try a php app/console doctrine:cache:clear-metadata 4.- Try a php app/console cache:clearsh4

2 Answers

1
votes

Move class name annotation to next line or give space before it as below

/**
 * Class Organization
 * @package AppBundle\Entity
 *
 * @ORM\Table(name="organization")
 * 
 * @ORM\Entity(repositoryClass="AppBundle\Repository\OrganizationRepository")
 */
0
votes
  1. You should check the php file name. Does you entity filename is Organization.php
  2. You should remove setId() method in your code (It won't resolve your problem, but you mustn't put this method). Because $id is auto generated.

Remove this:

 /**
  * @param int $id
  */
  public function setId($id)
  {
       $this->id = $id;
  }
  1. Also - try not to use the private visibility for the $id property. Right way is - set it as protected.