I have a problem with an invalid mapping. I keep getting an error with message:
Missing value for primary key course on "Namespace\XXX\Entity\Subject"
The doctrine:schema:validate command returns the following:
[Mapping] FAIL - The entity-class 'Namespace\XXX\Entity\Subject' mapping is invalid:
* The join columns of the association 'schedule' have to match to ALL identifier columns of the target entity 'Namespace\XXX\Entity\Subject', however 'id, course, class, day, timeslot' are missing.
This is my mapping:
Subject-entity
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="message", type="string", length=255)
*/
private $message;
/**
* #ORM\ManyToOne(targetEntity="Oggi\ScheduleBundle\Entity\Schedule", inversedBy="subjects")
* #ORM\JoinColumn(name="schedule", referencedColumnName="id")
*/
private $schedule;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="date")
*/
private $date;
/**
* #var \DateTime
*
* #ORM\Column(name="created", type="datetime")
*/
private $created;
Schedule Entity
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
*/
private $id;
/**
* #var string
*
* #ORM\Id
* #ORM\OneToOne(targetEntity="Oggi\KlasBundle\Entity\Course")
* #ORM\JoinColumn(name="course", referencedColumnName="id")
*/
private $course;
/**
* #var string
*
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Oggi\KlasBundle\Entity\Klas", inversedBy="schedules")
* #ORM\JoinColumn(name="class", referencedColumnName="id")
*/
private $klas;
/**
* #var string
*
* #ORM\Id
* #ORM\OneToOne(targetEntity="Day")
* #ORM\JoinColumn(name="day", referencedColumnName="id")
*/
private $day;
/**
* #var string
*
* #ORM\Id
* #ORM\OneToOne(targetEntity="Timeslot")
* #ORM\JoinColumn(name="timeslot", referencedColumnName="id")
*/
private $timeslot;
/**
* #ORM\OneToMany(targetEntity="Oggi\CalendarBundle\Entity\Subject", mappedBy="schedule")
*/
private $subjects;
I suppose that what Symfony is telling me to do is include all primary keys in my subject-entity. Is there a way to only include the ID of the schedule in this entity. I had this in mind:
Thanks in advance!
The reason you get this error is because all columns in your Schedule entity are marked as an Id.
Just remove #ORM\Id from all columns but $id - this should fix the case
Related
i'm trying to make a manyToMany relationship with more attributes than the ids, so I need two OneToMany relationships and two ManytoOne relationships having three tables/entities.
I have Product entity, Client entity and ProductClient entity:
class Client
{
/**
* #var integer
*
* #ORM\Column(name="id_client", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idClient;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100, nullable=false)
*/
private $name;
/**
* #var \ProductClient
*
* #ORM\OneToMany(targetEntity="ProductClient", mappedBy="client")
*/
private $products_clients;
}
class Product
{
/**
* #var integer
*
* #ORM\Column(name="id_product", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idProduct;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100, nullable=false)
*/
private $name;
/**
* #var \ProductClient
*
* #ORM\OneToMany(targetEntity="ProductClient", mappedBy="product")
*/
private $products_clients;
}
class ProductClient
{
/**
* #ORM\Column(name="product_id", type="integer")
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Product", inversedBy="products_clients")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id_product", nullable=false)
*/
protected $product;
/**
* #ORM\Column(name="client_id", type="integer")
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Client", inversedBy="products_clients")
* #ORM\JoinColumn(name="client_id", referencedColumnName="id_client", nullable=false)
*/
protected $client;
/**
* #var bool
*
* #ORM\Column(name="status", type="boolean")
*/
private $status;
}
That's something like this (with its getters and setters and more attributes). But symfony launches two "invalid entities errors" when I go to the Product crud:
AppBundle\Entity\Product - The association AppBundle\Entity\Product#products_clients refers to the owning side field AppBundle\Entity\ProductClient#product which is not defined as association, but as field.
AppBundle\Entity\Product - The association AppBundle\Entity\Product#products_clients refers to the owning side field AppBundle\Entity\ProductClient#product which does not exist.
And the same result if I go to the Client crud. What's wrong?
As you can see in the error message, AppBundle\Entity\ProductClient#product is not defined as association, but as field.
Just remove this #ORM\Column(name="product_id", type="integer") and this #ORM\Column(name="client_id", type="integer").
class ProductClient
{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Product", inversedBy="products_clients")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id_product", nullable=false)
*/
protected $product;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Client", inversedBy="products_clients")
* #ORM\JoinColumn(name="client_id", referencedColumnName="id_client", nullable=false)
*/
protected $client;
/**
* #var bool
*
* #ORM\Column(name="status", type="boolean")
*/
private $status;
}
How can I add relationship between user and posts using users id? I saw some examples, but I couldn't follow what inversedBy and mappedBy are for... Is it possible to store profile_id and not the object?
User profile entity:
class Profiles
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=255)
*/
private $password;
Posts/Comments entity:
class Comments
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var Profiles
*
* #ORM\ManyToOne(targetEntity="Profiles")
* #ORM\JoinColumn(name="profile_id", referencedColumnName="id")
*/
private $profile;
OK example
///User Enity
/**
* #ORM\OneToMany(targetEntity="Car", mappedBy="user")
*/
private $cars;
// Car Entity
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="cars")
* #ORM\JoinColumn(name="users_id", referencedColumnName="id")
*/
private $user;
So it sill take the user id and populate this against the Car users_id column in that record
So meaning the a user can be associated to many cars
I have a problem with two entities in Symfony2 with Doctrine:
This is the first Entity:
/**
* Pedidos
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="RestCarta\Bundle\FrontendBundle\Entity\PedidosRepository")
*/
class Pedidos
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="usuario", type="string", length=100)
*/
private $usuario;
/**
* #var string
*
* #ORM\Column(name="mesa", type="string", length=3)
*/
private $mesa;
/**
* #var integer
*
* #ORM\OneToOne(targetEntity="Articulos")
* #ORM\JoinColumn(name="articulo_id", referencedColumnName="id")
*/
private $articulo;
/**
* #var string
*
* #ORM\Column(name="precio", type="decimal")
*/
private $precio;
This is the second Entity:
/**
* Articulos
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="RestCarta\Bundle\FrontendBundle\Entity\ArticulosRepository")
*/
class Articulos
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="referencia", type="string", length=100)
*/
private $referencia;
/**
* #var string
*
* #ORM\Column(name="nombre", type="string", length=255)
*/
private $nombre;
/**
* #var string
*
* #ORM\Column(name="descripcion", type="string", length=255)
*/
private $descripcion;
/**
* #var string
*
* #ORM\Column(name="precio", type="decimal")
*/
private $precio;
/**
* #var string
*
* #ORM\Column(name="imagen", type="string", length=255)
*/
private $imagen;
/**
* #ORM\ManyToOne(targetEntity="Categorias", inversedBy="articulos")
* #ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
*/
protected $categoria;
And now the problem.
How i can persist one "Pedido" with contain one "Articulo" ??
I can read all "Pedido" and the LEFT JOIN with "Articulo" work perfectly, (data inserted manually via phpMyAdmin) but when I persist more data with this code:
$em = $this->getDoctrine()->getManager();
$pedido = new Pedidos();
$pedido->setUsuario('blablabla');
$pedido->setMesa('blablabla');
$pedido->setArticulo($identi);
$pedido->setPrecio('blablabla');
$em->persist($pedido);
$em->flush();
$identi = corresponds to an id of "Articulos"
The result is:
Warning: spl_object_hash() expects parameter 1 to be object, string
given in
/Applications/MAMP/htdocs/RestCarta/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php
line 1388 500 Internal Server Error - ContextErrorException
Please, anyone can help me?
Thanks in Advance
$identi must be a entity object, not only the ID you can use
$em->getReference('YourNamespace\Articulos', $identi);
I have a model which has many tables, but in this case we only need three.
The point is that the composite primary key of one is also the foreing key (composite too) and Symfony throws this exception:
MappingException: It is not possible to map entity 'Your\SomethingBundle\Entity\Empleado' with a composite primary key as part of the primary key of another entity 'Your\SomethingBundle\Entity\EmpleadoHorario#empleado'.
Here I explain the relationship:
1º Salon, it has a primary key ID
2º Empleado, it has a composite primary key ID, Salon_id and, also in the primary key, a foreing key referencing Salon: Salon_id
3º EmpleadoHorario: it has a composite primary key Fecha, Empleado_id, Salon_id and, also in the primary key, two a foreing keys referencing Salon: Salon_id, and Empleado: Empleado_id, Salon_id
All the relations has also the inverse union. Here is the code:
The Salon Entity:
/**
* Salon
*
* #ORM\Table(name="salon")
* #ORM\Entity
*/
class Salon
{
/**
* #var string
*
* #ORM\Column(name="id", type="string", length=50, nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
// More fields...
/**
* #var array_collection
*
* #ORM\OneToMany(targetEntity="Empleado", mappedBy="salon")
*/
private $empleados;
/**
* #var array_collection
*
* #ORM\OneToMany(targetEntity="EmpleadoHorario", mappedBy="salon")
*/
private $empleadoHorarios;
// Getters & Setters...
}
The Empleado Entity:
/**
* Empleado
*
* #ORM\Table(name="empleado")
* #ORM\Entity
*/
class Empleado
{
/**
* #var integer
*
* #ORM\Column(name="id", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #var string
*
* #ORM\JoinColumn(name="salon_id", referencedColumnName="id", nullable=false)
* #ORM\ManyToOne(targetEntity="Salon", inversedBy="empleados")
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $salon;
// More fields...
/**
* #var array_collection
*
* #ORM\OneToMany(targetEntity="EmpleadoHorario", mappedBy="salon")
*/
private $empleadoHorarios;
// Getters & setters...
}
And finally the EmpleadoHorario Entity:
/**
* EmpleadoHorario
*
* #ORM\Table(name="empleado_horario")
* #ORM\Entity
*/
class EmpleadoHorario
{
/**
* #var \DateTime
*
* #ORM\Column(name="fecha", type="date", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $fecha;
/**
* #var string
*
* #ORM\JoinColumn(name="salon_id", referencedColumnName="id", nullable=false)
* #ORM\ManyToOne(targetEntity="Salon", inversedBy="empleadoHorarios")
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $salon;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="Empleado", inversedBy="empleadoHorarios")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="salon_id", referencedColumnName="salon_id", nullable=false),
* #ORM\JoinColumn(name="empleado_id", referencedColumnName="id", nullable=false)
* })
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $empleado;
// More fields...
// Getters & Setters...
}
As I said above, the problem seems to be in the EmpleadoHorario.empleado field, which is part of a composite primary key and also composite foreing key.
Other answers across StackOverflow.com suggest the Mapping Inheritance, but I don't even know how it works. I tried twice after reading this but I couldn't solve my problem.
This sample code is illustration of my (temporary) solution :
<?php
namespace X;
use Doctrine\ORM\Mapping as Orm;
/**
* #Orm\Entity
* #Orm\Table(name="A")
*/
class A {
/**
* #Orm\Id
* #Orm\Column(name="id", type="integer")
* #Orm\GeneratedValue(strategy="NONE")
*
* #var integer
*/
private $id;
/**
* #Orm\Id
*
* #var string
*/
private $otherId;
/**
* #Orm\OneToMany(targetEntity="B", fetch="LAZY", mappedBy="a")
*
* #var array
*/
private $collectionOfB;
// getter, setter and other props/methods
}
/**
* #Orm\Entity
* #Orm\Table(name="B")
*/
class B {
/**
* #Orm\Id
* #Orm\Column(name="code")
*
* #var string
*/
private $code;
/**
* #Orm\Id
* #Orm\Column(name="a_id", type="integer")
*
* #var integer
*/
private $a_id;
/**
* #Orm\Id
* #Orm\Column(name="a_other_id")
*
* #var integer
*/
private $a_other_id;
/**
* #Orm\ManyToOne(targetEntity="A", fetch="LAZY", inversedBy="collectionOfB")
* #Orm\JoinColumns({#Orm\JoinColumn(name="a_id", referencedColumnName="id"), #Orm\JoinColumn(name="a_other_id", referencedColumnName="other_id")})
*
* #var A
*/
private $a;
/**
* #Orm\OneToOne(targetEntity="C", fetch="LAZY", mappedBy="b")
*
* #var C
*/
private $c;
// bla bla bla
}
/**
* #Orm\Entity
* #Orm\Table(name="C")
*/
class C {
/**
* #Orm\Id
* #Orm\Column(name="a_id", type="integer")
*
* #var integer
*/
private $a_id;
/**
* #Orm\Id
* #Orm\Column(name="a_other_id")
*
* #var integer
*/
private $a_other_id;
/**
* #Orm\Id
* #Orm\Column(name="b_code")
*
* #var string
*/
private $b_code;
/**
*
* #Orm\OneToOne(targetEntity="B", fetch="LAZY", inversedBy="c")
* #Orm\JoinColumns({#Orm\JoinColumn(name="a_id", referencedColumnName="a_id"), #Orm\JoinColumn(name="a_other_id", referencedColumnName="a_other_id"), #Orm\JoinColumn(name="b_code", referencedColumnName="b_code")})
*
* #var B
*/
private $b;
// bla bla bla
}
Another also ugly solution is just to give AUTOINCREMENT unique PK for EmpleadoHorario like id for instance and remove #ORM\Id and #ORM\GeneratedValue(strategy="NONE") notations from EmpleadoHorario. So finally it will look like:
/**
* EmpleadoHorario
*
* #ORM\Table(name="empleado_horario")
* #ORM\Entity
*/
class EmpleadoHorario
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="fecha", type="date", nullable=false)
*/
private $fecha;
/**
* #var string
*
* #ORM\JoinColumn(name="salon_id", referencedColumnName="id", nullable=false)
* #ORM\ManyToOne(targetEntity="Salon", inversedBy="empleadoHorarios")
*/
private $salon;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="Empleado", inversedBy="empleadoHorarios")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="salon_id", referencedColumnName="salon_id", nullable=false),
* #ORM\JoinColumn(name="empleado_id", referencedColumnName="id", nullable=false)
* })
*/
private $empleado;
// More fields...
// Getters & Setters...
}
I have a Entity of TeamMembers. And the TeamMember can have a Specification with a value.
So I have three Entities: TeamMember, Specifications, SpecificationValues.
In the SpecificationValue table I want to store the TeamMember_id, the Specification_id and the value that is just for that TeamMember.
The Specifications and TeamMembers Entities are working. But now I want to show all the Specifications, if I go to the edit route (see code example) of a TeamMember, and have to possibility over there to fill in some values that I want to store in the SpecificationValue Entity.
[TeamMember > Specifications]: list of all specifications, with a extra input field where I can insert some values, that will be stored in the SpecificationValues entity.
<?php
namespace My\BundleName\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SpecificationValue
*
* #ORM\Table()
* #ORM\Entity
*/
class SpecificationValue
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="Specifications")
* #ORM\JoinColumn(name="specification_id", referencedColumnName="id")
*/
protected $specification;
/**
* #ORM\ManyToOne(targetEntity="TeamMembers")
* #ORM\JoinColumn(name="teammember_id", referencedColumnName="id")
*/
protected $teammember;
/**
* #var string
* #ORM\Column(name="value", type="string", length=222)
*/
protected $value;
}
/**
* Specifications
*
* #ORM\Table()
* #ORM\Entity
*/
class Specifications
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
* #Gedmo\Translatable
*/
protected $name;
/**
* #ORM\ManyToOne(targetEntity="SpecificationCategory")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
}
/**
* TeamMembers
*
* #ORM\Table()
* #ORM\Entity
*/
class TeamMembers
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=250)
*/
protected $name;
/**
* #var boolean
*
* #ORM\Column(name="active", type="boolean")
*/
protected $active = true;
}
And the Forms are generated with generate:crud.
this is how the form should look like > http://i.stack.imgur.com/Nkkdy.png
But is that even possible with Entities in Symfony?