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.
Related
I am trying to learn the framework Doctrine 2. I have a Model in MySQL and implement it in Doctrine. After the implementation of the dependency between the two classes Task and Dropdown list, it doesnt operate.
My code is:
<?php
use Doctrine\Common\Collections;
use Doctrine\ORM\Mapping AS ORM;
/**
* Task
*
* #Table(name="task")
* #Entity
*/
class Task
{
/**
* #var integer
*
* #Column(name="ID", type="integer", nullable=false)
* #Id
* #GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var Dropdownlist
* #ORM\OneToMany(targetEntity="Dropdownlist", mappedBy="tasks")
* #ORM\JoinColumn(name="priority", referencedColumnName="id")
*/
protected $priority;
/**
* #var string
*
* #Column(name="Label", type="string", length=45, nullable=true)
*/
private $label;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set priority
*
* #param integer $priority
*
* #return Task
*/
public function setPriority($priority)
{
$this->priority = $priority;
return $this;
}
/**
* Get priority
*
* #return integer
*/
public function getPriority()
{
return $this->priority;
}
/**
* Set label
*
* #param string $label
*
* #return Task
*/
public function setLabel($label)
{
$this->label = $label;
return $this;
}
/**
* Get label
*
* #return string
*/
public function getLabel()
{
return $this->label;
}
}
/**
* Dropdownlist
*
* #Table(name="dropdownlist")
* #Entity
*/
class Dropdownlist
{
/**
* #var integer
*
* #Column(name="ID", type="integer")
* #Id
* #GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #Column(name="PriorityLabel", type="string", length=45, nullable=true)
*/
private $prioritylabel;
/*
* #var ArrayCollection
* #ORM\ManyToOne(targetEntity="Task", inversedBy="priority")
*/
protected $tasks;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set prioritylabel
*
* #param string $prioritylabel
*
* #return Dropdownlist
*/
public function setPrioritylabel($prioritylabel)
{
$this->prioritylabel = $prioritylabel;
return $this;
}
/**
* Get prioritylabel
*
* #return string
*/
public function getPrioritylabel()
{
return $this->prioritylabel;
}
public function getTasts(){
return $this->tasks;
}
public function __construct()
{
}
}
Problem:
The database schema is not in sync with the current mapping file.
What is the reason?
Where is the problem?
Best regards
It means your database is not in sync with your current mapping.
Run doctrine:schema:update --complete --dump-sql to see which changes need to be done. You can also run doctrine:schema:update --complete --force to deploy the changes directly.
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;
}
}
I have a file /src/AppBundle/Entity/Questionnaire.php with 3 Entity classes, where I'm trying to implement Single table inheritance with Doctrine 2 on Symfony 2.7. Questionnaire is a parent abstract class, and there are 2 child classes FirstQuestions and SecondsQuestions that extends Questionnaire. I choosed this model because I need to write data in table in 2 steps. The code of this file is below:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Questionnaire
*
* #ORM\Entity
* #ORM\Table(name="questionnaire")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({"firstquestions" = "FirstQuestions", "secondquestions" = "SecondQuestions"})
*/
abstract class Questionnaire {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
/**
* FirstQuestions
*/
class FirstQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="firstName", type="string", length=64)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="lastName", type="string", length=64)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=32)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="birthday", type="date")
*/
private $birthday;
/**
* #var integer
*
* #ORM\Column(name="shoeSize", type="integer")
*/
private $shoeSize;
/**
* Set firstName
*
* #param string $firstName
*
* #return Questionnaire
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
*
* #return Questionnaire
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* #param string $email
*
* #return Questionnaire
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set birthday
*
* #param \DateTime $birthday
*
* #return Questionnaire
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* #return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Set shoeSize
*
* #param integer $shoeSize
*
* #return Questionnaire
*/
public function setShoeSize($shoeSize)
{
$this->shoeSize = $shoeSize;
return $this;
}
/**
* Get shoeSize
*
* #return integer
*/
public function getShoeSize()
{
return $this->shoeSize;
}
}
/**
* SecondQuestions
*/
class SecondQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="favoriteIceCream", type="string", length=128)
*/
private $favoriteIceCream;
/**
* #var string
*
* #ORM\Column(name="favoriteSuperHero", type="string", length=32)
*/
private $favoriteSuperHero;
/**
* #var string
*
* #ORM\Column(name="favoriteMovieStar", type="string", length=64)
*/
private $favoriteMovieStar;
/**
* #var \DateTime
*
* #ORM\Column(name="endOfTheWorld", type="date")
*/
private $endOfTheWorld;
/**
* #var string
*
* #ORM\Column(name="superBowlWinner", type="string", length=32)
*/
private $superBowlWinner;
/**
* Set favoriteIceCream
*
* #param string $favoriteIceCream
*
* #return Questionnaire
*/
public function setFavoriteIceCream($favoriteIceCream)
{
$this->favoriteIceCream = $favoriteIceCream;
return $this;
}
/**
* Get favoriteIceCream
*
* #return string
*/
public function getFavoriteIceCream()
{
return $this->favoriteIceCream;
}
/**
* Set favoriteSuperHero
*
* #param string $favoriteSuperHero
*
* #return Questionnaire
*/
public function setFavoriteSuperHero($favoriteSuperHero)
{
$this->favoriteSuperHero = $favoriteSuperHero;
return $this;
}
/**
* Get favoriteSuperHero
*
* #return string
*/
public function getFavoriteSuperHero()
{
return $this->favoriteSuperHero;
}
/**
* Set favoriteMovieStar
*
* #param string $favoriteMovieStar
*
* #return Questionnaire
*/
public function setFavoriteMovieStar($favoriteMovieStar)
{
$this->favoriteMovieStar = $favoriteMovieStar;
return $this;
}
/**
* Get favoriteMovieStar
*
* #return string
*/
public function getFavoriteMovieStar()
{
return $this->favoriteMovieStar;
}
/**
* Set endOfTheWorld
*
* #param \DateTime $endOfTheWorld
*
* #return Questionnaire
*/
public function setEndOfTheWorld($endOfTheWorld)
{
$this->endOfTheWorld = $endOfTheWorld;
return $this;
}
/**
* Get endOfTheWorld
*
* #return \DateTime
*/
public function getEndOfTheWorld()
{
return $this->endOfTheWorld;
}
/**
* Set superBowlWinner
*
* #param string $superBowlWinner
*
* #return Questionnaire
*/
public function setSuperBowlWinner($superBowlWinner)
{
$this->superBowlWinner = $superBowlWinner;
return $this;
}
/**
* Get superBowlWinner
*
* #return string
*/
public function getSuperBowlWinner()
{
return $this->superBowlWinner;
}
}
So the problem is when I'm trying to create object of child class(FirstQuestions or SecondsQuestions) in method of controller, Symfony displays me error "500 Internal Server Error". The code of controller with method is below:
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Questionnaire;
use AppBundle\Entity\FirstQuestions;
use AppBundle\Entity\SecondQuestions;
class TestController extends Controller
{
/**
* #Route("/test", name="test")
*/
public function indexAction(Request $request)
{
$item = new FirstQuestions(); // everything works well without this line
return new Response(
'ok'
);
}
}
Maybe I am doing something wrong or didn't set any important annotation. Can anyone help me?
This will be one of those annoying little oversight errors - an extra semi-colon or something somewhere that you're not looking for it. I'm creating this additional answer so that I can give you exactly the code I'm using. Hopefully, you'll be able to cut-and-paste, replace your own files with this new code, and it will magically start working.
First - to prove the point, here's my (modified) output:
Veromo\Bundle\CoreBundle\Entity\FirstQuestions Object
(
[firstName:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[lastName:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[email:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[birthday:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[shoeSize:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[id:Veromo\Bundle\CoreBundle\Entity\Questionnaire:private] =>
)
Which shows that all I'm doing differently to you is using my own dev environment's namespaces.
AppBundle\Entity\Questionnaire.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Questionnaire
*
* #ORM\Entity
* #ORM\Table(name="questionnaire")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({"questionnaire"="Questionnaire", "firstquestions" = "FirstQuestions", "secondquestions" = "SecondQuestions"})
*/
abstract class Questionnaire {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
AppBundle\Entity\FirstQuestions.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* FirstQuestions
* #ORM\Entity()
*/
class FirstQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="firstName", type="string", length=64)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="lastName", type="string", length=64)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=32)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="birthday", type="date")
*/
private $birthday;
/**
* #var integer
*
* #ORM\Column(name="shoeSize", type="integer")
*/
private $shoeSize;
/**
* Set firstName
*
* #param string $firstName
*
* #return Questionnaire
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
*
* #return Questionnaire
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* #param string $email
*
* #return Questionnaire
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set birthday
*
* #param \DateTime $birthday
*
* #return Questionnaire
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* #return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Set shoeSize
*
* #param integer $shoeSize
*
* #return Questionnaire
*/
public function setShoeSize($shoeSize)
{
$this->shoeSize = $shoeSize;
return $this;
}
/**
* Get shoeSize
*
* #return integer
*/
public function getShoeSize()
{
return $this->shoeSize;
}
}
AppBundle\Entity\SecondQuestions.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SecondQuestions
* #ORM\Entity()
*/
class SecondQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="favoriteIceCream", type="string", length=128)
*/
private $favoriteIceCream;
/**
* #var string
*
* #ORM\Column(name="favoriteSuperHero", type="string", length=32)
*/
private $favoriteSuperHero;
/**
* #var string
*
* #ORM\Column(name="favoriteMovieStar", type="string", length=64)
*/
private $favoriteMovieStar;
/**
* #var \DateTime
*
* #ORM\Column(name="endOfTheWorld", type="date")
*/
private $endOfTheWorld;
/**
* #var string
*
* #ORM\Column(name="superBowlWinner", type="string", length=32)
*/
private $superBowlWinner;
/**
* Set favoriteIceCream
*
* #param string $favoriteIceCream
*
* #return Questionnaire
*/
public function setFavoriteIceCream($favoriteIceCream)
{
$this->favoriteIceCream = $favoriteIceCream;
return $this;
}
/**
* Get favoriteIceCream
*
* #return string
*/
public function getFavoriteIceCream()
{
return $this->favoriteIceCream;
}
/**
* Set favoriteSuperHero
*
* #param string $favoriteSuperHero
*
* #return Questionnaire
*/
public function setFavoriteSuperHero($favoriteSuperHero)
{
$this->favoriteSuperHero = $favoriteSuperHero;
return $this;
}
/**
* Get favoriteSuperHero
*
* #return string
*/
public function getFavoriteSuperHero()
{
return $this->favoriteSuperHero;
}
/**
* Set favoriteMovieStar
*
* #param string $favoriteMovieStar
*
* #return Questionnaire
*/
public function setFavoriteMovieStar($favoriteMovieStar)
{
$this->favoriteMovieStar = $favoriteMovieStar;
return $this;
}
/**
* Get favoriteMovieStar
*
* #return string
*/
public function getFavoriteMovieStar()
{
return $this->favoriteMovieStar;
}
/**
* Set endOfTheWorld
*
* #param \DateTime $endOfTheWorld
*
* #return Questionnaire
*/
public function setEndOfTheWorld($endOfTheWorld)
{
$this->endOfTheWorld = $endOfTheWorld;
return $this;
}
/**
* Get endOfTheWorld
*
* #return \DateTime
*/
public function getEndOfTheWorld()
{
return $this->endOfTheWorld;
}
/**
* Set superBowlWinner
*
* #param string $superBowlWinner
*
* #return Questionnaire
*/
public function setSuperBowlWinner($superBowlWinner)
{
$this->superBowlWinner = $superBowlWinner;
return $this;
}
/**
* Get superBowlWinner
*
* #return string
*/
public function getSuperBowlWinner()
{
return $this->superBowlWinner;
}
}
AppBundle\Controller\TestController.php
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Questionnaire;
use AppBundle\Entity\FirstQuestions;
use AppBundle\Entity\SecondQuestions;
class TestController extends Controller
{
/**
* #Route("/test",name="test")
*/
public function indexAction( Request $request )
{
$item = new FirstQuestions();
return new Response(
'<pre>'.print_r( $item, true ).'</pre>'
);
}
}
And just to be sure ...
app\config\routing.yml
test:
resource: "#AppBundle/Controller/TestController.php"
type: annotation
It's GOT to be some stupid, annoying little error that nobody is looking for.
Hope this helps ...
ALL Entity classes which are part of the mapped entity hierarchy need to be specified in the #DiscriminatorMap. So, yes, your annotation is incorrect.
Doctrine Single Table Inheritance
EDIT
You have another annotations error - neither of your subclasses has an #Entity annotation:
/**
* FirstQuestions
* #ORM\Entity()
*/
class FirstQuestions extends Questionnaire {
/**
* SecondQuestions
* #ORM\Entity()
*/
class SecondQuestions extends Questionnaire {
After fixing this I was able to use Doctrine's Schema Update tool to build the tables AND successfully created a FirstQuestions object.
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.
I firstly created the database and then generated the entities from it. After that I generated the getters and setters in the entities using:
php app/console doctrine:generate:entities DigitalManager
Later on I decided to create the association (One to Many) between the entities, so I added the respective commands in my Entities and what I got is this:
Party Entity:
<?php
namespace DigitalManager\Bundle\ERPBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Party
*
* #ORM\Table(name="party")
* #ORM\Entity
*/
class Party
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=45, nullable=true)
*/
private $name;
/**
* #ORM\ManyToOne(targetEntity="Account", inversedBy="parties")
* #ORM\JoinColumn(name="account_id", referencedColumnName="aid")
*/
public $account;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=45, nullable=true)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="phone_fax", type="string", length=45, nullable=true)
*/
private $phoneFax;
/**
* #var string
*
* #ORM\Column(name="fax", type="string", length=45, nullable=true)
*/
private $fax;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=45, nullable=true)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="mobile", type="string", length=45, nullable=true)
*/
private $mobile;
/**
* #var string
*
* #ORM\Column(name="country", type="string", length=45, nullable=true)
*/
private $country;
/**
* #var string
*
* #ORM\Column(name="city", type="string", length=45, nullable=true)
*/
private $city;
/**
* #var string
*
* #ORM\Column(name="phone_res", type="string", length=45, nullable=true)
*/
private $phoneRes;
/**
* #var string
*
* #ORM\Column(name="remarks", type="string", length=45, nullable=true)
*/
private $remarks;
/**
* #var boolean
*
* #ORM\Column(name="active", type="boolean", nullable=true)
*/
private $active;
/**
* #var integer
*
* #ORM\Column(name="limit", type="integer", nullable=true)
*/
private $limit;
/**
* #var integer
*
* #ORM\Column(name="term", type="integer", nullable=true)
*/
private $term;
/**
* #var string
*
* #ORM\Column(name="officer_id", type="string", length=45, nullable=true)
*/
private $officerId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime", nullable=true)
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="cnic", type="string", length=45, nullable=true)
*/
private $cnic;
/**
* #var string
*
* #ORM\Column(name="ntn", type="string", length=45, nullable=true)
*/
private $ntn;
/**
* #var boolean
*
* #ORM\Column(name="L_O", type="boolean", nullable=true)
*/
private $lO;
/**
* #var string
*
* #ORM\Column(name="type", type="string", length=45, nullable=true)
*/
private $type;
/**
* #var integer
*
* #ORM\Column(name="acc_id", type="integer", nullable=true)
*/
private $accId;
/**
* #var integer
*
* #ORM\Column(name="party_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $partyId;
/**
* Set name
*
* #param string $name
* #return Party
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* #param string $address
* #return Party
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set phoneFax
*
* #param string $phoneFax
* #return Party
*/
public function setPhoneFax($phoneFax)
{
$this->phoneFax = $phoneFax;
return $this;
}
/**
* Get phoneFax
*
* #return string
*/
public function getPhoneFax()
{
return $this->phoneFax;
}
/**
* Set fax
*
* #param string $fax
* #return Party
*/
public function setFax($fax)
{
$this->fax = $fax;
return $this;
}
/**
* Get fax
*
* #return string
*/
public function getFax()
{
return $this->fax;
}
/**
* Set email
*
* #param string $email
* #return Party
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set mobile
*
* #param string $mobile
* #return Party
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
/**
* Get mobile
*
* #return string
*/
public function getMobile()
{
return $this->mobile;
}
/**
* Set country
*
* #param string $country
* #return Party
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set city
*
* #param string $city
* #return Party
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set phoneRes
*
* #param string $phoneRes
* #return Party
*/
public function setPhoneRes($phoneRes)
{
$this->phoneRes = $phoneRes;
return $this;
}
/**
* Get phoneRes
*
* #return string
*/
public function getPhoneRes()
{
return $this->phoneRes;
}
/**
* Set remarks
*
* #param string $remarks
* #return Party
*/
public function setRemarks($remarks)
{
$this->remarks = $remarks;
return $this;
}
/**
* Get remarks
*
* #return string
*/
public function getRemarks()
{
return $this->remarks;
}
/**
* Set active
*
* #param boolean $active
* #return Party
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
* Get active
*
* #return boolean
*/
public function getActive()
{
return $this->active;
}
/**
* Set limit
*
* #param integer $limit
* #return Party
*/
public function setLimit($limit)
{
$this->limit = $limit;
return $this;
}
/**
* Get limit
*
* #return integer
*/
public function getLimit()
{
return $this->limit;
}
/**
* Set term
*
* #param integer $term
* #return Party
*/
public function setTerm($term)
{
$this->term = $term;
return $this;
}
/**
* Get term
*
* #return integer
*/
public function getTerm()
{
return $this->term;
}
/**
* Set officerId
*
* #param string $officerId
* #return Party
*/
public function setOfficerId($officerId)
{
$this->officerId = $officerId;
return $this;
}
/**
* Get officerId
*
* #return string
*/
public function getOfficerId()
{
return $this->officerId;
}
/**
* Set date
*
* #param \DateTime $date
* #return Party
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set cnic
*
* #param string $cnic
* #return Party
*/
public function setCnic($cnic)
{
$this->cnic = $cnic;
return $this;
}
/**
* Get cnic
*
* #return string
*/
public function getCnic()
{
return $this->cnic;
}
/**
* Set ntn
*
* #param string $ntn
* #return Party
*/
public function setNtn($ntn)
{
$this->ntn = $ntn;
return $this;
}
/**
* Get ntn
*
* #return string
*/
public function getNtn()
{
return $this->ntn;
}
/**
* Set lO
*
* #param boolean $lO
* #return Party
*/
public function setLO($lO)
{
$this->lO = $lO;
return $this;
}
/**
* Get lO
*
* #return boolean
*/
public function getLO()
{
return $this->lO;
}
/**
* Set type
*
* #param string $type
* #return Party
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return string
*/
public function getType()
{
return $this->type;
}
/**
* Set accId
*
* #param integer $accId
* #return Party
*/
public function setAccId($accId)
{
$this->accId = $accId;
return $this;
}
/**
* Get accId
*
* #return integer
*/
public function getAccId()
{
return $this->accId;
}
/**
* Get partyId
*
* #return integer
*/
public function getPartyId()
{
return $this->partyId;
}
}
Account Entity
<?php
namespace DigitalManager\Bundle\ERPBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Account
*
* #ORM\Table(name="account")
* #ORM\Entity
*/
class Account
{
public function __construct()
{
parent::__construct();
// The line below must be here as a single category is to be mapped to many products, so an ArrayCollection is required instead of Array.
$this->parties = new ArrayCollection();
}
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=45, nullable=true)
*/
private $name;
// The metadata above the $products property of the Category object is less important, and simply tells
// Doctrine to look at the Product.category property to figure out how the relationship is mapped.
/**
* #ORM\OneToMany(targetEntity="Party", mappedBy="account")
*/
protected $parties;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=200, nullable=true)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="level1", type="string", length=45, nullable=true)
*/
private $level1;
/**
* #var string
*
* #ORM\Column(name="level2", type="string", length=45, nullable=true)
*/
private $level2;
/**
* #var string
*
* #ORM\Column(name="level3", type="string", length=45, nullable=true)
*/
private $level3;
/**
* #var string
*
* #ORM\Column(name="contact", type="string", length=45, nullable=true)
*/
private $contact;
/**
* #var integer
*
* #ORM\Column(name="aid", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $aid;
/**
* Set name
*
* #param string $name
* #return Account
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* #param string $address
* #return Account
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set level1
*
* #param string $level1
* #return Account
*/
public function setLevel1($level1)
{
$this->level1 = $level1;
return $this;
}
/**
* Get level1
*
* #return string
*/
public function getLevel1()
{
return $this->level1;
}
/**
* Set level2
*
* #param string $level2
* #return Account
*/
public function setLevel2($level2)
{
$this->level2 = $level2;
return $this;
}
/**
* Get level2
*
* #return string
*/
public function getLevel2()
{
return $this->level2;
}
/**
* Set level3
*
* #param string $level3
* #return Account
*/
public function setLevel3($level3)
{
$this->level3 = $level3;
return $this;
}
/**
* Get level3
*
* #return string
*/
public function getLevel3()
{
return $this->level3;
}
/**
* Set contact
*
* #param string $contact
* #return Account
*/
public function setContact($contact)
{
$this->contact = $contact;
return $this;
}
/**
* Get contact
*
* #return string
*/
public function getContact()
{
return $this->contact;
}
/**
* Get aid
*
* #return integer
*/
public function getAid()
{
return $this->aid;
}
}
After adding these association commands, I tried to generate the getters and setters for these using:
php app/console doctrine:generate:entities DigitalManager
But it doesn't seem to generate the getters and setters for these associated attributes. I have looked up the syntax through the Symfony2 documentation and it was the same for creating associations. Can anyone please tell me what's wrong here, why does it not generate the getters and setters, although the attributes are there?
It may be trying to read the information from somewhere else. Is there an XML file for the configuration of this entity under Bundle/Resources/config/doctrine/?
There are two process to generate an entity and create a database and so on process.
I think you can do this way
1. create a entity using command
php app/console doctrine:generate:entity
you provide shortcut name Like AcmeHelloBundle. After that it will ask you all the filed name of a table. step by step you create the table.
2. If you not create your database then set database configuration file and run this command
php app/console doctrine:database:create
3. After create the database you need to create the table depending on your entity that create in list 2 and run the folloing command like
php app/console doctrine:schema:update --force
4.Now you can create association with other entity. I just show here create entity but you need to create all entity that need for your project. After perfect association you need to run this command
php app/console doctrine:generate:entities DigitalManager
5. Finnaly you need to perform this command again
php app/console doctrine:schema:update --force
This may work fine.