The target-entity "some entity" cannot be found - php

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;
}

Related

Doctrine entities reference each other's primary key

I have an Account entity which contains a collection of Group entities. In addition, each Account must have a default Group. How can one persist the entities? When attempting to do so, $manager->flush(); complains with a Not null violation for the entity that was persisted first.
Account:
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Bridge\Doctrine\IdGenerator\UuidV4Generator;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
*/
class Account
{
/**
* #ORM\Id
* #ORM\Column(type="uuid", unique=true)
* #ORM\GeneratedValue(strategy="CUSTOM")
* #ORM\CustomIdGenerator(class=UuidV4Generator::class)
*/
private $id;
/**
* #ORM\OneToOne(targetEntity=Group::class, cascade={"persist", "remove"})
* #ORM\JoinColumn(nullable=false, unique=true)
*/
private $defaultGroup;
/**
* #ORM\OneToMany(targetEntity=Group::class, mappedBy="account")
*/
private $groups;
public function __construct()
{
$this->groups = new ArrayCollection();
$this->defaultGroup = new Group();
}
// Typical getters and setters
}
Group:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
*/
class Group
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity=Account::class, inversedBy="groups")
* #ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $account;
// Typical getters and setters
}
Your approach is not possible because Group::$account is already mapped to the one-to-many Account::$groups property. Doctrine does not know what to do when you persist a Group via Account::$defaultGroup, as this is not the property Group::$account is inversing.
You will have to enforce this Account default group invariant yourself programmatically. There's multiple approaches to this, but I suggest using the existing Account::$groups mapping in combination with the Symfony Collection validation constraint. Validating that an Account should have at least 1 Group assigned to it, which will be the default one.
Here's an example:
Because the entity name Group was giving me syntax problems in the SQL dialect I was using I renamed it to Team.
The Account implementation:
<?php declare(strict_types = 1);
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
*/
class Account
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* #ORM\OneToMany(
* targetEntity="App\Entity\Team",
* mappedBy="account",
* cascade={"persist", "remove"},
* )
* #Assert\Count(
* min=1
* )
*/
private Collection $teams;
public function __construct()
{
$this->teams = new ArrayCollection();
$this->addTeam(new Team());
}
public function getId(): ?int
{
return $this->id;
}
public function getDefaultTeam(): ?Team
{
$team = $this->teams->first();
return $team instanceof Team ? $team : null;
}
public function getTeams(): Collection
{
return $this->teams;
}
public function setTeams(array $teams): void
{
$this->teams->clear();
foreach ($teams as $team) {
if ($team instanceof Team) {
$this->addTeam($team);
}
}
}
public function addTeam(Team $team): void
{
if (false === $this->teams->contains($team)) {
$this->teams->add($team);
$team->setAccount($this);
}
}
public function removeTeam(Team $team): void
{
if (true === $this->teams->contains($team)) {
$this->teams->removeElement($team);
}
}
}
The Team implementation:
<?php declare(strict_types = 1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Team
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* #ORM\ManyToOne(
* targetEntity="App\Entity\Account",
* inversedBy="teams",
* )
* #ORM\JoinColumn(
* name="account_id",
* referencedColumnName="id",
* nullable=false
* )
*/
private ?Account $account = null;
public function getId(): ?int
{
return $this->id;
}
public function getAccount(): ?Account
{
return $this->account;
}
public function setAccount(Account $account): void
{
$this->account = $account;
}
}

Interface Fatal Error

