Symfony3 - String returned instead of integer - php

In my User entity, I have this:
/**
* #ORM\Entity
* #ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var \Doctrine\Common\Collections\ArrayCollection
* #ORM\OneToMany(targetEntity="Blog\BlogBundle\Entity \Entry",mappedBy="author")
*/
protected $entries;
This is my full Entry entity.
<?php
namespace Blog\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Entry
*
* #ORM\Table(name="entry")
* #ORM\Entity(repositoryClass="Blog\BlogBundle\Repository\EntryRepository")
*/
class Entry
{
/**
* #var int
*
* #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="book", type="text")
*/
private $book;
/**
* #var \DateTime
*
* #ORM\Column(name="timestamp", type="datetime")
*/
private $timestamp;
/**
* #var AppBundle\Entity
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\User",inversedBy="entries")
* #ORM\JoinColumn(name="author", referencedColumnName="id")
*/
private $author;
/**
* #var \Doctrine\Common\Collections\ArrayCollection
* #ORM\OneToMany(targetEntity="Blog\BlogBundle\Entity\Reviews",mappedBy="book_id")
*/
protected $reviews;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Entry
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set book
*
* #param string $book
*
* #return Entry
*/
public function setBook($book)
{
$this->book = $book;
return $this;
}
/**
* Get book
*
* #return string
*/
public function getBook()
{
return $this->book;
}
/**
* Set timestamp
*
* #param \DateTime $timestamp
*
* #return Entry
*/
public function setTimestamp($timestamp)
{
$this->timestamp = $timestamp;
return $this;
}
/**
* Get timestamp
*
* #return \DateTime
*/
public function getTimestamp()
{
return $this->timestamp;
}
/**
* Set author
*
* #param \AppBundle\Entity\User $author
*
* #return Entry
*/
public function setAuthor(\AppBundle\Entity\User $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return \AppBundle\Entity\User
*/
public function getAuthor()
{
return $this->author;
}
/**
* Get author ID
*
* #return \AppBundle\Entity\User
*/
public function getAuthorId()
{
$id = $this->author;
return $this->$id;
}
public function __toString()
{
try {
return (string) $this->id;
} catch (Exception $exception) {
return '';
}
}
}
From this piece of code I am trying to retrieve Author ID, but instead author name is being returned. I presume this is because I am converting to string at the end. If I remove it, I get this error: symfony2 Catchable Fatal Error: Object of class could not be converted to string
To get the link, I am doing the following:
Setting path author with parameter of id in routing.yml of the bundle which triggers authorAction($id) inside BookController. Expecting an integer to be passed.
authorAction($id) sets up Doctrine Manager, calls getAllBooksByAuthor() function in a repository where a relevant query returns all books by author with id and renders the twig template.
The link is generated in the twig template, but instead parameter passed is author name - which is a string, not an integer.
How can I do it so author ID is passed instead of author name?
I'm stuck here and I need Entry entity to be able to handle both strings and integers.

Specifically this is the change:
/**
* Get author ID
*
* #return intger
*/
public function getAuthorId()
{
return $this->author->getId();
}
Note: the #return documentation should be changed to show it's returning an integer.

Related

Symfony 2.8 Catchable Fatal Error: Object of class ... could not be converted to string (one to many and many to one)

This is my first question here, so please forgive me if I omitted something.
I have 2 entities defined: Product and Category.
<?php
namespace Backend\AdminBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* #ORM\Table(name="category")
* #ORM\Entity(repositoryClass="Backend\AdminBundle\Repository\CategoryRepository")
*/
class Category
{
/**
* #ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* #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=100, unique=true)
*/
private $name;
/**
* #var int
*
* #ORM\Column(name="status", type="integer")
*/
private $status;
/**
* #var int
*
* #ORM\Column(name="parent_id", type="integer")
*/
private $parentId;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set status
*
* #param integer $status
* #return Category
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Set parentId
*
* #param integer $parentId
* #return Category
*/
public function setParentId($parentId)
{
$this->parentId = $parentId;
return $this;
}
/**
* Get parentId
*
* #return integer
*/
public function getParentId()
{
return $this->parentId;
}
}
and
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* #ORM\Table(name="product")
* #ORM\Entity(repositoryClass="Backend\AdminBundle\Repository\ProductRepository")
*/
class Product
{
/**
* #ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* #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\Column(name="short_description", type="string", length=255)
*/
private $shortDescription;
/**
* #var string
*
* #ORM\Column(name="full_description", type="text")
*/
private $fullDescription;
/**
* #var float
*
* #ORM\Column(name="price", type="float")
*/
private $price;
/**
* #var int
*
* #ORM\Column(name="status", type="integer")
*/
private $status;
/**
* #var int
*
* #ORM\Column(name="category_id", type="integer")
*/
private $categoryId;
/**
* 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 shortDescription
*
* #param string $shortDescription
* #return Product
*/
public function setShortDescription($shortDescription)
{
$this->shortDescription = $shortDescription;
return $this;
}
/**
* Get shortDescription
*
* #return string
*/
public function getShortDescription()
{
return $this->shortDescription;
}
/**
* Set fullDescription
*
* #param string $fullDescription
* #return Product
*/
public function setFullDescription($fullDescription)
{
$this->fullDescription = $fullDescription;
return $this;
}
/**
* Get fullDescription
*
* #return string
*/
public function getFullDescription()
{
return $this->fullDescription;
}
/**
* 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 status
*
* #param integer $status
* #return Product
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Set categoryId
*
* #param integer $categoryId
* #return Product
*/
public function setCategoryId($categoryId)
{
$this->categoryId = $categoryId;
return $this;
}
/**
* Get categoryId
*
* #return integer
*/
public function getCategoryId()
{
return $this->categoryId;
}
/**
* Set category
*
* #param \Backend\AdminBundle\Entity\Category $category
* #return Product
*/
public function setCategory(\Backend\AdminBundle\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \Backend\AdminBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
}
I've managed to properly set the relation between them as Category has Many Product and Product has one Category.
I successfully created my CRUD for Category and it works.
I was able to create my CRUD for Product, accessed the index route and works just fine.
The issue is that when I try to create a new Product I get the following exception:
Catchable Fatal Error: Object of class Backend\AdminBundle\Entity\Category could not be converted to string
It's my first Symfony test project and beside being a total noob, I'm stuck. I know I must be omitting something extremely obvious but I just can't tell what.
I'm using Symfony 2.8.
What am I doing wrong?
Define a magic method __toString() for your Category entity.
For sample, do it like this:
public function __toString()
{
return $this->getName();
}

Save copy of entity doctrine/symfony2

I want to keep the previous version of an entity. When the 'old' entity is updated I want to save it with the same id but with a different revision number so it looks something like this
id: 1 revision_number: 1
id: 1 revision_number: 2
This is the entity
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Form
*
* #ORM\Table()
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class Form
{
/**
* #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=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="export_template", type="string", length=255, nullable = true)
*/
private $exportTemplate;
/**
* #var \DateTime
*
* #ORM\Column(name="revision", type="datetime", nullable = true)
*/
private $revision;
/**
* #var integer
*
* #ORM\Column(name="revision_number", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $revisionNumber;
/**
* #ORM\ManyToOne(targetEntity="Client", inversedBy="forms")
* #ORM\JoinColumn(name="form_id", referencedColumnName="id")
*/
protected $client;
/**
* #ORM\OneToMany(targetEntity="Section", mappedBy="form", cascade={"persist"})
*/
protected $sections;
/**
* #ORM\OneToMany(targetEntity="Inspection", mappedBy="form")
*/
protected $inspections;
public function __construct()
{
$this->sections = new ArrayCollection();
$this->inspections = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Form
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set exportTemplate
*
* #param string $exportTemplate
* #return Form
*/
public function setExportTemplate($exportTemplate)
{
$this->exportTemplate = $exportTemplate;
return $this;
}
/**
* Get exportTemplate
*
* #return string
*/
public function getExportTemplate()
{
return $this->exportTemplate;
}
/**
* Set revision
*
* #param \DateTime $revision
* #return Form
*/
public function setRevision($revision)
{
$this->revision = $revision;
return $this;
}
/**
* Get revision
*
* #return \DateTime
*/
public function getRevision()
{
return $this->revision;
}
/**
* #param $revisionNumber
* #return Form
*/
public function setRevisionNumber($revisionNumber)
{
$this->revisionNumber = $revisionNumber;
return $this;
}
/**
* #return int
*/
public function getRevisionNumber()
{
return $this->revisionNumber;
}
/**
* Set client
*
* #param \AppBundle\Entity\Client $client
* #return Form
*/
public function setClient(\AppBundle\Entity\Client $client = null)
{
$this->client = $client;
return $this;
}
/**
* Get client
*
* #return \AppBundle\Entity\Client
*/
public function getClient()
{
return $this->client;
}
/**
* Add sections
*
* #param \AppBundle\Entity\Section $sections
* #return Form
*/
public function addSection(\AppBundle\Entity\Section $sections)
{
$this->sections[] = $sections;
return $this;
}
/**
* Remove sections
*
* #param \AppBundle\Entity\Section $sections
*/
public function removeSection(\AppBundle\Entity\Section $sections)
{
$this->sections->removeElement($sections);
}
/**
* Get sections
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getSections()
{
return $this->sections;
}
/**
* Add inspections
*
* #param \AppBundle\Entity\Inspection $inspections
* #return Form
*/
public function addInspection(\AppBundle\Entity\Inspection $inspections)
{
$this->inspections[] = $inspections;
return $this;
}
/**
* Remove inspections
*
* #param \AppBundle\Entity\Inspection $inspections
*/
public function removeInspection(\AppBundle\Entity\Inspection $inspections)
{
$this->inspections->removeElement($inspections);
}
/**
* Get inspections
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getInspections()
{
return $this->inspections;
}
/**
*
* #ORM\PrePersist()
*/
public function preSetDate(){
$this->revision = new \DateTime();
}
}
Is there a way I can do what I described?
I think what you may need is Loggable extension for Doctrine. Check this link:
https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/loggable.md
Loggable behavior tracks your record changes and is able to manage versions.
Using loggable behavior you can revert version from your repository with revert() method. You can find many examples on site I gave you the link above.
If you dont fancy using a 3rd party bundle for this:
your ID's will still have to be unique. If you need to track where the copy originated from then you could add a new parameter.
Once you have decided upon this, I would simply clone it, alter the revision number and add the original id if thats which way you want to go and then persist it.
class Form {
// ....
$orig_id = null;
// any getters/setters you need
// .....
}
then in the controller:
public function copyEntity($entity) {
$new_ent = clone $entity;
$new_ent->setOrigId( $entity->getId() );
$new_ent->setRevision( true ); // I would probably bin this as origId being !== null would do the same job
$this->entityManager->persist( $new_ent );
$this->entityManager->flush();
// .....
}

The target-entity cannot be found in - MappingException

I have Symfony project, in which there are 2 entities - Building and Building_type. They are connected with ManyToMany uni-directional association. So, when I try to access my controller, I have this error:
The target-entity Farpost\StoreBundle\Entity\Building_type cannot be found in 'Farpost\StoreBundle\Entity\Building#building_types'.
Farpost/StoreBundle/Entity/Building.php:
namespace Farpost\StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
*
*/
class Building
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="alias", type="string", length=255)
*/
protected $alias;
/**
* #var string
*
* #ORM\Column(name="number", type="string", length=255)
*/
protected $number;
/**
* #ORM\ManyToMany(targetEntity="Building_type")
* #ORM\JoinTable(name="buildings_types",
* joinColumns={#ORM\JoinColumn(name="building_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="building_type_id", referencedColumnName="id")}
* )
*/
protected $building_types;
public function __construct()
{
$this->building_types = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set alias
*
* #param string $alias
* #return Building
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
/**
* Get alias
*
* #return string
*/
public function getAlias()
{
return $this->alias;
}
/**
* Set number
*
* #param string $number
* #return Building
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* #return string
*/
public function getNumber()
{
return $this->number;
}
/**
* Add building_types
*
* #param \Farpost\StoreBundle\Entity\Building_type $buildingTypes
* #return Building
*/
public function addBuildingType(\Farpost\StoreBundle\Entity\Building_type $buildingTypes)
{
$this->building_types[] = $buildingTypes;
return $this;
}
/**
* Remove building_types
*
* #param \Farpost\StoreBundle\Entity\Building_type $buildingTypes
*/
public function removeBuildingType(\Farpost\StoreBundle\Entity\Building_type $buildingTypes)
{
$this->building_types->removeElement($buildingTypes);
}
/**
* Get building_types
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getBuildingTypes()
{
return $this->building_types;
}
/**
* Add buildings_types
*
* #param \Farpost\StoreBundle\Entity\Buildings_types $buildingsTypes
* #return Building
*/
public function addBuildingsType(\Farpost\StoreBundle\Entity\Buildings_types $buildingsTypes)
{
$this->buildings_types[] = $buildingsTypes;
return $this;
}
/**
* Remove buildings_types
*
* #param \Farpost\StoreBundle\Entity\Buildings_types $buildingsTypes
*/
public function removeBuildingsType(\Farpost\StoreBundle\Entity\Buildings_types $buildingsTypes)
{
$this->buildings_types->removeElement($buildingsTypes);
}
/**
* Get buildings_types
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getBuildingsTypes()
{
return $this->buildings_types;
}
}
Farpost/StoreBundle/Entity/Building_type.php:
namespace Farpost\StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
*
* #ORM\Entity
*
*/
class Building_type
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="alias", type="string", length=255)
*/
protected $alias;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set alias
*
* #param string $alias
* #return Building_type
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
/**
* Get alias
*
* #return string
*/
public function getAlias()
{
return $this->alias;
}
}
Farpost/APIBundle/Controller/DefaultController.php:
public function listAction($name)
{
$repository = $this->getDoctrine()->getManager()
->getRepository('FarpostStoreBundle:Building');
$items = $repository->findAll();
$response = new Response(json_encode($items));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
Also, app/console doctrine:schema:validate output is:
[Mapping] OK - The mapping files are correct.
[Database] OK - The database schema is in sync with the mapping files.
Because the name of the entity contains an underscore _ and the PSR-0 autoloader will try to find it in Farpost/StoreBundle/Entity/Building/type.php.
You need to rename your class to BuildingType and put it in Farpost/StoreBundle/Entity/BuildingType.php
Also, make sure that your composer.json has proper entries pointing to proper namespace. Mine did not, causing this error.

Symfony2 and Doctrine entity undefined method

I have this same issue as here The method name must start with either findBy or findOneBy. Undefined method Symfony? but the answers don't help.
When I run my code
<?php
namespace Map\ViewBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Map\ViewBundle\Entity\Markers;
class DefaultController extends Controller
{
public function getMarkersAction()
{
$em = $this->getDoctrine()->getManager();
$em->getRepository('MapViewBundle:Markers')
->findAllMarkers();
}
//...
}
I get exception
Undefined method 'findAllMarkers'. The method name must start with
either findBy or findOneBy!
this is my MarkersRepository file (located in Entity directory)
<?php
namespace Map\ViewBundle\Entity;
use Doctrine\ORM\EntityRepository;
class MarkersRepository extends EntityRepository
{
public function findAllMarkers() {
//this query will be different
return $this->getEntityManager()
->createQuery(
'SELECT m FROM MapViewBundle:Markers m'
)
->getResult();
}
}
and this is Markers entity class (located in Entity directory as well)
<?php
namespace Map\ViewBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table(name="markers")
* #ORM\Entity(repositoryClass="Map\ViewBundle\Entity\MarkersRepository")
*/
class Markers
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100, nullable=false)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=500, nullable=false)
*/
private $description;
/**
* #var integer
*
* #ORM\Column(name="icon", type="integer", nullable=false)
*/
private $icon;
/**
* #var \DateTime
*
* #ORM\Column(name="post_date", type="datetime", nullable=false)
*/
private $postDate;
/**
* #var float
*
* #ORM\Column(name="lat", type="float", precision=10, scale=0, nullable=false)
*/
private $lat;
/**
* #var float
*
* #ORM\Column(name="lng", type="float", precision=10, scale=0, nullable=false)
*/
private $lng;
/**
* #var integer
*
* #ORM\Column(name="relevance_degree", type="integer", nullable=false)
*/
private $relevanceDegree;
/**
* #var string
*
* #ORM\Column(name="geohash", type="string", length=12, nullable=false)
*/
private $geohash;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Map\ViewBundle\Entity\Tags", inversedBy="idMarkers")
* #ORM\JoinTable(name="markers_tags",
* joinColumns={
* #ORM\JoinColumn(name="id_markers", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="id_tags", referencedColumnName="id")
* }
* )
*/
private $idTags;
/**
* Constructor
*/
public function __construct()
{
$this->idTags = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set name
*
* #param string $name
* #return Markers
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* #param string $description
* #return Markers
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set icon
*
* #param integer $icon
* #return Markers
*/
public function setIcon($icon)
{
$this->icon = $icon;
return $this;
}
/**
* Get icon
*
* #return integer
*/
public function getIcon()
{
return $this->icon;
}
/**
* Set postDate
*
* #param \DateTime $postDate
* #return Markers
*/
public function setPostDate($postDate)
{
$this->postDate = $postDate;
return $this;
}
/**
* Get postDate
*
* #return \DateTime
*/
public function getPostDate()
{
return $this->postDate;
}
/**
* Set lat
*
* #param float $lat
* #return Markers
*/
public function setLat($lat)
{
$this->lat = $lat;
return $this;
}
/**
* Get lat
*
* #return float
*/
public function getLat()
{
return $this->lat;
}
/**
* Set lng
*
* #param float $lng
* #return Markers
*/
public function setLng($lng)
{
$this->lng = $lng;
return $this;
}
/**
* Get lng
*
* #return float
*/
public function getLng()
{
return $this->lng;
}
/**
* Set relevanceDegree
*
* #param integer $relevanceDegree
* #return Markers
*/
public function setRelevanceDegree($relevanceDegree)
{
$this->relevanceDegree = $relevanceDegree;
return $this;
}
/**
* Get relevanceDegree
*
* #return integer
*/
public function getRelevanceDegree()
{
return $this->relevanceDegree;
}
/**
* Set geohash
*
* #param string $geohash
* #return Markers
*/
public function setGeohash($geohash)
{
$this->geohash = $geohash;
return $this;
}
/**
* Get geohash
*
* #return string
*/
public function getGeohash()
{
return $this->geohash;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add idTags
*
* #param \Map\ViewBundle\Entity\Tags $idTags
* #return Markers
*/
public function addIdTag(\Map\ViewBundle\Entity\Tags $idTags)
{
$this->idTags[] = $idTags;
return $this;
}
/**
* Remove idTags
*
* #param \Map\ViewBundle\Entity\Tags $idTags
*/
public function removeIdTag(\Map\ViewBundle\Entity\Tags $idTags)
{
$this->idTags->removeElement($idTags);
}
/**
* Get idTags
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getIdTags()
{
return $this->idTags;
}
}
I tried clear cache and use command
php app/console doctrine:generate:entities Map
but still exception is throw. Anybody see whats wrong with my code?
Solution
Removing files: Markers.orm.yml and Tags.orm.yml from Map\ViewBundle\Resources\config\doctrine directory is the solution.
Start by verifying that you are not getting the correct repository.
$repo = $em->getRepository('MapViewBundle:Markers')
die('Repo Class ' . get_class($repo));
If you do happen to get the correct class then you have a typo.
And make sure you don't have any doctrine/markers.orm.yml or xml files hanging around.
Finally, update your question with the doctrine section in app/config/config.yml
This may sound weird but I encountered a similar issue with a custom repository function.
Try renaming your Repository function to getAllMarkers() and update the call in the controller and try again. This solved the issue for me on that occasion.

Symfony2 - Doctrine exception: class used in the discriminator map does not exist

I am using Symfony2 with Doctrine and when I query from my controller the next error appears(it appears in the navigator when I call for the page):
Entity class 'Bdreamers\SuenoBundle\Entity\Sueno_video' used in the discriminator map of class 'Bdreamers\SuenoBundle\Entity\Sueno' does not exist.
I have one entity(superclass) called "Sueno" and two entities that extend from it(subclasses): Sueno_foto and Sueno_video.
When I load the fixtures, Doctrine works perfectly and fills the database without any issue, filling correctly the discriminator field "tipo" in the "Sueno" table. It also fills correctly the inherited entity table "Sueno_video" introducing the ID of "Sueno" and the exclusive fields of "Sueno_video"
This is the code of the entity file for "Sueno":
<?php
namespace Bdreamers\SuenoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table()
* #ORM\Entity
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="tipo", type="string")
* #ORM\DiscriminatorMap({"sueno" = "Sueno", "video" = "Sueno_video", "foto" = "Sueno_foto"})
*/
class Sueno
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Bdreamers\UsuarioBundle\Entity\Usuario")
**/
private $usuario;
/**
* #ORM\ManyToMany(targetEntity="Bdreamers\SuenoBundle\Entity\Tag", inversedBy="suenos")
* #ORM\JoinTable(name="suenos_tags")
**/
private $tags;
/**
* #ORM\ManyToMany(targetEntity="Bdreamers\UsuarioBundle\Entity\Usuario", mappedBy="suenos_sigue")
* #ORM\JoinTable(name="usuarios_siguen")
**/
private $usuariosSeguidores;
/**
* #ORM\ManyToMany(targetEntity="Bdreamers\UsuarioBundle\Entity\Usuario", mappedBy="suenos_colabora")
* #ORM\JoinTable(name="usuarios_colaboran")
**/
private $usuariosColaboradores;
/**
* #var \DateTime
*
* #ORM\Column(name="fecha_subida", type="datetime")
*/
private $fechaSubida;
/**
* #var string
*
* #ORM\Column(name="titulo", type="string", length=40)
*/
private $titulo;
/**
* #var string
*
* #ORM\Column(name="que_pido", type="string", length=140)
*/
private $quePido;
/**
* #var string
*
* #ORM\Column(name="texto", type="string", length=540)
*/
private $texto;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set usuario
*
* #param string $usuario
* #return Sueno
*/
public function setUsuario($usuario)
{
$this->usuario = $usuario;
return $this;
}
/**
* Get usuario
*
* #return string
*/
public function getUsuario()
{
return $this->usuario;
}
public function getTags()
{
return $this->tags;
}
/**
* Set usuariosSeguidores
*
* #param string $usuariosSeguidores
* #return Sueno
*/
public function setUsuariosSeguidores($usuariosSeguidores)
{
$this->usuariosSeguidores = $usuariosSeguidores;
return $this;
}
/**
* Get usuariosSeguidores
*
* #return string
*/
public function getUsuariosSeguidores()
{
return $this->usuariosSeguidores;
}
/**
* Set usuariosColaboradores
*
* #param string $usuariosColaboradores
* #return Sueno
*/
public function setUsuariosColaboradores($usuariosColaboradores)
{
$this->usuariosColaboradores = $usuariosColaboradores;
return $this;
}
/**
* Get usuariosColaboradores
*
* #return string
*/
public function getUsuariosColaboradores()
{
return $this->usuariosColaboradores;
}
/**
* Set fechaSubida
*
* #param \DateTime $fechaSubida
* #return Sueno
*/
public function setFechaSubida($fechaSubida)
{
$this->fechaSubida = $fechaSubida;
return $this;
}
/**
* Get fechaSubida
*
* #return \DateTime
*/
public function getFechaSubida()
{
return $this->fechaSubida;
}
/**
* Set titulo
*
* #param string $titulo
* #return Sueno
*/
public function setTitulo($titulo)
{
$this->titulo = $titulo;
return $this;
}
/**
* Get titulo
*
* #return string
*/
public function getTitulo()
{
return $this->titulo;
}
/**
* Set quePido
*
* #param string $quePido
* #return Sueno
*/
public function setQuePido($quePido)
{
$this->quePido = $quePido;
return $this;
}
/**
* Get quePido
*
* #return string
*/
public function getQuePido()
{
return $this->quePido;
}
/**
* Set texto
*
* #param string $texto
* #return Sueno
*/
public function setTexto($texto)
{
$this->texto = $texto;
return $this;
}
/**
* Get texto
*
* #return string
*/
public function getTexto()
{
return $this->texto;
}
public function __construct() {
$this->usuariosColaboradores = new \Doctrine\Common\Collections\ArrayCollection();
$this->usuariosSeguidores = new \Doctrine\Common\Collections\ArrayCollection();
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __toString()
{
return $this->getTitulo();
}
}
And this is the code for the entity Sueno_video:
<?php
namespace Bdreamers\SuenoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table()
* #ORM\Entity
*/
class Sueno_video extends Sueno
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="link_video", type="string", length=255)
*/
private $linkVideo;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set linkVideo
*
* #param string $linkVideo
* #return Sueno_video
*/
public function setLinkVideo($linkVideo)
{
$this->linkVideo = $linkVideo;
return $this;
}
/**
* Get linkVideo
*
* #return string
*/
public function getLinkVideo()
{
return $this->linkVideo;
}
}
And finally the code in the controller:
public function homeAction()
{
$em = $this->getDoctrine()->getManager();
$suenos = $em->getRepository('SuenoBundle:Sueno')->findOneBy(array(
'fechaSubida' => new \DateTime('now -2 days')
));
return $this->render('EstructuraBundle:Home:home_registrado.html.twig');
}
The autoloader won't be able to resolve those class names to file paths, hence why it can't find your classes.
Changing the file and class names to SuenoVideo and SuenoFoto.

Categories