Fos elastica get code name in code table - symfony

I am using fosElastica for populate at elasticsearch. I have a two entity, that entities relationship together.
But I couldn't populate and show category name in products index. How can I to do?
Entity/AirportCodes.php
/**
* #var string $AirportCode
*
* #ORM\Column(name="AirportCode", type="string", length=3)
* #ORM\OneToOne(targetEntity="XXXBundle\Entity\AirportCodesTrans")
* #ORM\JoinColumn(name="AirportCode", referencedColumnName="airport_code", nullable=false)
*/
private $airportCode;
Services.yml
example-connect:
properties:
AirportCode: { type: text, analyzer: autocomplete }
AirportSlug: ~
CountryCode: ~
CityCode: ~
persistence:
driver: orm
model: XXXBundle\Entity\AirportCodes
identifier: AirportCode
provider: ~
finder: ~
Entity/AirportCodesTrans
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $AirportCode
*
* #ORM\Column(name="airport_code", type="string", length=3)
*/
private $airportCode;
/**
* #var string $lang
*
* #ORM\Column(name="lang", type="string", length=3)
*/
private $lang;
/**
* #var string $name
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;

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

Doctrine 2 - Join data wthout relation

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!

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