am working on symfony2 and am geting this error:
No mapping file found named 'Acme.BlogBundle.Entity.Posts.php' for
class 'Acme\BlogBundle\Entity\Posts'. 500 Internal Server Error -
MappingException
I generate Entity php app/console doctrine:generate:entity
Name of entity: AcmeBlogBundle:Post
Format: php
All that i put in Acme:BlogBundle:Entity directory.
This is my Entity Post class with getter and setter methds:
<?php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Posts
*/
class Posts
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $title;
/**
* #var string
*/
private $shortText;
/**
* #var string
*/
private $longText;
/**
* #var string
*/
private $author;
/**
* #var \DateTime
*/
private $dateCreated;
/**
* #var \DateTime
*/
private $dateModified;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Posts
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set shortText
*
* #param string $shortText
* #return Posts
*/
public function setShortText($shortText)
{
$this->shortText = $shortText;
return $this;
}
/**
* Get shortText
*
* #return string
*/
public function getShortText()
{
return $this->shortText;
}
/**
* Set longText
*
* #param string $longText
* #return Posts
*/
public function setLongText($longText)
{
$this->longText = $longText;
return $this;
}
/**
* Get longText
*
* #return string
*/
public function getLongText()
{
return $this->longText;
}
/**
* Set author
*
* #param string $author
* #return Posts
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set dateCreated
*
* #param \DateTime $dateCreated
* #return Posts
*/
public function setDateCreated($dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* #return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
/**
* Set dateModified
*
* #param \DateTime $dateModified
* #return Posts
*/
public function setDateModified($dateModified)
{
$this->dateModified = $dateModified;
return $this;
}
/**
* Get dateModified
*
* #return \DateTime
*/
public function getDateModified()
{
return $this->dateModified;
}
}
In my controller i first set Post Entity after definig namespace of controller.
use Acme\BlogBundle\Entity\Posts;
After that i create method
public function AddAction()
{
// $post = Acme\BlogBundle\Entity\Posts()
$posts = new Posts();
$posts->setTitle('Test Title');
$em = $this->getDoctrine()->getManager();
$em->persist($posts);
$em->flush();
}
Here is and Stack Trace output
[1] Doctrine\Common\Persistence\Mapping\MappingException: No mapping
file found named 'Acme.BlogBundle.Entity.Posts.php' for class
'Acme\BlogBundle\Entity\Posts'.
at n/a
in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php
line 74
at Doctrine\Common\Persistence\Mapping\MappingException::mappingFileNotFound('Acme\BlogBundle\Entity\Posts',
'Acme.BlogBundle.Entity.Posts.php')
in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/DefaultFileLocator.php
line 117
at Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator->findMappingFile('Acme\BlogBundle\Entity\Posts')
in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/PHPDriver.php
line 59
at Doctrine\Common\Persistence\Mapping\Driver\PHPDriver->loadMetadataForClass('Acme\BlogBundle\Entity\Posts',
object(ClassMetadata))
in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php
line 104
at Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass('Acme\BlogBundle\Entity\Posts',
object(ClassMetadata))
in /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
line 113
at Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(object(ClassMetadata),
null, false, array())
in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
line 302
at Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata('Acme\BlogBundle\Entity\Posts')
in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
line 205
at Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('Acme\BlogBundle\Entity\Posts')
in /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php
line 268
at Doctrine\ORM\EntityManager->getClassMetadata('Acme\BlogBundle\Entity\Posts')
in /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php
line 1580
at Doctrine\ORM\UnitOfWork->doPersist(object(Posts), array('000000000d824498000000009cdc8511' => object(Posts)))
in /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php
line 1555
at Doctrine\ORM\UnitOfWork->persist(object(Posts))
in /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php
line 565
at Doctrine\ORM\EntityManager->persist(object(Posts))
in /var/www/html/Symfony/src/Acme/BlogBundle/Controller/DefaultController.php
line 23
at Acme\BlogBundle\Controller\DefaultController->indexAction()
in line
at call_user_func_array(array(object(DefaultController), 'indexAction'), array())
in /var/www/html/Symfony/app/bootstrap.php.cache line 2815
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request),
'1')
in /var/www/html/Symfony/app/bootstrap.php.cache line 2789
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1',
true)
in /var/www/html/Symfony/app/bootstrap.php.cache line 2918
at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request),
'1', true)
in /var/www/html/Symfony/app/bootstrap.php.cache line 2220
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
in /var/www/html/Symfony/web/app_dev.php line 28
Update:
New Entity Test:
<?php
// src/Acme/BlogBundle/Entity/Test.php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table()
* #ORM\Entity
*/
class Test
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="short_text", type="string", length=255)
*/
private $shortText;
/**
* #var string
*
* #ORM\Column(name="long_text", type="text")
*/
private $longText;
/**
* #var \DateTime
*
* #ORM\Column(name="date_created", type="datetime")
*/
private $dateCreated;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Test
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set shortText
*
* #param string $shortText
* #return Test
*/
public function setShortText($shortText)
{
$this->shortText = $shortText;
return $this;
}
/**
* Get shortText
*
* #return string
*/
public function getShortText()
{
return $this->shortText;
}
/**
* Set longText
*
* #param string $longText
* #return Test
*/
public function setLongText($longText)
{
$this->longText = $longText;
return $this;
}
/**
* Get longText
*
* #return string
*/
public function getLongText()
{
return $this->longText;
}
/**
* Set dateCreated
*
* #param \DateTime $dateCreated
* #return Test
*/
public function setDateCreated($dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* #return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
}
Again the some error
$ php app/console doctrine:generate:entities AcmeBlogBundle
Generating entities for bundle "AcmeBlogBundle"
[RuntimeException]
Bundle "AcmeBlogBundle" does not contain any mapped entities.
doctrine:generate:entities [--path="..."] [--no-backup] name
You should add mapping information for fields. Read more
Like this:
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
I made the mistake of adding a new Entity through console with a mapping type of 'php' instead of my default 'annotations'. Immediately without even touching the generated class my site wouldn't load. It threw this MappingException for my user class.
I removed the new Entity class thinking that would fix it, and everything else I could think of. Then I looked in my app/Resources/config directory. There was a new directory called Doctrine. It contained a mapping file for the Entity that caused the issue. When I removed this directory and file, the Exception was gone.
If you want to use annotations to defining your mapping, just select the option "annotations" when you generate your entity.
Although this question is quite old I don't want to keep a secret how I solved it. It seems that I had an old mapping files reference in the cache because I moved it to another folder. Clearing the cache solved the issue for me.
Related
i'm trying to understand how to work with form collections in sf 3 but i'm having trouble following the tutorial from sf website because it hits me with an error and i kind manage understanding what i'm doing wrong and i hope you can help me out.
Offer Entity
<?php
namespace Zenith\OfferGeneratorBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\ArrayCollection;
use Zenith\productsBundle\Entity\products;
/**
* Offer
*
* #ORM\Table(name="offer")
* #ORM\Entity(repositoryClass="Zenith\OfferGeneratorBundle\Repository\OfferRepository")
*/
class Offer
{
/**
* #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=255)
*/
private $name;
/**
* #var string
*
* #ORM\ManyToMany(targetEntity="ProductCollection", inversedBy="offer", cascade={"persist"})
*/
private $products;
/**
* #var \DateTime
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(name="createdAt", type="datetime")
*/
private $createdAt;
/**
* #var \DateTime
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(name="modifiedAt", type="datetime")
*/
private $modifiedAt;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Offer
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
*
* #return Offer
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set modifiedAt
*
* #param \DateTime $modifiedAt
*
* #return Offer
*/
public function setModifiedAt($modifiedAt)
{
$this->modifiedAt = $modifiedAt;
return $this;
}
/**
* Get modifiedAt
*
* #return \DateTime
*/
public function getModifiedAt()
{
return $this->modifiedAt;
}
/**
* Add product
*
* #param \Zenith\OfferGeneratorBundle\Entity\ProductCollection $product
*
* #return Offer
*/
public function addProduct(ProductCollection $product)
{
$product->addOffer($this);
$this->products->add($product);
}
/**
* Remove product
*
* #param \Zenith\OfferGeneratorBundle\Entity\ProductCollection $product
*/
public function removeProduct(\Zenith\OfferGeneratorBundle\Entity\ProductCollection $product)
{
$this->products->removeElement($product);
}
/**
* Get products
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
}
ProductCollection Entity
<?php
namespace Zenith\OfferGeneratorBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* #ORM\Table( )
* #ORM\Entity(repositoryClass="Zenith\OfferGeneratorBundle\Repository\ProductRepository")
*/
class ProductCollection
{
/**
* #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=255)
*/
private $name;
/**
* #var int
*
* #ORM\Column(name="price", type="integer")
*/
private $price;
/**
* #var int
*
* #ORM\Column(name="quantity", type="integer")
*/
private $quantity;
/**
* #var string
*
* #ORM\ManyToMany(targetEntity="Offer", mappedBy="products")
*/
private $offer;
/**
* #var int
*
* #ORM\ManyToMany(targetEntity="Zenith\ProductBundle\Entity\Product")
*/
private $product;
/**
* Constructor
*/
public function __construct()
{
$this->offer = new \Doctrine\Common\Collections\ArrayCollection();
$this->product = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return ProductCollection
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set price
*
* #param integer $price
*
* #return ProductCollection
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* #return integer
*/
public function getPrice()
{
return $this->price;
}
/**
* Set quantity
*
* #param integer $quantity
*
* #return ProductCollection
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* #return integer
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Add offer
*
* #param \Zenith\OfferGeneratorBundle\Entity\Offer $offer
*
* #return ProductCollection
*/
public function addOffer(\Zenith\OfferGeneratorBundle\Entity\Offer $offer)
{
if (!$this->offer->contains($offer)) {
$this->offer->add($offer);
}
}
/**
* Remove offer
*
* #param \Zenith\OfferGeneratorBundle\Entity\Offer $offer
*/
public function removeOffer(\Zenith\OfferGeneratorBundle\Entity\Offer $offer)
{
$this->offer->removeElement($offer);
}
/**
* Get offer
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getOffer()
{
return $this->offer;
}
/**
* Add product
*
* #param \Zenith\ProductBundle\Entity\Product $product
*
* #return ProductCollection
*/
public function addProduct(\Zenith\ProductBundle\Entity\Product $product)
{
$this->product[] = $product;
return $this;
}
/**
* Remove product
*
* #param \Zenith\ProductBundle\Entity\Product $product
*/
public function removeProduct(\Zenith\ProductBundle\Entity\Product $product)
{
$this->product->removeElement($product);
}
/**
* Get product
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getProduct()
{
return $this->product;
}
}
When i try to add something using the form after the submit i get the following error.
Error: Call to a member function add() on a non-object
500 Internal Server Error - FatalErrorException
Stack Trace:
{
$product->addOffer($this);
$this->products->add($product);
}
/**
Based on one of the answers from here i changed the code and i got another error that i think it's from data transformer that i'm using on one of the fields.
<?php
namespace Zenith\OfferGeneratorBundle\Form\DataTransformer;
use Zenith\ProductBundle\Entity\Product;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class ProductNameTransformer implements DataTransformerInterface
{
private $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
/**
* Transforms an object (issue) to a string (number).
*
* #param Issue|null $issue
* #return string
*/
public function transform($issue)
{
if (null === $issue) {
return '';
}
return $issue->getId();
}
/**
* Transforms a string (number) to an object (issue).
*
* #param string $issueNumber
* #return Issue|null
* #throws TransformationFailedException if object (issue) is not found.
*/
public function reverseTransform($issueNumber)
{
// no issue number? It's optional, so that's ok
if (!$issueNumber) {
return;
}
$issue = $this->manager
->getRepository('ProductBundle:Product')
// query for the issue with this id
->findByName($issueNumber)
;
if (null === $issue) {
// causes a validation error
// this message is not shown to the user
// see the invalid_message option
throw new TransformationFailedException(sprintf(
'An issue with number "%s" does not exist!',
$issueNumber
));
}
return $issue;
}
}
Notice: Array to string conversion
500 Internal Server Error - ContextErrorException
Stack Trace
in vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php at line 67 -
public function bindValue($param, $value, $type = \PDO::PARAM_STR)
{
try {
return parent::bindValue($param, $value, $type);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
at ErrorHandler ->handleError ('8', 'Array to string conversion', '/var/www/f.dev/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php', '67', array('param' => '1', 'value' => array(object(Product)), 'type' => '2'))
at PDOStatement ->bindValue ('1', array(object(Product)), '2')
in vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php at line 67 +
at PDOStatement ->bindValue ('1', array(object(Product)), '2')
in vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php at line 120 +
at Statement ->bindValue ('1', array(object(Product)), 'string')
in vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php at line 277 +
at BasicEntityPersister ->executeInserts ()
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 1018 +
at UnitOfWork ->executeInserts (object(ClassMetadata))
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 378 +
at UnitOfWork ->commit (null)
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php at line 356 +
at EntityManager ->flush ()
in src/Zenith/OfferGeneratorBundle/Controller/OfferController.php at line 118 +
at OfferController ->newOfferSubmitedAction (object(Request))
at call_user_func_array (array(object(OfferController), 'newOfferSubmitedAction'), array(object(Request)))
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 139 +
at HttpKernel ->handleRaw (object(Request), '1')
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 62 +
at HttpKernel ->handle (object(Request), '1', true)
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php at line 169 +
at Kernel ->handle (object(Request))
in web/app_dev.php at line 30 +
You need to initialise your products collection in your __construct, otherwise you are attempting to call add on a null value (products will be initialised as null).
public function __construct()
{
$this->products = new ArrayCollection();
}
I'm in a deadend and can't seem to find my way out of this apparently simple problem.
I'm working with Symfony2 and trying to create a database table from an Entity that I generated with the generate:doctrine:entity command. But when I run doctrine:schema:create I always get a "No Metadata Classes to process" message like Doctrine can't find my entity.
I checked the database, the namespace, the annotations, tried creating other entities manually instead of by using the generate:doctrine:entity command but I always get the same result.
Here is my entity :
namespace Blogger\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="blog")
*/
class Blog
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="author", type="string", length=100)
*/
private $author;
/**
* #var string
*
* #ORM\Column(name="blog", type="text")
*/
private $blog;
/**
* #var string
*
* #ORM\Column(name="image", type="string", length=20)
*/
private $image;
/**
* #var string
*
* #ORM\Column(name="tags", type="text")
*/
private $tags;
/**
* #var \DateTime
*
* #ORM\Column(name="created", type="datetime")
*/
private $created;
/**
* #var \DateTime
*
* #ORM\Column(name="updated", type="datetime")
*/
private $updated;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Blog
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set author
*
* #param string $author
*
* #return Blog
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set blog
*
* #param string $blog
*
* #return Blog
*/
public function setBlog($blog)
{
$this->blog = $blog;
return $this;
}
/**
* Get blog
*
* #return string
*/
public function getBlog()
{
return $this->blog;
}
/**
* Set image
*
* #param string $image
*
* #return Blog
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* #return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set tags
*
* #param string $tags
*
* #return Blog
*/
public function setTags($tags)
{
$this->tags = $tags;
return $this;
}
/**
* Get tags
*
* #return string
*/
public function getTags()
{
return $this->tags;
}
/**
* Set created
*
* #param \DateTime $created
*
* #return Blog
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* #param \DateTime $updated
*
* #return Blog
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* #return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
}
I'm getting out of ideas. I saw other similar questions but none that could help me. If someone knows what to do, I'd love your help ;)
Something weird happened to me when I put my entity class in a directory called Models, so my namespace was:
AppBundle\Models
But it didn't work.
So I removed that folder and create another folder exactly named Entity and it worked.
So try to remove BlogBundle and put your Entity folder right under you Appbundle folder
Most of the time This error come because of entity path issue and cache
run command first
doctrine:cache:clear-metadata
and then try
doctrine:schema:create
I had the same problem. Try to remove Blogger from your namespace:
namespace BlogBundle\Entity;
I am trying to write a web service to fetch the category list and all the business under that category in an nested array fashion.
I am getting an Semantical Error saying :
{
code: 500
message: "[Semantical Error] line 0, col 14 near 'StreetBumbApiBundle:Buss_owner': Error: Class 'StreetBumb\ApiBundle\Entity\Buss_owner' is not defined."
}
I already defined the entity in the controller, i dont know why it is showing this error.
This is how my controller's function looks like:
public function getCategoryAction(){
$em = $this->getDoctrine()->getEntityManager();
$pro = $em->getRepository('StreetBumbApiBundle:Category')
->getBusinessByCategory();
//return $pro;
$i = 0;
foreach($pro as $p)
{
$data['category'][$i]['id'] = $p->getId();
$data['category'][$i]['Name'] = $p->getCatName();
//$result[$i] = $p->getId();
$catId = $p->getId();
$business = $em->createQuery('SELECT b FROM StreetBumbApiBundle:Buss_owner b WHERE b.catId = :catId')->setParameter('catId', $catId);
$result = $business->getResult();
foreach($result as $r)
{
$data['business'][$i]['id'][] = $r->getId();
}
$i++;
}
return $data;
}
Please guide if anyone have idea.. Thanx
UPDATE:
Buss_owner Entity:
<?php
namespace StreetBumb\ApiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Buss_owner
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="StreetBumb\ApiBundle\Entity\Buss_ownerRepository")
*/
class Buss_owner
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=50)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* #var integer
*
* #ORM\Column(name="phno", type="integer")
*/
private $phno;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="fbId", type="integer")
*/
private $fbId;
/**
* #var string
*
* #ORM\Column(name="uniqueId", type="string", length=255)
*/
private $uniqueId;
/**
* #var integer
*
* #ORM\Column(name="catId", type="integer")
* #ORM\ManyToMany(targetEntity="Category", mappedBy="Buss_owner")
*/
private $catId;
public function __construct() {
$this->catId = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Buss_owner
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set email
*
* #param string $email
* #return Buss_owner
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set phno
*
* #param integer $phno
* #return Buss_owner
*/
public function setPhno($phno)
{
$this->phno = $phno;
return $this;
}
/**
* Get phno
*
* #return integer
*/
public function getPhno()
{
return $this->phno;
}
/**
* Set address
*
* #param string $address
* #return Buss_owner
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set catId
*
* #param integer $catId
* #return Buss_owner
*/
public function setCatId($catId)
{
$this->catId = $catId;
return $this;
}
/**
* Get catId
*
* #return integer
*/
public function getCatId()
{
return $this->catId;
}
/**
* Set password
*
* #param string $password
* #return Buss_owner
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set uniqueId
*
* #param string $uniqueId
* #return Buss_owner
*/
public function setUniqueId($uniqueId)
{
$this->uniqueId = $uniqueId;
return $this;
}
/**
* Get uniqueId
*
* #return string
*/
public function getUniqueId()
{
return $this->uniqueId;
}
/**
* Set fbId
*
* #param integer $fbId
* #return Buss_owner
*/
public function setFbId($fbId)
{
$this->fbId = $fbId;
return $this;
}
/**
* Get fbId
*
* #return integer
*/
public function getFbId()
{
return $this->fbId;
}
}
I believe the problem is the underscore in the entity name.
Doctrine's naming convention is to use CamelCase which it then converts to underscores in the database. Due to this behind the scenes magic, underscores can cause problems in entity names.
Try changing the entity class name to BussOwner and calling this in the controller. If you can't change the table name you can handle this by modifying the entity annotation to:
#ORM\Table(name="buss_owner")
I have a Util class in a Symfony 2.4 proyect that get the slug from a book. The slug cannot be repeated. Each slug must be unique. I have the following code form the class that generate the slug. I have the error:
Fatal error: Using $this when not in object context in C:\xampp\htdocs\gens\src\
Sakya\GensBundle\Util\Util.php on line 38
namespace Sakya\GensBundle\Util;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManager;
class Util
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public static function getSlug($slug)
{
$separador = '-';
$tildes=array('á','é','í','ó','ú');
$vocales=array('a','e','i','o','u');
str_replace($vocales,$tildes,$slug);
$slug = iconv('UTF-8', 'ASCII//TRANSLIT', $slug);
$slug = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $slug);
$slug = strtolower(trim($slug, $separador));
$slug = preg_replace("/[\/_|+ -]+/", $separador, $slug);
$i = 1;
while (! $this->em->getRepository("GensBundle:Libro")->findBySlug($slug)){
$slug = $slug."-".$i;
$i++;
}
return $slug;
}
}
It is mi Entity "Libro"
<?php
namespace Sakya\GensBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Sakya\GensBundle\Util\Util;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Libro
*
* #ORM\Table()
* #ORM\Entity
*/
class Libro
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="libro", type="string", length=255)
*/
private $libro;
/**
* #ORM\Column(type="string")
*
* #Assert\NotBlank()
*/
protected $slug;
/**
* #var string
*
* #ORM\Column(name="autor", type="string", length=255)
*/
private $autor;
/**
* #var string
*
* #ORM\Column(name="prefacio", type="string", length=700)
*/
private $prefacio;
/**
* #ORM\OneToMany(targetEntity="Capitulo", mappedBy="libro")
*/
private $capitulo;
public function __toString()
{
return $this->getLibro();
}
/**
* Constructor
*/
public function __construct()
{
$this->capitulo = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set libro
*
* #param string $libro
* #return Libro
*/
public function setLibro($libro)
{
$this->libro = $libro;
$this->slug = Util::getSlug($libro);
return $this;
}
/**
* Get libro
*
* #return string
*/
public function getLibro()
{
return $this->libro;
}
/**
* Set autor
*
* #param string $autor
* #return Libro
*/
public function setAutor($autor)
{
$this->autor = $autor;
return $this;
}
/**
* Get autor
*
* #return string
*/
public function getAutor()
{
return $this->autor;
}
/**
* Add capitulo
*
* #param \Sakya\GensBundle\Entity\Capitulo $capitulo
* #return Libro
*/
public function addCapitulo(\Sakya\GensBundle\Entity\Capitulo $capitulo)
{
$this->capitulo[] = $capitulo;
return $this;
}
/**
* Remove capitulo
*
* #param \Sakya\GensBundle\Entity\Capitulo $capitulo
*/
public function removeCapitulo(\Sakya\GensBundle\Entity\Capitulo $capitulo)
{
$this->capitulo->removeElement($capitulo);
}
/**
* Get capitulo
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCapitulo()
{
return $this->capitulo;
}
/**
* Set prefacio
*
* #param string $prefacio
* #return Libro
*/
public function setPrefacio($prefacio)
{
$this->prefacio = $prefacio;
return $this;
}
/**
* Get prefacio
*
* #return string
*/
public function getPrefacio()
{
return $this->prefacio;
}
/**
* Set slug
*
* #param string $slug
* #return Libro
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* #return string
*/
public function getSlug()
{
return $this->slug;
}
}
How can I make a consult that check that the slug is not in the database form the Uti Class?
Thanks for all and sorry for my English
The problem is you are trying refer to a property from static method. Don't do it unless the property is also static.
The best way I can suggest is change the static method into not-static, then create a service from Util class, and make validation outside the entity. It's not a good idea using service inside entity class.
You didn't show the context but I can guess: if you want set slug based on a form - then the best way to achieve what you want would be create a validator.
I develop store using SyliusSandbox bundle.
Sylius uses xml files to store ORM schema for entities.
I've copied its xml definitions to my Bundle and use it there.
But for my own entities, I'd like to use annotations. So, basically I need to mix two types of definitions in one bundle.
If I try to persist an entity which is using annotations, I get an error that xml file for this entity was not found:
No mapping file found named
'ShopBundle\Resources\config\doctrine/ProductLocalized.orm.xml' for
class '\ShopBundle\Entity\ProductLocalized'.
My entity looks like this:
<?php
namespace Pixeljets\ShopBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Pixeljets\ShopBundle\Entity\ProductLocalized
*
* #ORM\Table(name="product_localized")
* #ORM\Entity
*/
class ProductLocalized
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Product", inversedBy="localized")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $product;
/**
* #var integer $product_id
*
* #ORM\Column(name="product_id", type="integer")
*/
private $product_id;
/**
* #var string $title
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string $url
*
* #ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* #var string $keywords
*
* #ORM\Column(name="keywords", type="string", length=255, nullable=true)
*/
private $keywords;
/**
* #var string $description
*
* #ORM\Column(name="description", type="string", length=255, nullable=true)
*/
private $description;
/**
* #var string $body
*
* #ORM\Column(name="body", type="text", nullable=true)
*/
private $body;
/**
* #var boolean $published
*
* #ORM\Column(name="published", type="boolean")
*/
private $published;
/**
* #var string $lang
*
* #ORM\Column(name="lang", type="string", length=2)
*/
private $lang;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set product_id
*
* #param integer $productId
* #return ProductLocalized
*/
public function setProductId($productId)
{
$this->product_id = $productId;
return $this;
}
/**
* Get product_id
*
* #return integer
*/
public function getProductId()
{
return $this->product_id;
}
/**
* Set title
*
* #param string $title
* #return ProductLocalized
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set url
*
* #param string $url
* #return ProductLocalized
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* #return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set keywords
*
* #param string $keywords
* #return ProductLocalized
*/
public function setKeywords($keywords)
{
$this->keywords = $keywords;
return $this;
}
/**
* Get keywords
*
* #return string
*/
public function getKeywords()
{
return $this->keywords;
}
/**
* Set description
*
* #param string $description
* #return ProductLocalized
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set body
*
* #param string $body
* #return ProductLocalized
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* Get body
*
* #return string
*/
public function getBody()
{
return $this->body;
}
/**
* Set published
*
* #param boolean $published
* #return ProductLocalized
*/
public function setPublished($published)
{
$this->published = $published;
return $this;
}
/**
* Get published
*
* #return boolean
*/
public function getPublished()
{
return $this->published;
}
/**
* Set lang
*
* #param string $lang
* #return ProductLocalized
*/
public function setLang($lang)
{
$this->lang = $lang;
return $this;
}
/**
* Get lang
*
* #return string
*/
public function getLang()
{
return $this->lang;
}
/**
* Set product
*
* #param Pixeljets\ShopBundle\Entity\Product $product
* #return ProductLocalized
*/
public function setProduct(\Pixeljets\ShopBundle\Entity\Product $product = null)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* #return Pixeljets\ShopBundle\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
}
?>
How can I 'tell' symfony to use annotation approach for schema?
You cannot mix metadata formats inside of a bundle:
From: http://symfony.com/doc/current/book/doctrine.html
A bundle can accept only one metadata definition format. For example, it's not
possible to mix YAML metadata definitions with annotated PHP entity class definitions.
You will need to use two bundles or stick with one format.
You should think about using Translatable behavior from DoctrineExtensions to make your products support i18n. Should be much easier than creating another entity for that.