I have two bundles with two entity’s. I need to create a manyToMany relationship between these entity’s.
Property:
namespace Pfwd\AdminBundle\PropertyBundle\Entity;
use Pfwd\UserBundle\Entity\User as User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="property")
*/
class Property {
//...
/**
* #ORM\ManyToMany(targetEntity="User")
* #ORM\JoinTable(name="user_role",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="property_id", referencedColumnName="id")}
* )
*
* #var ArrayCollection $salesAgents
*/
protected $salesAgents;
//..
User:
namespace Pfwd\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User implements UserInterface
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*
* #var integer $id
*/
protected $id;
// ...
The property bundle depends on the user bundle.
When I run
php app/console doctrine:schema:update --force
I get the following error:
[ErrorException]
Warning: class_parents(): Class Pfwd\AdminBundle\PropertyBundle\Entity\User
does not exist and could not be loaded in <SITE_PATH>/symfony2/vendor/doctrine/lib/Doctrine/ORM/Mapping/Cl
assMetadataFactory.php line 223
Any ideas?
According to your Property class definition, you have defined a ManyToMany relation with User. As you omitted the namespace, the current one will be used:
User will be translated to Pfwd\AdminBundle\PropertyBundle\Entity\User
You have to specify the FQCN:
#ORM\ManyToMany(targetEntity="Pfwd\UserBundle\Entity\User")
Related
Im having a bit of problems.
I have 2 classes:
Carorder:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Orderdetail;
/**
* #ORM\Entity
* #ORM\Table(name="carorder")
*/
class Carorder
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\OneToMany(targetEntity="Orderdetail", mappedBy="Carorder", cascade={"persist","remove"})
**/
protected $orderdetails;
//Then all the auto genereted setters and getters beneath here
Orderdetail:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/** #ORM\Entity
* #ORM\Table(name="orderdetail")
*/
class Orderdetail
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="Carorder", inversedBy="orderdetails")
**/
protected $carorder;
/**
* #ORM\Column(type="integer")
*/
protected $amount;
//Then all the auto generated setters and getters beneath here
I can't access the orderdetail through the Carorder. For example this example, just thorws the
Undefined index: Carorder
Example:
$repository = $this->getDoctrine()->getRepository('AppBundle:Carorder');
$orders = $repository->findAll();
$orderdetail = $orders[0]->getOrderdetails()->first();
I have no idea what is causing this, so i hoped you guys could help me out.
You mapped the property Carorder but your property name is carorder, it's case sensitive.
A correct mapping could be:
/**
* #ORM\OneToMany(targetEntity="Orderdetail", mappedBy="carorder", cascade={"persist","remove"})
**/
protected $orderdetails;
I have this User entity extends from BaseUser:
namespace PDOneBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use PDOneBundle\Classes\Form\Validator\Constraints as GdcaAssert;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
* #ORM\Table(name="fos_user")
* #ORM\Entity(repositoryClass="PDOneBundle\Entity\Repository\UserRepository")
* #Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*/
class User extends BaseUser
{
/*
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var int
*
* #Assert\NotBlank()
*
* #ORM\ManyToOne(targetEntity="Company", inversedBy="users", cascade={"persist"})
* #ORM\JoinColumn(name="company_id", referencedColumnName="ID")
*/
protected $company;
/**
* #ORM\ManyToMany(targetEntity="Company", mappedBy="admins", cascade={"persist"})
*/
protected $companies;
/**
* #ORM\ManyToMany(targetEntity="Project", mappedBy="admins", cascade={"persist"})
*/
protected $projects;
// methods goes here
}
I am using also EasyAdminBundle and any time I try to add a new user from EasyAdmin I got the following error:
Neither the property "expiresAt" nor one of the methods
"getExpiresAt()", "expiresAt()", "isExpiresAt()", "hasExpiresAt()",
"__get()" exist and have public access in class
"PDOneBundle\Entity\User".
Why? It is not supposed that those methods are extended from BaseUser? Why the error?
It doesn't look like there is a getter for property expiresAt : https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Model/User.php
I guess you could easily add your own getter in your extended User class if EasyAdminBundle requires it.
class User extends BaseUser
{
// ...
public function getExpiresAt() {
return $this->expiresAt;
}
// ...
}
Looks like you could also define which properties EasyAdminBundle controls : https://github.com/javiereguiluz/EasyAdminBundle/blob/v1.2.1/Resources/doc/11-configuration-reference.md#advanced-configuration-with-custom-field-configuration
I'm trying to implement PUGXMultiUserBundle in a Sylius install.
After following bundle's instructions I'm able to fill the form for register userOne but all I get is:
'Entity class 'UserOne' used in the discriminator map of class 'UserBundle\Entity\User' does not exist.
As explained in bundle's documentation, my User.php:
namespace UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* #ORM\Entity
* #ORM\Table(name="sylius_user")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"userOne" = "UserOne", "userTwo" = "UserTwo"})
*/
abstract class User extends BaseUser {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
And my userOne.php class:
namespace UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
/**
* #ORM\Entity
* #ORM\Table(name="userOne")
* #UniqueEntity(fields = "username", targetClass ="UserBundle\Entity\User", message="fos_user.username.already_used")
* #UniqueEntity(fields = "email", targetClass ="UserBundle\Entity\User", message="fos_user.email.already_used")
*/
class UserOne extends User {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
I would appreciate any feedback. I can't generate the entities and/or complete the registration due to the same error.
Any help would be really appreciated.
i try make onetoone relation from example - http://docs.doctrine-project.org/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity
this is second try , first is here symfony 2 doctrine relation onetoone
Adres
<?php
/**
* Created by PhpStorm.
* User: grek
* Date: 18.12.13
* Time: 16:33
*/
namespace Miejsce\ObiektyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Adres {
/**
* #var integer
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=64, nullable=true)
*/
public $street;
/**
* #ORM\OneToOne(targetEntity="Miejsce\ObiektyBundle\Entity\User") */
private $user;
}
User
<?php
/**
* Created by PhpStorm.
* User: grek
* Date: 18.12.13
* Time: 16:33
*/
namespace Miejsce\ObiektyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class User {
/**
* #var integer
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=64, nullable=true)
*/
public $name;
/**
* #ORM\OneToOne(targetEntity="Miejsce\ObiektyBundle\Entity\Adres")
*/
private $adres;
}
and have : php app/console doctrine:schema:update --force
php app/console doctrine:schema:update --force
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "#Doctrine\ORM\Mapping" in property Miejsce\ObiektyBundle\Entity\Adres::$user does not e
xist, or could not be auto-loaded.
So where i can have error ?
error was in syntax - i have #ORM/OneToOne but need #ORM\OneToOne now work fine ! :)
The answer is in the error message. Look at what is different between your two classes.
/**
* #Entity
*/
Vs:
/**
* #ORM\Entity
*/
So update the one that is giving you the error.
edit
When you import Doctrine's annotations with use Doctrine\ORM\Mapping as ORM;, you'll need to start all those annotations with #ORM\. The annotation-reader will know that #ORM\Entity will actually mean #Doctrine\ORM\Mapping\Entity, which is the class that defines that annotation.
I'm not sure if I understand exactly what you are asking, but your annotations are not setup correctly.
/**
* #Entity
should become
/**
* #ORM\Entity
And
/**
* #Id #Column...
should become
/**
* #ORM\Id
* #ORM\Column...
Same for #OneToOne should be #ORM\OneToOne
Basically you are not prefixing your annotations correctly. You have use Doctrine\ORM\Mapping as ORM; but you aren't using it properly. Prefix your annotations and that will get you going.
I can't figure out how i can have an entity relationship between 2 bundles.
The closest i've been to succeed so far is this:
User Entity:
<?php
namespace Acme\AuthBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\generatedValue(strategy="AUTO")
*/
protected $id;
//...
/**
* #ORM\OneToMany(targetEntity="Acme\NewsBundle\Entity\Article", mappedBy="author")
*/
protected $articles;
//...
Article Entity:
<?php
namespace Acme\NewsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="Acme\NewsBundle\Entity\ArticleRepository")
* #ORM\Table(name="articles")
*/
class Article
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
//...
/**
* #ORM\ManyToOne(targetEntity="Acme\AuthBundle\Entity\User", inversedBy="articles")
*/
protected $author;
//...
When generating entities and updating the database, i get no errors and the relation is correctly set in the DB.
But when I try to fetch the articles i get this:
Class Acme\AuthBundle\Entity\Article does not exist
500 Internal Server Error - ReflectionException
Note that the User entity is in AuthBundle and Article entity in NewsBundle.
Thanks.
You need to specify the name of entity class when defining one-to-many, many-to-one relations. Still you are specifying Acme\AuthBundle\Entity\User not Aief\AuthBundle\Entity\User as targetEntity.
The same for Article and Article repository.