Answer the question
In order to leave comments, you need to log in
How to implement interface at database level (Symfony3, Doctrine ORM, SonataAdminBundle)?
Good afternoon.
I am using Symfony 3, Doctrine ORM, SonataAdminBundle.
A task arose, the solution of which I see the introduction of a certain interface, but so far I have no idea how to properly store the data of such a structure in the database and edit it in the admin panel.
Suppose there are such initial conditions (I apologize for the artificiality of the construction).
There is an entity "Film", which has some of its own properties (we are not interested in them). There is a table for movies in the `film` database.
/**
* @ORM\Table(name="film")
* @ORM\Entity
*/
class Film
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
}
/**
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
}
/**
* @ORM\Table(name="cinema")
* @ORM\Entity
*/
class Cinema
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Town
*
* @ORM\ManyToOne(targetEntity="Town")
* @ORM\JoinColumn(name="town_id", referencedColumnName="id", nullable=false)
*/
private $town;
/**
* @return Town
*/
public function getTown()
{
return $this->town;
}
...
}
/**
* @ORM\Table(name="town")
* @ORM\Entity
*/
class Town
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
}
/**
* @ORM\Table(name="view")
* @ORM\Entity
*/
class View
{
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
*/
private $user;
/**
* @var Film
*
* @ORM\ManyToOne(targetEntity="Film")
* @ORM\JoinColumn(name="film_id", referencedColumnName="id", nullable=false)
*/
private $film;
/**
* @var Cinema
*
* @ORM\ManyToOne(targetEntity="Cinema")
* @ORM\JoinColumn(name="cinema_id", referencedColumnName="id", nullable=false)
*/
private $cinema;
/**
* @return Cinema
*/
public function getCinema()
{
return $this->cinema;
}
...
}
class View
{
...
/**
* @var ViewPlaceInterface
*/
private $viewPlace;
/**
* @return ViewPlaceInterface
*/
public function getViewPlace()
{
return $this->viewPlace;
}
...
}
interface ViewPlaceInterface
{
/**
* @return Town
*/
public function getTown();
}
class Cinema implements ViewPlaceInterface
{
/**
* @return Town
*/
public function getTown()
{
...
}
}
class TV implements ViewPlaceInterface
{
/**
* @return Town
*/
public function getTown()
{
...
}
}
class Computer implements ViewPlaceInterface
{
/**
* @return Town
*/
public function getTown()
{
...
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question