Symfony2 and Doctrine entity undefined method - php

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.

Related

using reflection methods in traits

I have 15 Entity Classes, and I want to put getEntityName static function in there but I don't want to make code duplication, and some of my entities extends vendor related class so I cannot put an abstract class to extend from these entities. I wanted to use traits.
<?php
namespace FrontendBundle\Traits\Entity;
trait GetEntityNameTrait {
public static function getEntityName()
{
return str_replace('\\', '\\\\', get_parent_class());
}
}
I use this trait in any of this entities, but I'm getting following compile error.
Compile Error: Cannot redeclare class FrontendBundle\Entity\Country
I tried several reflection methods all are the same result is this a bug or something is not logical ?
Others:
get_parent_class()
get_class()
get_called_class()
__CLASS__
<?php
namespace FrontendBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FrontendBundle\EntityInterface;
use Symfony\Component\Validator\Constraints as Assert;
use FrontendBundle\Traits\Entity\GetEntityNameTrait;
/**
* Country
*
* #ORM\Table(name="country", uniqueConstraints={#ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"}), #ORM\UniqueConstraint(name="iso_UNIQUE", columns={"iso"}), #ORM\UniqueConstraint(name="slug_tr_UNIQUE", columns={"slug_tr"}), #ORM\UniqueConstraint(name="slug_en_UNIQUE", columns={"slug_en"})})
* #ORM\Entity(repositoryClass="FrontendBundle\Repository\CountryRepository")
* #ORM\HasLifecycleCallbacks
*/
class Country implements EntityInterface
{
use GetEntityNameTrait;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="iso", type="string", length=2, nullable=false)
*/
private $iso;
/**
* #var string
*
* #ORM\Column(name="iso3", type="string", length=3, nullable=true)
*/
private $iso3;
/**
* #var string
*
* #ORM\Column(name="currency_code", type="string", length=3, nullable=true)
*/
private $currencyCode;
/**
* #var string
*
* #ORM\Column(name="currency_name", type="string", length=20, nullable=true)
*/
private $currencyName;
/**
* #var string
*
* #ORM\Column(name="currency_symbol", type="string", length=5, nullable=true)
*/
private $currencySymbol;
/**
* #var string
*
* #ORM\Column(name="name_en", type="string", length=80, nullable=false)
*/
private $nameEn;
/**
* #var string
*
* #ORM\Column(name="name_tr", type="string", length=80, nullable=false)
*/
private $nameTr;
/**
* #var integer
*
* #ORM\Column(name="numcode", type="smallint", nullable=true)
*/
private $numcode;
/**
* #var integer
*
* #ORM\Column(name="phonecode", type="integer", nullable=false)
*/
private $phonecode;
/**
* #var float
*
* #ORM\Column(name="latitude", type="float", precision=18, scale=10, nullable=false)
*/
private $latitude;
/**
* #var float
*
* #ORM\Column(name="longitude", type="float", precision=18, scale=10, nullable=false)
*/
private $longitude;
/**
* #var string
*
* #ORM\Column(name="slug_en", type="string", length=50, nullable=false)
*/
private $slugEn;
/**
* #var string
*
* #ORM\Column(name="slug_tr", type="string", length=50, nullable=false)
*/
private $slugTr;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
/**
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
private $updatedAt;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set iso
*
* #param string $iso
* #return Country
*/
public function setIso($iso)
{
$this->iso = $iso;
return $this;
}
/**
* Get iso
*
* #return string
*/
public function getIso()
{
return $this->iso;
}
/**
* Set iso3
*
* #param string $iso3
* #return Country
*/
public function setIso3($iso3)
{
$this->iso3 = $iso3;
return $this;
}
/**
* Get iso3
*
* #return string
*/
public function getIso3()
{
return $this->iso3;
}
/**
* Set currencyCode
*
* #param string $currencyCode
* #return Country
*/
public function setCurrencyCode($currencyCode)
{
$this->currencyCode = $currencyCode;
return $this;
}
/**
* Get currencyCode
*
* #return string
*/
public function getCurrencyCode()
{
return $this->currencyCode;
}
/**
* Set currencyName
*
* #param string $currencyName
* #return Country
*/
public function setCurrencyName($currencyName)
{
$this->currencyName = $currencyName;
return $this;
}
/**
* Get currencyName
*
* #return string
*/
public function getCurrencyName()
{
return $this->currencyName;
}
/**
* Set currencySymbol
*
* #param string $currencySymbol
* #return Country
*/
public function setCurrencySymbol($currencySymbol)
{
$this->currencySymbol = $currencySymbol;
return $this;
}
/**
* Get currencySymbol
*
* #return string
*/
public function getCurrencySymbol()
{
return $this->currencySymbol;
}
/**
* Set nameEn
*
* #param string $nameEn
* #return Country
*/
public function setNameEn($nameEn)
{
$this->nameEn = $nameEn;
return $this;
}
/**
* Get nameEn
*
* #return string
*/
public function getNameEn()
{
return $this->nameEn;
}
/**
* Set nameTr
*
* #param string $nameTr
* #return Country
*/
public function setNameTr($nameTr)
{
$this->nameTr = $nameTr;
return $this;
}
/**
* Get nameTr
*
* #return string
*/
public function getNameTr()
{
return $this->nameTr;
}
/**
* Set numcode
*
* #param integer $numcode
* #return Country
*/
public function setNumcode($numcode)
{
$this->numcode = $numcode;
return $this;
}
/**
* Get numcode
*
* #return integer
*/
public function getNumcode()
{
return $this->numcode;
}
/**
* Set phonecode
*
* #param integer $phonecode
* #return Country
*/
public function setPhonecode($phonecode)
{
$this->phonecode = $phonecode;
return $this;
}
/**
* Get phonecode
*
* #return integer
*/
public function getPhonecode()
{
return $this->phonecode;
}
/**
* Set latitude
*
* #param float $latitude
* #return Country
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* #return float
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* #param float $longitude
* #return Country
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* #return float
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* Set slugEn
*
* #param string $slugEn
* #return Country
*/
public function setSlugEn($slugEn)
{
$this->slugEn = $slugEn;
return $this;
}
/**
* Get slugEn
*
* #return string
*/
public function getSlugEn()
{
return $this->slugEn;
}
/**
* Set slugTr
*
* #param string $slugTr
* #return Country
*/
public function setSlugTr($slugTr)
{
$this->slugTr = $slugTr;
return $this;
}
/**
* Get slugTr
*
* #return string
*/
public function getSlugTr()
{
return $this->slugTr;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Country
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
* #return Country
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
*
* #ORM\PrePersist
* #ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setUpdatedAt(new \DateTime('now'));
if ($this->getCreatedAt() == null) {
$this->setCreatedAt(new \DateTime('now'));
}
}
}

