I'm new of Symfony, I made a web-application with it, in witch a User is in relation ManyToMany with an Entity "Mission". It doesn't work, because I get the error
FatalErrorException: Error: Call to undefined method
Acme\ManagementBundle\Entity\UserGroundStation::getMission()
Te getMission is defined in the MissionEntity, and called in the construct. Do I have to define it again? I don't understand.
My User is:
<?php
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\HasLifecycleCallbacks()
* #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(){
parent::__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;
}
}
And the Mission Entity is:
/**
* #ORM\Entity
*/
class Mission {
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
* #var integer
*/
protected $id;
/**
* #ORM\Column(type="string", length=60)
* #var String
*/
protected $name;
/**
* #ORM\Column(type="string", length=600)
* #var String
*/
protected $description;
/**
* #ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\User", inversedBy="users")
*/
protected $users;
public function __construct(){
$this -> users = new ArrayCollection();
}
public function setUsers(Collection $users){
$this -> users = $users;
return $this;
}
public function addUser($user){
$this -> users -> add($user);
return $this;
}
public function getUsers(){
return $this -> users;
}
//setters and getters for id, name and description
}
How to solve this? Thank you
Either you have a typo:
getMission
instead of:
getMissions
Or you surely do not have that method declared in Mission entity, also the class you are calling the method from is not on the listing. Ensure it inherits from User.
Related
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?
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.
I'm trying to set up my User entity to use roles, and following the documentation at http://symfony.com/doc/current/cookbook/security/entity_provider.html
Users work fine, and if I hardcode the $roles value everything works as expected, log in/out is good, etc. But, if I try and retrieve the roles through a many-to-many relationship as outlined in the documentation I get back null.
I should also mention that after creating the entities when I ran 'php app/console doctrine:schema:update --force' it created the role table, but not the 'user_role' table as it said it would. I went ahead and created it manually and entered a row for the user I was testing with, but that was my first clue something wasn't working. It's really frustrating because I followed the documentation and it looks like it should work.
The error I get back while trying to log-in is:
FatalErrorException: Error: Call to a member function toArray() on a non-object
Which points to return $this->roles->toArray() in the user entity.
My User Entity (the relevant bits):
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="ACME\MyBundle\Entity\UserRepository")
*
*/
class User implements UserInterface, \Serializable
{
...
/**
* #ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;
...
/**
* Constructor
*/
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getRoles()
{
return $this->roles->toArray();
}
...
}
My Role Entity:
use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table(name="role")
* #ORM\Entity()
*/
class Role implements RoleInterface
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id()
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="name", type="string", length=30)
*/
private $name;
/**
* #ORM\Column(name="role", type="string", length=20, unique=true)
*/
private $role;
/**
* #ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* #see RoleInterface
*/
public function getRole()
{
return $this->role;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Role
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set role
*
* #param string $role
* #return Role
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
}
Does anybody see a problem in my code or have experience with this same issue? I'm stuck at the moment.
In your entity Role you have
* #ORM\Table(name="role")
change it to
* #ORM\Table(name="user_role")
because your table name is user_role not role
I had the same probleme and i removed the toArray methode
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="ACME\MyBundle\Entity\UserRepository")
*
*/
class User implements UserInterface, \Serializable
{
...
/**
* #ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;
...
/**
* Constructor
*/
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getRoles()
{
return $this->roles;//return $this->roles->toArray();
}
...
}
i am using ZF2 with doctrine i am getting this error.
The target-entity Entity\User cannot be found in 'Subject\Entity\Subject#user'.
Here is the snippet to my code.
<?php
namespace Subject\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/**
* #ORM\Entity
* #ORM\Table(name="subject")
* #property string $subjectname
* #property int $user_id
* #property int $id
*/
class Subject implements InputFilterAwareInterface {
protected $inputFilter;
/**
* #ORM\Id
* #ORM\Column(type="integer");
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $subjectname;
/**
* #ORM\ManyToOne(targetEntity="Entity\User", inversedBy="subjects")
* #var User|null
*/
private $user;
/** #return User|null */
public function getUser() {
return $this->user;
}
/** #param User $user */
public function setUser(User $user) {
if($user === null || $user instanceof User) {
$this->user = $user;
} else {
throw new InvalidArgumentException('$user must be instance of Entity\User or null!');
}
}}
and then my "User" entity
namespace Subject\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/*
* #ORM\Entity
* #ORM\Table(name="users")
* #property string $username
* #property string $password
* #property int $id
*/
class User implements InputFilterAwareInterface {
protected $_username;
protected $_password;
/**
* #ORM\OneToMany(targetEntity="Entity\Subject", mappedBy="user")
* #var Collection
*/
private $subjects;
/** #ORM\Id() #ORM\Column(type="integer") #ORM\GeneratedValue(strategy="AUTO") #var int */
protected $_id;
public function __get($property) {
return $this->$property;
}
public function __set($property, $value) {
$this->$property = $value;
}
//Getters and setters
/** #return Collection */
public function getSubjects() {
return $this->comments;
}
/** #param Comment $comment */
public function addSubject(Subject $comment) {
$this->comments->add($comment);
$comment->setUser($this);
}
}
Your entity declaration is incorrect:
* #ORM\ManyToOne(targetEntity="Entity\User", inversedBy="subjects")
This should be either this:
* #ORM\ManyToOne(targetEntity="Subject\Entity\User", inversedBy="subjects")
Or, since the two classes share the same namespace, you can also use this:
* #ORM\ManyToOne(targetEntity="User", inversedBy="subjects")
The targetEntity has to be the fully qualified class name (FQCN), except if referring to a class in the same namespace, in which case the short name may be used (as per the last example above).
In our case, the file name was not the same as the class name: it was just a typo.
I had the same problem. The problem in my case was case-sensitivity.
Bad:
class Package {
/**
* #ORM\OneToMany(targetEntity="PackagUSA", mappedBy="package")
*/
private Collection $packageusas;
}
class Packageusa {
/**
* #ORM\ManyToOne(targetEntity="Package", inversedBy="packageusas")
* #ORM\JoinColumn(name="package_id", referencedColumnName="id")
*/
private Package $package;
}
Good:
class Package {
/**
* #ORM\OneToMany(targetEntity="PackageUSA", mappedBy="package")
*/
private Collection $packageusas;
}
class PackageUSA {
/**
* #ORM\ManyToOne(targetEntity="Package", inversedBy="packageusas")
* #ORM\JoinColumn(name="package_id", referencedColumnName="id")
*/
private Package $package;
}
Being new to symfony2 and querybuilder, I am trying to select all my containers but limiting the result to only those for which the logged-in user has access.
With the current code I get this error:
Notice: Undefined index: Container in /var/www/biztv_symfony/vendor/doctrine/lib/Doctrine/ORM/Query/SqlWalker.php line 746
Here is my attempt at the query (I have tried a couple of options from this and other forum sites and still can't seem to understand it right...)
public function indexAction()
{
//Get the requisits
$companyId = $this->container->get('security.context')->getToken()->getUser()->getCompany()->getId();
$em = $this->getDoctrine()->getEntityManager();
//Fetch the containers
$repository = $this->getDoctrine()->getRepository('BizTVContainerManagementBundle:Container');
$query = $repository->createQueryBuilder('c')
->innerJoin('c.users','u')
->where('c.company = :company')
->setParameter('company', $companyId)
->orderBy('c.name', 'ASC')
->getQuery();
$containers = $query->getResult();
I keep track of access with a many-to-many relation, this is my user entity...
<?php
// src/BizTV/UserBundle/Entity/User.php
namespace BizTV\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use BizTV\BackendBundle\Entity\company as company;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var object BizTV\BackendBundle\Entity\company
*
* #ORM\ManyToOne(targetEntity="BizTV\BackendBundle\Entity\company")
* #ORM\JoinColumn(name="company", referencedColumnName="id", nullable=false)
*/
protected $company;
/**
* #ORM\ManyToMany(targetEntity="BizTV\ContainerManagementBundle\Entity\Container", inversedBy="User")
* #ORM\JoinTable(name="access")
*/
private $access;
public function __construct()
{
parent::__construct();
$this->access = new \Doctrine\Common\Collections\ArrayCollection();
}
//getters and setters...
This is my container entity:
<?php
namespace BizTV\ContainerManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use BizTV\UserBundle\Entity\User as user;
/**
* BizTV\ContainerManagementBundle\Entity\Container
*
* #ORM\Table(name="container")
* #ORM\Entity
*/
class Container
{
/**
* #var integer $id
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string $name
* #Assert\NotBlank(message = "Du måste ange ett namn för området")
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
//TODO: form handler assuring no name is used twice in same company
/**
* #var object BizTV\BackendBundle\Entity\company
*
* #ORM\ManyToOne(targetEntity="BizTV\BackendBundle\Entity\company")
* #ORM\JoinColumn(name="company", referencedColumnName="id", nullable=false)
*/
protected $company;
/**
* #ORM\ManyToMany(targetEntity="BizTV\UserBundle\Entity\User", mappedBy="Container")
*/
private $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
In the Container entity, when you specify the $users property mapping, the value of mappedBy is important: you need to specify the name of the inverse property in the User class.
Here, we have, Container::$users <-> User::$access.
Change your annotations to:
class User
{
/**
* #ORM\ManyToMany(targetEntity="BizTV\ContainerManagementBundle\Entity\Container", inversedBy="users")
* #ORM\JoinTable(name="access")
*/
private $access;
}
class Container
{
/**
* #ORM\ManyToMany(targetEntity="BizTV\UserBundle\Entity\User", mappedBy="access")
*/
private $users;
}
I encourage you to read the doctrine orm documentation part about associations, and verify and fix this in all your associations.