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.
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 overriding validation messages in my Entity.
But when i got errors, the validation messages displayed twice:
First is message from FOSUserBundle(Login is short) and second is my message(Min 6). Why?
<?php
namespace Acme\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #Assert\Length(min=5, max=16,
* minMessage="Min 6",
* maxMessage="Max 16",
* )
*/
protected $username;
//..
}
You need to create own group of validators:
http://symfony.com/doc/current/book/validation.html#validation-groups
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 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")