Doctrine Scheme update. Overlooking entities

I have created 2 new entities and when I run
app/console doctrine:schema:update --force
It says i am up to date and these two new entities do not get tables created.
Every other command i have found will detect entities in other bundles but none from the bundle i am working on. Example would be
app/console doctrine:mapping:info
It does not show there either.
Here is a example of one. This lives in the Entity Folder of my bundle
<?php
namespace test\OrganizationBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* Department
* #ORM\Entity
* #ORM\Table(name="departments")
*/
class Department
{
/**
* #var integer
*
* #ORM\Column(name="id", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=50, nullable=false)
*/
private $name;
/**
* #var integer
* #ORM\Column(name="facilityId", type="int")
*
*/
private $facilityId;
/**
* #var \DateTime
* #ORM\Column(name="created", type="datetime")
*/
private $created;
/**
* #var \DateTime
* #ORM\Column(name="updated", type="datetime")
*/
private $updated;
/**
* #var array
* #ORM\ManyToMany(targetEntity="DeptTag", mappedBy="departments")
*/
private $deptTags;
public function __construct()
{
$this->deptTags = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Department
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set facilityId
*
* #param string $facilityId
*
* #return Department
*/
public function setFacilityId($facilityId)
{
$this->facilityId = $facilityId;
return $this;
}
/**
* Get facilityId
*
* #return string
*/
public function getFacilityId()
{
return $this->facilityId;
}
/**
* Set created
*
* #param \DateTime $created
*
* #return Department
*/
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 Department
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* #return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set deptTags
*
* #param array $deptTags
*
* #return Department
*/
public function setDeptTags($deptTags)
{
$this->deptTags = $deptTags;
return $this;
}
/**
* Get deptTags
*
* #return array
*/
public function getDeptTags(DeptTag)
{
return $this->deptTags[] = $deptTag;
}
}

Symfony, Cannot retrieve record from table

In Symfony I cannot retrieve a record from a table using a find(), but I can using createQuery()? This is happening randomly on my tables in my project. The data just seem to become unaccessable using symfony find(), findBy() etc, but I can use dql???
Why is this happening? Has anyone ever had this happen? I cannot figure this out. Thanks for helping!
Test I've Ran: I've created a similar table using the exact same entity fields and imported the data into the table and it works absolutely fine. Why has this table just stopped responding to Symfony's request?
This Works
$dql = "SELECT co FROM WIC\CommonBundle\Entity\CustomOptions co WHERE co.account=:account_id AND co.option_field=:value";
$query = $em->createQuery($dql);
$query->setParameters(array(
'value' => 'reorder_reason',
));
$customOptionValue = $query->getResult();
echo count($customOptionValue); // equals 3
This DOES NOT Work - Exact same variables are passed in
$customOptionValue = $em->getRepository('WICCommonBundle:CustomOptions')->findBy(
array(
"option_field"=>"reorder_reason",
)
);
echo count($customOptionValue); // equals 0
Here is my CustomOptions entity:
namespace WIC\CommonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* CustomOptions
*
* #ORM\Table(uniqueConstraints={#ORM\UniqueConstraint(name="accountFieldValueOptions", columns={"account_id", "option_value", "option_field"})})
* #ORM\Entity(repositoryClass="WIC\CommonBundle\Entity\CommonRepository")
* #Gedmo\Loggable
* #Gedmo\SoftDeleteable(fieldName="deletedAt")
* #ORM\HasLifecycleCallbacks
*/
class CustomOptions
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $name
*
* #Gedmo\Versioned
* #ORM\Column(name="name", type="string", length=255, unique=false, nullable=true)
*/
private $name;
/**
* #var string $option_value
*
* #Gedmo\Versioned
* #ORM\Column(name="option_value", type="string", length=255)
* #Assert\NotBlank(message="Option Value Should Not Be Blank")
*/
private $option_value;
/**
* #var string $option_field
*
* #Gedmo\Versioned
* #ORM\Column(name="option_field", type="string", length=255, unique=false, nullable=true)
*/
private $option_field;
/**
* #ORM\ManyToOne(targetEntity="WIC\AccountBundle\Entity\Account", fetch="EAGER")
* #ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false)
* #Gedmo\Versioned
*/
protected $account;
/**
* #var datetime $created
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(type="datetime", nullable=true)
*/
private $created;
/**
* #var datetime $updated
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(type="datetime", nullable=true)
*/
private $updated;
/**
* #ORM\Column(name="deletedAt", type="datetime", nullable=true)
*/
private $deletedAt;
public function __construct()
{
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set account
*
* #param \WIC\AccountBundle\Entity\Account $account
* #return InventoryLocation
*/
public function setAccount(\WIC\AccountBundle\Entity\Account $account = null)
{
$this->account = $account;
return $this;
}
/**
* Get account
*
* #return \WIC\AccountBundle\Entity\Account
*/
public function getAccount()
{
return $this->account;
}
/**
* Set created
*
* #param \DateTime $created
* #return CustomOptions
*/
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 CustomOptions
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* #return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set deletedAt
*
* #param \DateTime $deletedAt
* #return CustomOptions
*/
public function setDeletedAt($deletedAt)
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* Get deletedAt
*
* #return \DateTime
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
/**
* Get option_value
*
* #return string
*/
public function getOptionValue()
{
return $this->option_value;
}
/**
* Set option_value
*
* #param string $option_value
* #return CustomOptions
*/
public function setOptionValue($option_value)
{
$this->option_value = $option_value;
}
/**
* Get option_field
*
* #return string
*/
public function getOptionField()
{
return $this->option_field;
}
/**
* Set option_field
*
* #param string $option_field
* #return CustomOptions
*/
public function setOptionField($option_field)
{
$this->option_field = $option_field;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set name
*
* #param string $name
* #return CustomOptions
*/
public function setName($name)
{
$this->name = $name;
}
Try using the ID instead of the entire object
I.e.
$customOptionValue = $em->getRepository('WICCommonBundle:CustomOptions')->findBy(array(
"account"=>$account->getId(),
"option_field"=>"reorder_reason",
));
Edit: it's your failure to follow the coding standards expected by symfony that caused this issue for you:
Use camelCase, not underscores, for variable, function and method names
private $option_field should become private $optionField and you should adjust any functions you created to reflect this. Then your findBy array will use "optionField"=>"reorder_reason"
I see that you are using soft #Gedmo\SoftDeleteable. Can you please check if the record that exists and can not be retreived does not have deletedAt set?
Doctrine queries are ignoring record, where deletedAt is set. But lets say "not doctrine queries" would still be able to find these records.

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 - 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