I made a web application with Symfony2. There are 2 kind of user, a UserOperator and a UserGroundStation. Both of them extend User.
USER GROUND STATION
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
* #ORM\Table(name="user_GroundStation")
* #UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User",
message="Username already_used")
* #UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User",
message="Email already_used")
*/
class UserGroundStation extends User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=60)
* #var String
*/
protected $name;
/**
* #var float
*
* #ORM\Column(name="latitude", type="float")
*/
private $latitude;
/**
* #var float
*
* #ORM\Column(name="longitude", type="float")
*/
private $longitude;
/**
* Set latitude
*
* #param string $latitude
* #return UserGroundStation
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* #return string
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* #param float $longitude
* #return UserGroundStation
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* #return float
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* #ORM\PrePersist
*/
public function setCreatedAtValue()
{
$this->addRole('ROLE_GS');
}
}
USER OPERATOR
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
* #ORM\Table(name="user_Operator")
* #UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User",
message="Username already_used")
* #UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User",
message="Email already_used")
*/
class UserOperator extends User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=60)
* #var String
*/
protected $name;
/**
* #ORM\PrePersist
*/
public function setCreatedAtValue()
{
$this->addRole('ROLE_OPERATOR');
}
}
They extend User.
When I call the registration page for UserOperator, it works, when I call registration page for UserGroundStation, Symfony alerts me:
Neither the property "mission" nor one of the methods "getMission()", "isMission()", "hasMission()", "__get()" exist and have public access in class "Acme\ManagementBundle\Entity\UserGroundStation".
How is it possible? Where can I find the error?
USER
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* #ORM\Entity
* #ORM\Table(name="user")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"user_one" = "UserGroundStation", "user_two" = "UserOperator"})
* #ORM\MappedSuperclass()
*
*/
abstract class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\Mission", mappedBy="users")
*/
protected $missions;
public function __construct(){
$this -> missions = new ArrayCollection();
}
public function setMissions(Collection $missions){
$this -> missions = $missions;
return $this;
}
public function addMission($mission){
$this -> missions -> add($mission);
return $this;
}
public function getMissions(){
return $this -> missions;
}
It was really a dummy problem.
They have different FormType:
RegistrationUserOperatorFormType is different to RegistrationUserGroundStationFormType and
ProfileUserGroundStationFormType is different to ProfileUserOperatorFormType.
Related
I have two tables one is entry.php and user.php
I Want to associate $balance of user with the $balance of entry
but when I try php bin/console doctrine:migrations:diff
I get
No changes detected in your mapping information.
this is my user.php
<?php
namespace BankBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Table(name="user")
*
* #ORM\Entity(repositoryClass="BankBundle\Entity\user")
**/
class user
{
/**
* #ORM\Id
* #ORM\Column(name="id",type="integer")
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="entry", mappedBy="balance")
* #var entry[] An ArrayCollection of entry objects.
*/
private $balance;
public function getId()
{
return $this->id;
}
public function getBalance()
{
return $this->balance;
}
}
and my entry.php
<?php
namespace BankBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Table(name="entry")
*
* #ORM\Entity(repositoryClass="BankBundle\Repository\BankRepository")
**/
class entry
{
/**
* #ORM\Id
* #ORM\Column(name="id",type="integer")
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Column(name="amount",type="integer")
*/
private $amount;
/**
* #ORM\Column(name="balance",type="integer")
* #ORM\ManyToOne(targetEntity="user", inversedBy="balance")
*/
private $balance;
/**
* #ORM\Column(name="created_at",type="datetime")
*/
private $created_at;
}
Hi there and thanks ahead to anybody who will try to help.
I'm pretty new to Symfony and doctrine and I cannot figure out, why my ticket entity is stored without the ManyToMany Realtionship.
I tried very hard, did anything I found for some days but finally I need help.
Here we are
Ticket
<pre>
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use AppBundle\Entity\Computer;
/**
* #ORM\Entity
* #ORM\Table(name="tickets")
*/
class Ticket {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="integer")
*/
protected $creator;
/**
* #ORM\Column(type="integer")
*/
protected $owner;
/**
* #ORM\ManyToOne(targetEntity="Category", inversedBy="tickets")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* #ORM\ManyToOne(targetEntity="Type", inversedBy="tickets")
* #ORM\JoinColumn(name="type_id", referencedColumnName="id")
*/
protected $type;
/**
* #ORM\Column(type="string")
*/
protected $subject;
/**
* #ORM\ManyToMany(targetEntity="Computer", inversedBy="tickets", cascade={"persist"})
* #ORM\JoinTable(name="inventory_computer_join",
* joinColumns={#ORM\JoinColumn(name="ticket_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="computer_id", referencedColumnName="id")}
*)
*/
protected $computer;
[...]
/**
* Add computer
*
* #param \AppBundle\Entity\Computer $computer
*
* #return Ticket
*/
public function addComputer(\AppBundle\Entity\Computer $computer)
{
$this->computers[] = $computer;
return $this;
}
/**
* Remove computer
* #param \AppBundle\Entity\Computer $computer
*/
public function removeComputer(\AppBundle\Entity\Computer $computer)
{
$this->computers->removeElement($computer);
}
/**
* Get computers
* #return \Doctrine\Common\Collections\Collection
*/
public function getComputers()
{
return $this->computers;
}
}
</pre>
Computer
<pre>
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use AppBundle\Entity\Ticket;
/**
* #ORM\Entity
* #ORM\Table(name="inventory_computer")
*/
class Computer
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="Ticket", mappedBy="computer")
*/
protected $tickets;
[...]
/**
* Add ticket
*
* #param \AppBundle\Entity\Ticket $ticket
*
* #return Computer
*/
public function addTicket(\AppBundle\Entity\Ticket $ticket)
{
$this->tickets[] = $ticket;
return $this;
}
</pre>
Controller
<pre>
/**
* #Route("admint/createTicket", name="AdminTicketErstellen")
*/
public function AdminCreateTicketAction(Request $request) {
$ticket = new Ticket;
$form = $this->createForm(TicketType::class, $ticket, array(
'method' => 'GET'
));
$form->handleRequest($request);
if ($form->isSubmitted()) {
$em = $this->getDoctrine()->getManager();
$em->persist($ticket);
$em->flush();
return $this->redirectToRoute('measure_servers');
}
return $this->render( 'sysadmin/pages/AdminCreateTicket.html.twig', array(
'ticket' => $form->createView(),
)
);
}
</pre>
If you want storage ManyToMany try it;
First:
Change protected $commputer; to protected $computers at Ticket.php entity.
Because, you using $this->computers at addComputer() or removeComputer() .
Second: * #ORM\ManyToMany(targetEntity="Ticket", mappedBy="computer") to mappedBy="computers" at Computer.php entity.
You can storage ticket with computer, try it at Controller:
$ticket = new Ticket();
$computer = new Computer();
$ticket->setCreator(4);
$ticket->setOwner(3);
$computer->addTicket($ticket);
$ticket->addComputer($computer);
$em = $this->getDoctrine()->getManager();
$em->persist($computer);
$em->persist($ticket);
$em->flush();
I use Knp\DoctrineBehaviors for Translation and StofDoctrineExtensionsBundle for Sluggable. How to make checking the uniqueness of slug by the sluggable and locale? I want to get the slug look like this:
for EN: /contacts
for PT: /pt/contacts
for PT (if duplicate): /pt/contacts-1
for ES: /es/contacts
But now, i have this database filter_node_translation
Entity\FilterNode.php:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* #ORM\Table(name="filter_node")
* #ORM\Entity()
*/
class FilterNode
{
use ORMBehaviors\Translatable\Translatable;
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(name="id", type="integer")
*/
protected $id;
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
}
FilterNodeTranslation:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Table(name="filter_node_translation")
* #ORM\Entity()
*/
class FilterNodeTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* #var string
*
* #ORM\Column(type="string", nullable=true)
*/
protected $sluggable;
/**
* #var string
*
* #Gedmo\Slug(fields={"sluggable"})
* #ORM\Column(type="string", nullable=true)
*/
protected $slug;
/**
* #return string
*/
public function getSluggable()
{
return $this->sluggable;
}
/**
* #param string $sluggable
*/
public function setSluggable($sluggable)
{
$this->sluggable = $sluggable;
}
}
I found solution. Gedmo sluggable have other configuration option "unique_base".
It looks like this:
class FilterNodeTranslation
{
/**
* #var string
*
* #Gedmo\Slug(updatable=true, unique=true, unique_base="locale", fields={"sluggable"})
* #ORM\Column(type="string")
*/
protected $slug;
}
I'm unsure how to get all hotel types that have "at least one" hotel? As the relation is ManyToMany.
So with queryBuilder, I assumed I can do something like:
$qb = $this->em->createQueryBuilder();
$qb->select('t')
->from(HotelType::class, 't', 't.id')
->join('t.hotels', 'm');
But this doesn't seem to work? I'm not sure what I must do. Here are my two entities:
HotelType entity :
use Doctrine\ORM\Mapping as ORM;
/**
* HotelType
*
* #ORM\Table(name="hotel_type")
* #ORM\Entity
*/
class HotelType
{
/**
* #var string
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\ManyToMany(targetEntity="Hotel", cascade={"persist"}, mappedBy="mType")
*/
private $hotels;
}
Hotel entity :
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Hotel
*
* #ORM\Table(name="hotel")
* #ORM\Entity(repositoryClass="Application\Repository\HotelRepository")
*/
class Hotel
{
/**
* #var string
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="HotelType", inversedBy="hotels", cascade={"persist"})
* #ORM\JoinTable(name="hotel_hoteltype",
* joinColumns={#ORM\JoinColumn(name="hotel_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="hoteltype_id", referencedColumnName="id")}
* )
*/
private $mType;
public function __construct()
{
$this->mType = new ArrayCollection();
}
public function getMType()
{
return $this->mType;
}
public function addMType($mtype)
{
if (!$this->mType->contains($mtype) && is_object($mtype)) {
$this->mType->add($mtype);
}
return $this;
}
public function removeMType($mtype)
{
if ($this->mType->contains($mtype) && is_object($mtype)) {
$this->mType->removeElement($mtype);
}
return $this;
}
public function clearMType()
{
$this->mType->clear();
return $this;
}
}
I have a big understanding problem with OneToMany Relationship - i searched a lot but that didnt help, maybe someone can help me with this.
I have a Event Entity with OneToMany Relation
<?php
namespace #\#\#;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Turmforum\BookingBundle\Entity\Contact;
/**
* Event
*
* #ORM\Table(name="events")
* #ORM\Entity
*/
class Event
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="Contact", mappedBy="event")
*/
protected $event_contacts;
/**
*
*/
public function __construct() {
$this->contacts = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function getContacts() {
return $this->contacts;
}
public function addContact(Contact $contact) {
$this->contacts[] = $contact;
return $this;
}
And have a Contact Entity with ManyToOne Relation
<?php
namespace #\#\#;
use Doctrine\ORM\Mapping as ORM;
use Turmforum\BookingBundle\Entity\Event;
/**
* Contact
*
* #ORM\Table(name="contacts")
* #ORM\Entity
*/
class Contact
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Event", inversedBy="event_contacts")
* #ORM\JoinColumn(name="event_id", referencedColumnName="id")
*/
protected $event;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* #return mixed
*/
public function getEvent() {
return $this->event;
}
When i try to save my event details over a form i get the error:
Neither the property "event_id" nor one of the methods "getEventId()",
"eventId()", "isEventId()", "hasEventId()", "__get()" exist and have
public access in class "Turmforum\BookingBundle\Entity\Event".
What am i missing?