So this is my code on my newly created page inside the Entity folder...
use Doctrine\ORM\Mapping as ORM;
use Application\Entity\Categories;
use PerfectWeb\Core\Interfaces\Routable;
use Application\Mapper\Injector;
use PerfectWeb\Core\Traits;
use PerfectWeb\Core\View\Helper\Object;
use PerfectWeb\Core\Utils\Slug;
/**
* #ORM\Entity
*/
class VodCategory extends Categories implements
Entity\Interfaces\Categories, Routable
{
function getRoute($type = Object::ROUTE_TYPE_VIEW)
{
return 'category/categories';
}
function getRouteParams()
{
return
[
Injector::CATEGORY => $this->getID(),
'slug' => Slug::getSlug($this->getSlug()),
];
}
}
And this is my category.php file:
<?php
namespace Application\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use PerfectWeb\Core\Traits;
/**
* Categories
* #ORM\Table(name="categories")
* #ORM\Entity
*/
class Categories
{
use Traits\Entity;
use Traits\User;
use Traits\Name;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", nullable=false, unique=false)
*/
protected $name;
/**
* #ORM\ManyToOne(targetEntity="Categories", cascade={"persist"})
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
* #var integer
*/
protected $parent = null;
/**
* #var \Application\Entity\User
*
* #ORM\ManyToOne(targetEntity="User", inversedBy="categories")
* #ORM\JoinColumn(name="user", referencedColumnName="id", onDelete="CASCADE")
*/
protected $user;
/**
*
* #ORM\OneToMany(targetEntity="Videos\Entity\Video", mappedBy="category", fetch="EXTRA_LAZY"))
*
*/
protected $videos;
/**
* #var string
*
* #ORM\Column(name="entity", type="string", nullable=true, unique=false)
*/
protected $entity;
/**
*
* construct function for array collection
*/
public function __construct()
{
$this->videos = new ArrayCollection();
}
/**
* #return mixed
*/
public function getVideos()
{
return $this->videos;
}
/**
* #param mixed $videos
*/
public function setVideos($videos)
{
$this->videos = $videos;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Get parent
*
* #return integer
*/
public function getParent()
{
return $this->parent;
}
/**
* Set parent
*
* #param integer $parent
* #return Categories
*/
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
public function __toString()
{
return $this->getName();
}
}
the first bit of code gives me an error:
Fatal Error Interface 'Categories\Entity\Interfaces\Categories' not found in /var/www/html/camclients/module/Videos/src/Videos/Entity/VodCategory.php
What am I doing wrong?
You implement the interface Entity\Interfaces\Categories inside your category class but this interface cannot be found.
You should have an interface in that namespace (and folder) or you should point to the correct location (the folder/namespace where your interface is located).
If your interface exists then it is probably a namespace issue like #doydoy44 suggested in the comment. Then make sure that the namespace declaration and file location of your interface are correct.

Symfony Catchable Fatal Error: Object of class AppBundle\Entity\Email could not be converted to string

i'm new in symfony, i created a small DB schema attached:
Email Class:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Email
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\EmailRepository")
*/
class Email extends Service
{
/**
* #var string
*
* #ORM\Column(name="emailAddress", type="string", length=255)
*/
private $emailAddress;
/**
* Set emailAddress
*
* #param string $emailAddress
*
* #return Email
*/
public function setEmailAddress($emailAddress)
{
$this->emailAddress = $emailAddress;
return $this;
}
/**
* Get emailAddress
*
* #return string
*/
public function getEmailAddress()
{
return $this->emailAddress;
}
}
and my service class:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Service
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\ServiceRepository")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"newsletter" = "Newsletter", "email" = "Email", "service" = "Service"})
*
*/
class Service
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="serviceTitle", type="string", length=255)
*/
private $serviceTitle;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set serviceTitle
*
* #param string $serviceTitle
*
* #return Service
*/
public function setServiceTitle($serviceTitle)
{
$this->serviceTitle = $serviceTitle;
return $this;
}
/**
* Get serviceTitle
*
* #return string
*/
public function getServiceTitle()
{
return $this->serviceTitle;
}
/**
* #ORM\ManyToOne(targetEntity="Service", inversedBy="children")
*/
private $parent;
/**
* #ORM\OneToMany(targetEntity="Service", mappedBy="parent")
*/
private $children;
/**
* Constructor
*/
public function __construct()
{
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set parent
*
* #param \AppBundle\Entity\Service $parent
*
* #return Service
*/
public function setParent(\AppBundle\Entity\Service $parent = null)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* #return \AppBundle\Entity\Service
*/
public function getParent()
{
return $this->parent;
}
/**
* Add child
*
* #param \AppBundle\Entity\Service $child
*
* #return Service
*/
public function addChild(\AppBundle\Entity\Service $child)
{
$this->children[] = $child;
return $this;
}
/**
* Remove child
*
* #param \AppBundle\Entity\Service $child
*/
public function removeChild(\AppBundle\Entity\Service $child)
{
$this->children->removeElement($child);
}
/**
* Get children
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
}
then i generate crud for email, it was working perfect, now i actually want to add two fields for service (SERVICE TITLE, and PARENT SERVICE)
what i did, i just opened email type form and added two fields for (service title and Parent service):
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class EmailType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('emailAddress')
->add('serviceTitle')
->add('parent')
;
}
after saving, when i create new email service browser return exception:
Catchable Fatal Error: Object of class AppBundle\Entity\Email could not be converted to string
right now i just add parent ID in text box (input type text) but actually i want to use (choice lise) from which user can set a parent service at the time of creating new service, and that choice list having all previously created services
add a __toString() method to your email class:
public function __toString()
{
return $this->emailAddress;
}

Call to undefined method, ArrayCollection

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.

Symfony2\Doctrine - Retrieving User Roles

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();
}
...
}

Categories