based on the way to do it from FOSUserbundle, i made a vendor bundle that contains a Analytic Abstrakt class than implements an AnalyticInterface and in a src/AppBundle, i(ve got a Analytic entity that extends the vendor Analytic abstract class but that doesn't work, my entity doesn't the abstract class properties ($referer, $host, $ip, $browser, ...)
The interface:
<?php
/**
* Created by PhpStorm.
* User: VanIllaSkyPE
* Date: 01/11/2016
* Time: 23:26
*/
namespace Gkratz\AnalyticBundle\Model;
/**
* Interface AnalyticInterface
* #package Gkratz\AnalyticBundle\Model
*/
interface AnalyticInterface
{
/**
* Set date
*
* #param \DateTime $date
* #return Analytic
*/
public function setDate($date);
/**
* Get date
*
* #return \DateTime
*/
public function getDate();
/**
* Set page
*
* #param string $page
* #return Analytic
*/
public function setPage($page);
/**
* Get page
*
* #return string
*/
public function getPage();
/**
* Set ip
*
* #param string $ip
* #return Analytic
*/
public function setIp($ip);
/**
* Get ip
*
* #return string
*/
public function getIp();
/**
* Set host
*
* #param string $host
* #return Analytic
*/
public function setHost($host);
/**
* Get host
*
* #return string
*/
public function getHost();
/**
* Set browser
*
* #param string $browser
* #return Analytic
*/
public function setBrowser($browser);
/**
* Get browser
*
* #return string
*/
public function getBrowser();
/**
* Set referer
*
* #param string $referer
* #return Analytic
*/
public function setReferer($referer);
/**
* Get referer
*
* #return string
*/
public function getReferer();
/**
* Set newSession
*
* #param boolean $newSession
* #return Analytic
*/
public function setNewSession($newSession);
/**
* Get newSession
*
* #return boolean
*/
public function getNewSession();
}
The base abstract class:
<?php
/**
* Created by PhpStorm.
* User: VanIllaSkyPE
* Date: 01/11/2016
* Time: 22:30
*/
namespace Gkratz\AnalyticBundle\Model;
/**
* Class Analytic
* #package Gkratz\AnalyticBundle\Model
*/
abstract class Analytic implements AnalyticInterface
{
protected $id;
/**
* #var \DateTime
*/
protected $date;
/**
* #var string
*/
protected $page;
/**
* #var string
*/
protected $ip;
/**
* #var string
*/
protected $host;
/**
* #var string
*/
protected $browser;
/**
* #var string
*/
protected $referer;
/**
*
* #var boolean
*/
protected $newSession;
public function __construct()
{
$this->date = new \Datetime();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set date
*
* #param \DateTime $date
* #return Analytic
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set page
*
* #param string $page
* #return Analytic
*/
public function setPage($page)
{
$this->page = $page;
return $this;
}
/**
* Get page
*
* #return string
*/
public function getPage()
{
return $this->page;
}
/**
* Set ip
*
* #param string $ip
* #return Analytic
*/
public function setIp($ip)
{
$this->ip = $ip;
return $this;
}
/**
* Get ip
*
* #return string
*/
public function getIp()
{
return $this->ip;
}
/**
* Set host
*
* #param string $host
* #return Analytic
*/
public function setHost($host)
{
$this->host = $host;
return $this;
}
/**
* Get host
*
* #return string
*/
public function getHost()
{
return $this->host;
}
/**
* Set browser
*
* #param string $browser
* #return Analytic
*/
public function setBrowser($browser)
{
$this->browser = $browser;
return $this;
}
/**
* Get browser
*
* #return string
*/
public function getBrowser()
{
return $this->browser;
}
/**
* Set referer
*
* #param string $referer
* #return Analytic
*/
public function setReferer($referer)
{
$this->referer = $referer;
return $this;
}
/**
* Get referer
*
* #return string
*/
public function getReferer()
{
return $this->referer;
}
/**
* Set newSession
*
* #param boolean $newSession
* #return Analytic
*/
public function setNewSession($newSession)
{
$this->newSession = $newSession;
return $this;
}
/**
* Get newSession
*
* #return boolean
*/
public function getNewSession()
{
return $this->newSession;
}
}
the App analytic entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gkratz\AnalyticBundle\Model\Analytic as BaseAnalytic;
/**
* Analytic
*
* #ORM\Table(name="analytic")
* #ORM\Entity(repositoryClass="AppBundle\Repository\AnalyticRepository")
*/
class Analytic extends BaseAnalytic
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
public function __construct()
{
parent::__construct();
}
}
Thank you for any response
The abstract class Analytic defines only properties.
Analytic needs to annotate them with #ORM\Column(...) if you want to use them as DB columns.
Also see: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html
Related
I am trying to use the Entity Philip inside a Symfony Controller
But this error Showing when i want to load the route:
Error Screenshot
Attempted to load class “PhilipRepositroy ” from namespace
“AppBundle\Repository ” Did you forget a “use” statement for another
namespace?
My Controller
/**
* #Route("/see", name="see")
*/
public function SeeAction()
{
$Philip = $this->getDoctrine()
->getRepository('AppBundle:Philip')
->findAll();
return $this->render('business/see.html.twig', array(
'Philip' => $Philip
));
}
My Philip Entity
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Philip
*/
class Philip
{
/**
* #var int
*/
private $id;
/**
* #var string
*/
private $philipname;
/**
* #var string
*/
private $philipemail;
/**
* #var string
*/
private $philipphone;
/**
* #var string
*/
private $philipregion;
/**
* #var string
*/
private $philipville;
/**
* #var string
*/
private $philipcin;
/**
* #var string
*/
private $philipcv;
/**
* #var string
*/
private $philipgender;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set philipname
*
* #param string $philipname
* #return Philip
*/
public function setPhilipname($philipname)
{
$this->philipname = $philipname;
return $this;
}
/**
* Get philipname
*
* #return string
*/
public function getPhilipname()
{
return $this->philipname;
}
/**
* Set philipemail
*
* #param string $philipemail
* #return Philip
*/
public function setPhilipemail($philipemail)
{
$this->philipemail = $philipemail;
return $this;
}
/**
* Get philipemail
*
* #return string
*/
public function getPhilipemail()
{
return $this->philipemail;
}
/**
* Set philipphone
*
* #param string $philipphone
* #return Philip
*/
public function setPhilipphone($philipphone)
{
$this->philipphone = $philipphone;
return $this;
}
/**
* Get philipphone
*
* #return string
*/
public function getPhilipphone()
{
return $this->philipphone;
}
/**
* Set philipregion
*
* #param string $philipregion
* #return Philip
*/
public function setPhilipregion($philipregion)
{
$this->philipregion = $philipregion;
return $this;
}
/**
* Get philipregion
*
* #return string
*/
public function getPhilipregion()
{
return $this->philipregion;
}
/**
* Set philipville
*
* #param string $philipville
* #return Philip
*/
public function setPhilipville($philipville)
{
$this->philipville = $philipville;
return $this;
}
/**
* Get philipville
*
* #return string
*/
public function getPhilipville()
{
return $this->philipville;
}
/**
* Set philipcin
*
* #param string $philipcin
* #return Philip
*/
public function setPhilipcin($philipcin)
{
$this->philipcin = $philipcin;
return $this;
}
/**
* Get philipcin
*
* #return string
*/
public function getPhilipcin()
{
return $this->philipcin;
}
/**
* Set philipcv
*
* #param string $philipcv
* #return Philip
*/
public function setPhilipcv($philipcv)
{
$this->philipcv = $philipcv;
return $this;
}
/**
* Get philipcv
*
* #return string
*/
public function getPhilipcv()
{
return $this->philipcv;
}
/**
* Set philipgender
*
* #param string $philipgender
* #return Philip
*/
public function setPhilipgender($philipgender)
{
$this->philipgender = $philipgender;
return $this;
}
/**
* Get philipgender
*
* #return string
*/
public function getPhilipgender()
{
return $this->philipgender;
}
}
My PhilipRepository
<?php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* PhilipRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class PhilipRepository extends EntityRepository
{
}
What i can do to resolve this problem? I am new to Symfony
So I guess you failed to get Repository class? Then in your Philip entity class add mappings
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="AppBundle\Repository\PhilipRepository")
*/
class Philip
{
...
}
I have a bit of a problem figuring out the following:
How can I make Symfony insert a new menu when a new coffeeshop has been made with a form? (foreign key in menu is shopid)
Thanks in advance, code below.
Menu Entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Menu
*
* #ORM\Table(name="menu")
* #ORM\Entity(repositoryClass="AppBundle\Repository\MenuRepository")
*
*/
class Menu
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity="Coffeeshop")
* #ORM\JoinColumn(name="coffeeshop_id", referencedColumnName="id")
*/
private $shopId;
/**
* #var \DateTime $updated
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(type="datetime")
*/
private $updated;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set shopId
*
* #param integer $shopId
*
* #return Menu
*/
public function setShopId($shopId)
{
$this->shopId = $shopId;
return $this;
}
/**
* Get shopId
*
* #return int
*/
public function getShopId()
{
return $this->shopId;
}
/**
* Set lastUpdated
*
* #param \DateTime $lastUpdated
*
* #return Menu
*/
public function setLastUpdated($lastUpdated)
{
$this->lastUpdated = $lastUpdated;
return $this;
}
/**
* Get lastUpdated
*
* #return \DateTime
*/
public function getLastUpdated()
{
return $this->lastUpdated;
}
/**
* Set updated
*
* #param \DateTime $updated
*
* #return Menu
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* #return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
}
Coffeeshop Entity:
<?php
/// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="coffeeshop")
*/
class Coffeeshop
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=100)
*/
private $name;
/**
* #ORM\Column(type="string")
*/
private $phone;
/**
* #ORM\Column(type="string", length=50)
*/
private $streetName;
/**
* #ORM\Column(type="string", length=6)
*/
private $houseNumber;
/**
* #ORM\Column(type="string", length=7)
*/
private $zipcode;
/**
* #ORM\Column(type="text")
*/
private $description;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Coffeeshop
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set phone
*
* #param string $phone
*
* #return Coffeeshop
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* #return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set streetName
*
* #param string $streetName
*
* #return Coffeeshop
*/
public function setStreetName($streetName)
{
$this->streetName = $streetName;
return $this;
}
/**
* Get streetName
*
* #return string
*/
public function getStreetName()
{
return $this->streetName;
}
/**
* Set houseNumber
*
* #param string $houseNumber
*
* #return Coffeeshop
*/
public function setHouseNumber($houseNumber)
{
$this->houseNumber = $houseNumber;
return $this;
}
/**
* Get houseNumber
*
* #return string
*/
public function getHouseNumber()
{
return $this->houseNumber;
}
/**
* Set description
*
* #param string $description
*
* #return Coffeeshop
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set zipcode
*
* #param string $zipcode
*
* #return Coffeeshop
*/
public function setZipcode($zipcode)
{
$this->zipcode = $zipcode;
return $this;
}
/**
* Get zipcode
*
* #return string
*/
public function getZipcode()
{
return $this->zipcode;
}
/**
* Set menu
*
* #param \AppBundle\Entity\Menu $menu
*
* #return Coffeeshop
*/
public function setMenu(\AppBundle\Entity\Menu $menu = null)
{
$this->menu = $menu;
return $this;
}
/**
* Get menu
*
* #return \AppBundle\Entity\Menu
*/
public function getMenu()
{
return $this->menu;
}
/**
* Set coffeeshopmenu
*
* #param \AppBundle\Entity\Menu $coffeeshopmenu
*
* #return Coffeeshop
*/
public function setCoffeeshopmenu(\AppBundle\Entity\Menu $coffeeshopmenu = null)
{
$this->coffeeshopmenu = $coffeeshopmenu;
return $this;
}
/**
* Get coffeeshopmenu
*
* #return \AppBundle\Entity\Menu
*/
public function getCoffeeshopmenu()
{
return $this->coffeeshopmenu;
}
}
Coffeeshop FormBuilder:
<?php
/**
* Created by PhpStorm.
* User:
* Date: 23-9-2016
* Time: 14:20
*/
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class CoffeeshopType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('phone')
->add('streetName')
->add('houseNumber')
->add('zipcode')
->add('description')
->add('save', SubmitType::class, array('label' => 'Add shop'))
;
}
}
In many ways:
you can define Coffeeshoptypeas a service, then inject ManagerRegistry (#doctrine) to __construct() (or just EntityManager), set an event listener for event FormEvents::POST_SUBMIT. Something like that:
$this->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {/*...*/});
in controller, where you persist changes from Coffeeshoptype
use an event listener for Doctrine (or create an entity listener, feature from Doctrine). With doctrine events, you can find if entity (Coffeeshop) is persisting or updating and depends of situation, create new menu.
All of above methods can have access to Doctrine (thanks to Dependency Injection), also some of these methods are bad approaches. I suggest to attach EventListener (or EventSubscriber) to one of Doctrine Events and then do persisting for new menu. But if you need to create a new menu only when Coffeeshop is submitted by form, create event listener in form type.
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'm really new to Symfony and I'm learning how it works.
So I have a DB and a table on it called USER with the fields name,username,password etc etc.
I'm trying to create a login form that will take the user from that table.
I've already created my enity User
I've read the doc here, but it's not what I want, because as i said my entity it's still there.
Should I use FOS user bunble? I've read the doc here of this bundle, but I didn't get the final part (step7).So my question is, can I use my User Entity with FOS user Bundle? If the answer is no, there is another solution to that, sicne I don't what to change my DB table?
Thank everyone in advance
P.s I generated the User entity following this dochere and this is the resulting class
namespace LocalWorker\BackendBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Utente
*/
class Utente
{
/**
* #var string
*/
private $uNome;
/**
* #var string
*/
private $uCognome;
/**
* #var string
*/
private $uUsername;
/**
* #var string
*/
private $uPassword;
/**
* #var string
*/
private $uEmail;
/**
* #var string
*/
private $uRuolo;
/**
* #var integer
*/
private $uStatus;
/**
* #var \DateTime
*/
private $uDateIns;
/**
* #var \DateTime
*/
private $uDateActive;
/**
* #var integer
*/
private $uId;
/**
* Set uNome
*
* #param string $uNome
* #return Utente
*/
public function setUNome($uNome)
{
$this->uNome = $uNome;
return $this;
}
/**
* Get uNome
*
* #return string
*/
public function getUNome()
{
return $this->uNome;
}
/**
* Set uCognome
*
* #param string $uCognome
* #return Utente
*/
public function setUCognome($uCognome)
{
$this->uCognome = $uCognome;
return $this;
}
/**
* Get uCognome
*
* #return string
*/
public function getUCognome()
{
return $this->uCognome;
}
/**
* Set uUsername
*
* #param string $uUsername
* #return Utente
*/
public function setUUsername($uUsername)
{
$this->uUsername = $uUsername;
return $this;
}
/**
* Get uUsername
*
* #return string
*/
public function getUUsername()
{
return $this->uUsername;
}
/**
* Set uPassword
*
* #param string $uPassword
* #return Utente
*/
public function setUPassword($uPassword)
{
$this->uPassword = $uPassword;
return $this;
}
/**
* Get uPassword
*
* #return string
*/
public function getUPassword()
{
return $this->uPassword;
}
/**
* Set uEmail
*
* #param string $uEmail
* #return Utente
*/
public function setUEmail($uEmail)
{
$this->uEmail = $uEmail;
return $this;
}
/**
* Get uEmail
*
* #return string
*/
public function getUEmail()
{
return $this->uEmail;
}
/**
* Set uRuolo
*
* #param string $uRuolo
* #return Utente
*/
public function setURuolo($uRuolo)
{
$this->uRuolo = $uRuolo;
return $this;
}
/**
* Get uRuolo
*
* #return string
*/
public function getURuolo()
{
return $this->uRuolo;
}
/**
* Set uStatus
*
* #param integer $uStatus
* #return Utente
*/
public function setUStatus($uStatus)
{
$this->uStatus = $uStatus;
return $this;
}
/**
* Get uStatus
*
* #return integer
*/
public function getUStatus()
{
return $this->uStatus;
}
/**
* Set uDateIns
*
* #param \DateTime $uDateIns
* #return Utente
*/
public function setUDateIns($uDateIns)
{
$this->uDateIns = $uDateIns;
return $this;
}
/**
* Get uDateIns
*
* #return \DateTime
*/
public function getUDateIns()
{
return $this->uDateIns;
}
/**
* Set uDateActive
*
* #param \DateTime $uDateActive
* #return Utente
*/
public function setUDateActive($uDateActive)
{
$this->uDateActive = $uDateActive;
return $this;
}
/**
* Get uDateActive
*
* #return \DateTime
*/
public function getUDateActive()
{
return $this->uDateActive;
}
/**
* Get uId
*
* #return integer
*/
public function getUId()
{
return $this->uId;
}
}
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.