I am trying to create a symfony2 application. The main idea behind the project is that there is an event which many guests are invited to and they are categorized. I have created a relational model for all the entities.
There are 4 tables:
Guests - who is invited
Category - what category/categories he belongs to ?
Event - the event which they are invited to
Guest_Event (attendance)
I have concluded to the following schemas:
xxxxBundle\Entity\Guest:
type: entity
table: guest
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 100
nullable: false
surname:
type: string
length: 100
nullable: false
email:
type: string
length: 255
nullable: true
address:
type: string
length: 255
nullable: true
phone:
type: string
length: 10
description:
type: text
created_at:
type: datetime
updated_at:
type: datetime
nullable: true
token:
type: string
length: 255
unique: true
is_activated:
type: boolean
nullable: true
manyToOne:
category:
targetEntity: Category
inversedBy: guest
joinColumn:
name: category_id
referencedColumnName: id
lifecycleCallbacks:
prePersist: [ setCreatedAtValue ]
preUpdate: [ setUpdatedAtValue ]
Category
xxxxBundle\Entity\Category:
type: entity
table: category
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
unique: true
oneToMany:
guests:
targetEntity: Guest
mappedBy: category
attend:
targetEntity: Attendance
mappedBy: category
Event
xxxxxBundle\Entity\Event:
type: entity
table: event
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 100
nullable: false
location:
type: string
length: 255
nullable: true
scheduled_at:
type: datetime
manyToMany:
category:
targetEntity: guest
inversedBy: event
joinColumn:
name: event_id
referencedColumnName: id
A guest might belong to multiple categories (manyToOne)
A category will have many guests (manyToOne)
A guest might attend many events (manyToOne)
An event might have many attendants (manyToMany?)
the attendance table (guest_event) should be a join table ?
I am a little bit confused about ORM and doctrine coding. Creating the tables via SQL code or phpmyadmin seems much easier to me but I want to go the hard way ! The documentation seems confusing because each tutorial suggests different things and the doctrine ORM section in the symfony2 book doesn't have a complete example but pieces of code..
How can I correct my tables to include all the specifications ?
My two cents:
A guest might belong to multiple categories
So many guests can belong to many categories, so it's many to many guest side. Assuming the owning side is Guest:
xxxxBundle\Entity\Guest:
manyToMany:
categories:
targetEntity: Category
inversedBy: guests
joinTable:
name: guests_categories
A category will have many guests (manyToOne)
If category will have many guests, why is's many to one? Many categories can be assigned to many guest:
xxxxBundle\Entity\Category:
manyToMany:
guests:
targetEntity: Guest
mappedBy: categories
If i understand you correctly, a categoy may exist even without a guest, and vice-versa a guest may exist even without a category.
And for guest/events relation, i'll go again for many to many / many to many. Take a look at here and ask yourself: one/many type of my entity can have one/many type of another entity?
I would strongly suggest you work your way through the official Symfony 2 documentation:
http://symfony.com/doc/master/book/doctrine.html
It takes you step by step through the process of initializing the database, making an entity, mapping it to the database and then using the entity.
You are trying to understand multiple concepts by reading assorted documentation. The fact that you seem to have yaml files in your entity directory indicates a basic lack of understanding.
Go through the tutorial first then add in your real entities. It's pretty straight forward once you get a few things working.
I changed my mind and used annotations as yaml seems confusing to me.
So far :
For the Guest entity
<?php
// xxxxBundle/Entity/Guest.php
namespace xxxxBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="guest")
*/
class Guest
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ManyToMany(targetEntity="Category", inversedBy="guests")
* #JoinTable(name="guests_categories")
*/
protected $categories;
/**
* #ManyToMany(targetEntity="Event", inversedBy="events")
* #JoinTable(name="guests_events")
*/
protected $events;
/**
* #ORM\Column(type="string", length=30)
*/
protected $name;
/**
* #ORM\Column(type="string", length=30)
*/
protected $surname;
/**
* #ORM\Column(type="string", length=30)
*/
protected $email;
/**
* #ORM\Column(type="string", length=100)
*/
protected $address;
/**
* #ORM\Column(type="string", length=10)
*/
protected $phone;
/**
* #ORM\Column(type="string", length=10)
*/
protected $mobile;
/**
* #ORM\Column(type="text")
*/
protected $description;
/**
* #ORM\Column(type="datetime")
*/
protected $created_at;
/**
* #ORM\Column(type="datetime")
*/
protected $updated_at;
/**
* #ORM\Column(type="string")
*/
protected $token;
/**
* #ORM\Column(type="boolean")
*/
protected $is_activated;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new NotBlank());
$metadata->addPropertyConstraint('surname', new NotBlank());
$metadata->addPropertyConstraint('email', new Email(array(
'message' => 'I do not like invalid emails. Give me a real one!')));
$metadata->addPropertyConstraint('phone', new MaxLength(10));
$metadata->addPropertyConstraint('mobile', new MaxLength(10));
}
}
For the Category entity
<?php
// xxxxBundle/Entity/Category.php
namespace xxxxBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="category")
*/
class Category
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ManyToMany(targetEntity="Guest", mappedBy="categories")
*/
protected $guests;
/**
* #ORM\Column(type="string", length=30)
*/
protected $name;
/**
* #ORM\Column(type="text")
*/
protected $description;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new NotBlank());
}
}
For the Event entity
<?php
// xxxxBundle/Entity/Event.php
namespace xxxxBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="event")
*/
class Event
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ManyToMany(targetEntity="Guest", mappedBy="categories")
*/
protected $guests;
/**
* #ORM\Column(type="string", length=30)
*/
protected $name;
/**
* #ORM\Column(type="string", length=100)
*/
protected $location;
/**
* #ORM\Column(type="text")
*/
protected $description;
/**
* #ORM\Column(type="datetime")
*/
protected $scheduled_at;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new NotBlank());
$metadata->addPropertyConstraint('location', new NotBlank());
}
}
I feel confused about the Attendance entity. The Attendance entity will have the following variables:
Guest_id
Event_id
Will_attend (yes/no/maybe)
Comment
Replied_at
Updated_at
I do not know which will be the primary key (or the primary keys?). The db table does not need to have a separate Id (or not?).
<?php
// xxxxBundle/Entity/Attendance.php
namespace xxxxBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="attendance")
*/
class Attendance
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ManyToMany(targetEntity="Guest", mappedBy="categories")
*/
protected $guests;
/**
* #ManyToMany(targetEntity="Event", mappedBy="events")
*/
protected $events;
/**
* #ORM\Column(type="string", length=3)
*/
protected $will_attend;
/**
* #ORM\Column(type="text")
*/
protected $description;
/**
* #ORM\Column(type="datetime")
*/
protected $replied_at;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
}
The attendance entity requires a person to declare while he/she will attend an event or not. That means that it's a oneToOne relation because for every event must be ONE event - ONE person - ONE attendance reply.
The solution is to change the following code in the attendance entity :
/**
* #ORM\OneToOne(targetEntity="Guest")
*/
protected $guests;
/**
* #ORM\OneToOne(targetEntity="Event")
*/
protected $events;
Then run php app/console doctrine:generate:entities , php app/console doctrine:schema:update --force and the crud command If you generate them automatically. Now everything works fine.
Related
I use FOSRestBundle with JMSSerializerBundle in my web service.
Here is a function in my action controller that returns a list of roles. I don't know how to return a "parent_id" field in this list.
<?php
use FOS\RestBundle\Controller\FOSRestController;
class RoleController extends FOSRestController
{
...
/**
* List all roles.
*
* #Annotations\View()
*
* #return array
*/
public function getRolesAction()
{
$roles = $this->repository->findRoles();
$view = $this->view($roles, 200);
return $this->handleView($view);
}
...
}
Here is my role entity. It has properties: id, name and parent. Parent is a role.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Role
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\RoleRepository")
*/
class Role
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Role")
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
// + getters and setters
...
}
Here is my serializer config file:
AppBundle\Entity\Role:
exclusion_policy: ALL
properties:
id:
expose: true
type: integer
xml_attribute: true
name:
expose: true
type: string
Now getRolesAction in action controller will output JSON with id and name properties. How to add parent_id property to the serializer output?
You could use something like:
/**
* #Serializer\VirtualProperty
* #Serializer\Type("integer")
* #Serializer\SerializedName("parent_id")
*
* #return integer
*/
public function getParentId()
{
return $this->parent->getId();
}
I can't tell you how to transfer the annotation in your config (probably YAML) format, but you'll be able to do that.
IMPORTANT
This only works for serialization - so don't expect this attribute to be deserialized later!!
Alternative:
If you only want to achieve the result you were asking for you could add this to your attribute:
/**
* #Accessor(getter="getParentId",setter="setParent") */
* #Serializer\Type("integer")
* #Serializer\SerializedName("parent_id")
*/
private $parent;
and additionally another getter:
/**
*
* #return integer
*/
public function getParentId()
{
return $this->parent->getId();
}
With the accessor you tell JMS from which getter to get the result to serialize while still being able to use the setter for deserialization.
I'm using Symfony 2 with Sonata Admin Bundle.
As far as i can see, Sonata Admin has only AND filtering on list action.
Use this as example:
My entity : Prodotto
/**
* Prodotto
*
* #ORM\Table()
* #ORM\Entity
* #UniqueEntity("prodotto")
*/
class Prodotto extends DefaultEntity
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
//getters - setters Zone
/**
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #var string
*
* #ORM\Column(name="prodotto", type="string", length=255)
*/
private $prodotto;
/**
* #var \mybundle\Entity\Tipologia
* #ORM\ManyToOne(targetEntity="Tipologia")
*/
private $tipologia;
/**
* #var \mybundle\Entity\Brand
* #ORM\JoinColumn(name="brand_id", referencedColumnName="id")
* #ORM\ManyToOne(targetEntity="Brand")
*
*/
private $brand;
/**
* #var \mybundle\Entity\Layout
* #ORM\ManyToOne(targetEntity="Layout")
* #ORM\JoinColumn(name="layout_id", referencedColumnName="id")
*/
private $layout;
/**
* #var \mybundle\Entity\Carta
* #ORM\ManyToOne(targetEntity="Carta")
* #ORM\JoinColumn(name="carta_id", referencedColumnName="id")
*/
private $carta;
//[... many other fields omitted...]
//[... Getters and setters omitted (default get/set, no custom code) ...]
My admin class: ProdottoAdmin (please note that i copied only the configuration useful for this question, the class contains all the required configurations for all the actions)
/**
* #param DatagridMapper $datagridMapper
*/
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('id')
->add('prodotto')
->add('tipologia')
->add('brand')
->add('layout')
->add('misura')
;
}
/**
* #param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('dataModifica','datetime',array('pattern' => 'dd/MM/yyyy','label'=>'Data'))
->add('tipologia')
->add('brand')
->add('layout')
->add('misura')
->add('Nome Prodotto', 'string', array('template' => 'mybundle:Admin:List/link_column_list_prodotto.html.twig'))
->add('_action', 'actions', array(
'actions' => array(
'show' => array(),
'edit' => array(),
'delete' => array(),
)
))
;
}
My Service Configuration (in services.yml):
services:
mybundle.admin.prodotto:
class: mybundle\Admin\ProdottoAdmin
arguments: [~, mybundle\Entity\Prodotto, mybundle:ProdottoAdmin]
tags:
- {name: sonata.admin, manager_type: orm, group: Prodotti, label: Prodotti}
With this configuration, i actually got a fully functional data grid filter, as you can see from the picture(image added for better understanding of the problem):
But, the default Sonata Admin filtering expose only an AND filter, i can add ONE time all the property of the entity and make an AND filtering with them.
But, now i need to extend that functionality.
I need to add an OR filter, app-wide. I want that the user can filter like "give me all the product that are related with Brand1 OR Brand2 AND are of Tipologia1."
I know i can make precise custom query, like the example above, to obtain a single result, but:
That's will not be app-wide, i have many entities and i can't write all the custom query needed
That's verbose, i will have to write much of the same code in all the data grid filters
That's not wise, because if tomorrow i change an entity, the data grid filter is coupled with the entity and i need to remember to add/modify the query.
So, finally, my question is:
There is a "correct" (or at least, a raccomended) way or pattern / maybe a configurable bundle to implement that OR filtering?
I have a problem related to Doctrine2:
1- I have two tables joining on a many-to-one relation:
Table 1 - Activity
The Schema:
Backend\adminBundle\Entity\Activity:
type: entity
table: activity
indexes:
result_id:
columns:
- result_id
id:
id:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
generator:
strategy: IDENTITY
fields:
......
manyToOne:
result:
targetEntity: Actionresult
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
result_id:
referencedColumnName: id
orphanRemoval: false
The Entity
<?php
namespace Backend\adminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Activity {
/**
* #var \Backend\adminBundle\Entity\Actionresult
*
* #ORM\ManyToOne(targetEntity="Backend\adminBundle\Entity\Actionresult")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="result_id", referencedColumnName="id")
* })
*/
private $result;
/**
* #var \Backend\adminBundle\Entity\SfGuardUser
*
* #ORM\ManyToOne(targetEntity="Backend\adminBundle\Entity\SfGuardUser")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
/* There are other Properties */
/**
* Set result
*
* #param \Backend\adminBundle\Entity\Actionresult $result
* #return Activity
*/
public function setResult(\Backend\adminBundle\Entity\Actionresult $result = null)
{
$this->result = $result;
return $this;
}
/**
* Get result
*
* #return \Backend\adminBundle\Entity\Actionresult
*/
public function getResult()
{
return $this->result;
}
}
Table 2 - Actionresult Related to Activity Table by Id:
The schema:
Backend\adminBundle\Entity\Actionresult:
type: entity
table: actionresult
id:
id:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
generator:
strategy: IDENTITY
fields:
name:
type: string
nullable: false
length: 255
fixed: false
comment: ''
lifecycleCallbacks: { }
The Entity:
<?php
namespace Backend\adminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Actionresult
*
* #ORM\Table(name="actionresult")
* #ORM\Entity
*/
class Actionresult
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Actionresult
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
The Question:
With doctrine i can refer from table Activity to Actionresult with the name result.
How can i refer with doctrine from table Actionresult to Activity??
Thank you in advance.
To be thorough, you should try and stick to one type of entity mapping in Symfony whenever possible. The #ORM* annotations are redundant if you use YAML config, and vice-versa. I'll provide the answer using YAML, and I believe you'll be able to convert to annotations if need be.
# Activity.yml
Activity:
type: entity
...
manyToOne:
result:
targetEntity: ActionResult
inversedBy: activities
# ActionResult.yml
Result:
type: entity
oneToMany:
activities:
targetEntity: Activity
mappedBy: result
# ActionResult.php
class Result {
protected $activities;
public function __construct()
{
$this->activities = new Doctrine\Common\Collections\ArrayCollection();
}
public function getActivities()
{
return $this->activities;
}
public function addActivity(Activity $activity)
{
$activity->setResult($this);
$this->activities->add($activity);
}
public function removeActivity(Activity $activity)
{
$activity->setResult(null);
$this->activities->removeElement($activity);
}
}
# Activity.php
class Activity {
protected $result;
public function getResult()
{
return $this->result;
}
public function setResult(ActionResult $result = null)
{
$this->result = $result;
}
}
Reference:
Bidirectional one to many: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-bidirectional
I have the following two classes which are have a ManyToMany relationship and saved in a joining table called countries_involved.
class CountriesInvolved
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $description;
/**
* #var \DateTime
*/
private $createdAt;
/**
* #var \DateTime
*/
private $updatedAt;
/**
* #var \ACME\SouthBundle\Entity\Country
*/
private $country;
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $involvement;
}
and
class Involvement
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $name;
/**
* #var string
*/
private $description;
}
The relationship is defined as below in YML
manyToMany:
involvement:
targetEntity: Involvement
joinTable:
name: countries_involvement
joinColumns:
case_country_involved_id:
referencedColumnName: id
inverseJoinColumns:
involvement_id:
referencedColumnName: id
I'm trying to return results of countries involved based on the id of an involvement but kind of stuck in writing the query without getting an error. Here's what I tried thus far:
$em = $this->getDoctrine()->getManager()->createQueryBuilder();
$q = $em->select('c')
->from('ACMESouthBundle:CountriesInvolved','c')
->innerJOIN('c.Involvement','i')
->where('i.id = 1')
->groupBy('c.country')->getQuery();
The error is:
[Semantical Error] line 0, col 80 near 'i WHERE i.id': Error: Class ACME\SouthBundle\Entity\CountriesInvolved has no association named Involvement
Firstly, I would recommend the use of annotations, your code will be more readable.
The problem I think is that you have forgotten inversedBy and mappedBy properties.
The following code is a possible solution to your problem using annotations.
You should add this code to Involvement entity:
/**
* #ORM\ManyToMany(targetEntity="CountriesInvolved", inversedBy="involvement")
* #ORM\JoinTable(name="countries_involvement",
* joinColumns={#ORM\JoinColumn(name="case_country_involved_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="involvement_id", referencedColumnName="id")}
* )
*/
private $countries;
and in CountriesInvolved entity you should add the following annotations in $involvement:
/**
* #ORM\ManyToMany(targetEntity="Involvement", mappedBy="countries")
*/
private $involvement;
I have just rewrite the query, something like this:
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT c
FROM ACMESouthBundle:CountriesInvolved c
JOIN c.involvement i
WHERE i.id = :id
');
$query->setParameter('id', '1');
return $query->getResult();
EDIT
This is with YAML method:
CountriesInvolved:
manyToMany:
involvement:
targetEntity: Involvement
mappedBy: countries
CountriesInvolved:
manyToMany:
countries:
targetEntity: CountriesInvolved
inversedBy: involvement
joinTable:
name: countries_involvement
joinColumns:
case_country_involved_id:
referencedColumnName: id
inverseJoinColumns:
involvement_id:
referencedColumnName: id
I'm trying to apply Doctrine to an existing database that has a OneToMany relationship between two tables: Commerce and Area.
I generated the yml schema from the database resulting in the following:
Area:
type: entity
table: area
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
name:
type: string
length: 255
fixed: false
nullable: true
lifecycleCallbacks: { }
Commerce:
type: entity
table: commerce
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
name:
type: string
length: 255
fixed: false
nullable: true
manyToOne:
area:
targetEntity: Area
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
area_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
From that schema I generated the Entities:
use Doctrine\ORM\Mapping as ORM;
/**
* Model\EntitiesBundle\Entity\Area
*
* #ORM\Table(name="area")
* #ORM\Entity
*/
class Area
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $name
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* #return string
*/
public function getName()
{
return $this->name;
}
}
use Doctrine\ORM\Mapping as ORM;
/**
* Model\EntitiesBundle\Entity\Commerce
*
* #ORM\Table(name="commerce")
* #ORM\Entity
*/
class Commerce
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $name
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* #var Area
*
* #ORM\ManyToOne(targetEntity="Area")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="area_id", referencedColumnName="id")
* })
*/
private $area;
/**
* #return \Model\EntitiesBundle\Entity\Area
*/
public function getArea()
{
return $this->area;
}
/**
* #return string
*/
public function getName()
{
return $this->name;
}
}
My problem comes when I try:
$commerce = $em->getRepository('ModelEntitiesBundle:Commerces')
->find($id);
echo $commerce->getArea()->getName();
The Area entity has empty attributes.
Any ideas? Thank you!
I solved the problem. It was somewhere else, I have two Entity Managers and the one I needed was not the default.