Doctrine 2 - Join data wthout relation - symfony

i have this structure in my basic model:
class Value extends ...
{
/**
* #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=false)
*/
private $name;
...
}
This model cant be extended from my site, but i need to add a self-referencing ManyToMany relation for reading associated data.
So i created this new model:
class ValueGrid extends ...
{
/**
* #var integer $id
*
* #ORM\Id
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var integer $parentValueId
*
* #ORM\Column(name="parentValueID", type="integer", nullable=false)
*/
private $parentValueId;
/**
* #var integer $childValueId
*
* #ORM\Column(name="childValueID", type="integer", nullable=false)
*/
private $childValueId;
...
}
When i read data from "Value", i do it like this:
$builder = $this->getEntityManager()->createQueryBuilder();
$builder->select(array('value'))
->from('Value', 'value');
// Next i need to simulate the relation
$builder->addSelect(['valuegrid']);
$builder->leftJoin('ValueGrid', 'valuegrid', 'WITH', 'value.id = valuegrid.parentValueId');
It gives this output:
{"id":1,"value":"a"},
null,
{"id":2,"value":"b"},
null,
{"id":3,"value":"c"},
{"id":1,"parentValueId":1,"childValueId":2},
{"id":4,"value":"d"},
null,
{"id":5,"value":"e"},
null
But it should be this format:
{"id":1,"value":"a", "valuegrid":null},
{"id":2,"value":"b", "valuegrid":null},
{"id":3,"value":"c", "valuegrid":{"id":1,"parentValueId":1,"childValueId":2}},
{"id":4,"value":"d", "valuegrid":null},
{"id":5,"value":"e", "valuegrid":null},
Anyone has some suggestions?
I really dont know why my approach does not work..
Thansk and Greetings!

Related

Symfony3 - The association refers to the owning side field which does not exist with ManyToMany and fields table

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

Symfony2 Entities: Many-To-One relationship between user and posts

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

How persist one to one entity unidirectionally in Symfony2

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

Doctrine - "Missing value for primary key"

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

Doctrine 2: OneToMany Relation, order by foreign key

I have three entities (Profile, ProfileValue and Value).
A Profile has a one-to-many relation with ProfileValue, which has a many-to-one relation with the Value entity.
Is it possible to get the rleated ProfileValues from the Profile, ordered by the value id?
If I add the orderby annotation for a non-foreign-key like the enabled field in ProfileValue, it works. But for a foreign key, it failed with the message "inrecognized field". Any ideas?
/**
*
* #ORM\Table(name="profile")
* #ORM\Entity
*/
class Profile {
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false, options={"unsigned"=true})
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var array $profileCValues
* #ORM\OneToMany(targetEntity="ABC\XYZBundle\Entity\ProfileValue", mappedBy="profile", cascade={"persist"})
* #ORM\OrderBy({"value" = "ASC"})
*/
private $profileValues;
And here is the ProfileValue entity:
/**
* ABC\XYZBundle\Entity\ProfileValue
*
* #ORM\Table(name="profile_value", indexes={#ORM\Index(columns={"profile_id"}), #ORM\Index(columns={"value_id"}) })
* #ORM\Entity
*/
class ProfileValue {
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false, options={"unsigned"=true})
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var integer $enabled
*
* #ORM\Column(name="enabled", type="boolean", length=1, nullable=true)
*/
private $enabled;
/**
* #var ABC\XYZBundle\Entity\Profile
* #ORM\ManyToOne(targetEntity="ABC\XYZBundle\Entity\Profile", inversedBy="profileValues")
* #ORM\JoinColumn(name="profile_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
private $profile;
/**
* #var ABC\XYZBundle\Entity\Value
* #ORM\ManyToOne(targetEntity="ABC\XYZBundle\Entity\Value")
* #ORM\JoinColumn(name="value_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $value;
}
Had the same problem and solved it by adding a new field with foreign key:
/**
* #var integer $valueId
*
* #ORM\Column(name="value_id", type="integer")
*/
private $valueId;
Then you can order it without problems:
/**
* #var array $profileCValues
* #ORM\OneToMany(targetEntity="ABC\XYZBundle\Entity\ProfileValue", mappedBy="profile", cascade={"persist"})
* #ORM\OrderBy({"valueId" = "ASC"})
*/
private $profileValues;
Does this work?
/**
* #var array $profileCValues
* #ORM\OneToMany(targetEntity="ABC\XYZBundle\Entity\ProfileValue", mappedBy="profile", cascade={"persist"})
* #ORM\OrderBy({"id" = "ASC"})
*/
private $profileValues;
I don't use XML but this works in YML:
oneToMany:
foos:
targetEntity: Company\ProjectBundle\Entity\Foo
mappedBy: bar
orderBy: { 'id': ASC }

Resources