doctrine2 + symfony2 : multiple $images on same entity - symfony

in my database, i got this
table data
----------
id
name
description
image1
image2
image3
key
...
and my entity Media from SonataMediaBundle, i want to put the images in the entity Media Manager of SonataMediaBundle
How in my entity write the properties for images ?
What kind of realtion is ?
thx for your help

This is simply 3 times a ManyToOne relation to (probably) an Image entity.
The entity would look like following:
class Item
{
/**
* #ORM\Id()
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $name;
/**
* #ORM\Column(type="text")
*/
protected $description;
/**
* #ORM\ManyToOne(targetEntity="YourNamespace\Entity\Image")
*/
protected $image1;
/**
* #ORM\ManyToOne(targetEntity="YourNamespace\Entity\Image")
*/
protected $image2;
/**
* #ORM\ManyToOne(targetEntity="YourNamespace\Entity\Image")
*/
protected $image3;
/**
* #ORM\Column(type="string")
*/
protected $key;
// ...
}

Related

SYMFONY, API PLATFORM how to add edit and show links to the serialized object

I'm working with SYMFONY and API PLATFORM to create REST API.
I have a Project Entity as an API Resource :
class Project
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $reference;
/**
* #ORM\Column(type="string", length=255, unique=true)
* #Gedmo\Slug(fields={"reference"})
*/
private $slug;
/**
* #ORM\Column(type="datetime")
* #Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* #ORM\Column(type="datetime")
* #Gedmo\Timestampable(on="update")
*/
private $updatedAt;
/**
* #ORM\ManyToOne(targetEntity=User::class, inversedBy="projects")
* #ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity=Type::class, inversedBy="projects")
* #ORM\JoinColumn(nullable=false)
*/
private $type;
/**
* #ORM\ManyToOne(targetEntity=Status::class, inversedBy="projects")
* #ORM\JoinColumn(nullable=false)
*/
private $status;
With postman i get :
How can i add edit and show route to get a serialized object like this :
"hydra:member": [
{
...
"status": "/api/statuses/6",
"edit": "<a href='link_to_edit'>edit</a>", // add a link to edit
"show": "<a href='link_to_show'>show</a>" // add a link to show
},
knowing that i don't want to add edit and show to the entity properties or mapped them
Thanks for the help
Technically, you already have your edit and show routes (if you didn't customize them) : you only have to make a PUT or GET request to the value of the #id field of each object.
If you want to add an extra property to your entity, that isn't mapped you can do something like this :
/**
* #SerializedName("edit_route")
*
* #Groups({"projects:read"}))
*
* #return string
*/
public function getEditRoute()
{
return 'your_edit_route';
}
I wouldn't return HTML in this kind of field though, especially if your route is anything else than GET, and apps that use you API might not use HTML, so you're better off returning the simplest value and letting them do their thing with it.

Symfony4 extending entity does not add Entity column to database schema

I am developing symfony 4.2 application. I have entity Meal and OrderItem. OrderItem should have all Meal entity properties + few of it's own. The problem is with ManyToOne relationship column. It is not added to order_item table.
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html
I tried following "Class Table Inheritance" and "Mapped Superclasses". Example from "Mapped Superclasses" does not add $mappedRelated1 to EntitySubClass . And example from "Class Table Inheritance" removes every other extended field from Meal class and adds some kind of "dtype" column to Meal table.
/**
* #ORM\Entity(repositoryClass="App\Repository\OrderItemRepository")
*/
class OrderItem extends Meal
{
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Order", inversedBy="orderItems")
*/
private $order;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank
* #Assert\Type("string")
*/
private $amount;
}
/**
* #ORM\Entity(repositoryClass="App\Repository\MealRepository")
*/
class Meal
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255)
* #Assert\NotBlank
* #Assert\Type("string")
*/
protected $name;
/**
* #ORM\Column(type="float")
* #Assert\NotBlank
* #Assert\Type("float")
*/
protected $price;
/**
* #ORM\Column(type="string", length=255)
* #Assert\Image(
* mimeTypes={"image/jpeg", "image/png"}
* )
*/
protected $image;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Menu", inversedBy="meals")
*/
protected $menu;
}
I expect to have "menu_id" column in "order_item" table, which would have relation to Menu entity.
I know that I could copy all the properties from Meal to OrderItem, but that does not sound right.
EDIT:
Both Meal and OrderItem should be able to have it's instance.
menu_id column is already exists in meal
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Menu", inversedBy="meals")
*/
protected $menu;
OrderItem class doesnt need to have just because it is already subclass of meal.
Also you dont need to copy all propertites from Meal to OrderItem.
u can check php extends here
I had the same issue. I used Trait instead of inheritance and the problem was solved.
Your class will be like this
trait MealInfoTrait
{
/**
* #ORM\Column(type="string", length=255)
* #Assert\NotBlank
* #Assert\Type("string")
*/
protected $name;
/**
* #ORM\Column(type="float")
* #Assert\NotBlank
* #Assert\Type("float")
*/
protected $price;
/**
* #ORM\Column(type="string", length=255)
* #Assert\Image(
* mimeTypes={"image/jpeg", "image/png"}
* )
*/
protected $image;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Menu", inversedBy="meals")
*/
protected $menu;
}
Then, change you Meal class
/**
* #ORM\Entity(repositoryClass="App\Repository\MealRepository")
*/
class Meal
{
use MealInfoTrait;
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
}
And do the same with your OrderItem class. Don't forget to add the id for the database.
/**
* #ORM\Entity(repositoryClass="App\Repository\OrderItemRepository")
*/
class OrderItem
{
use MealInfoTrait;
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Order", inversedBy="orderItems")
*/
private $order;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank
* #Assert\Type("string")
*/
private $amount;
}

Error mapping doctrine on OneToOne bidirectional relation

I'm trying to create an entity that is connected with another entity 1:1. The whole point is that Equip has to have an Estadi, and just one. I can update the schema correctly, the database is okay, but on the webpage debugger appears a mapping error.
The association AppBundle\Entity\Equip#estadi refers to the inverse
side field AppBundle\Entity\Estadi#nom which is not defined as
association.
The association AppBundle\Entity\Equip#estadi refers to the inverse
side field AppBundle\Entity\Estadi#nom which does not exist
This is entity Estadi:
/**
* #ORM\Entity
* #ORM\Table(name="estadis")
*/
class Estadi{
/**
* #ORM\Column(type="string",length=30)
* #ORM\OneToOne(targetEntity="Equip",mappedBy="estadi",cascade={"persist"})
* #ORM\Id
*/
protected $nom;
/**
* #ORM\Column(type="integer")
*/
protected $aforament;
/**
* #ORM\Column(type="integer")
*/
protected $num_portes;
/**
* #ORM\Column(type="string",length=50)
*/
protected $direccio;
/**
* #ORM\Column(type="string", length=4)
*/
protected $any_construccio;
/**
* #ORM\Column(type="string", length=30)
*/
protected $nom_aficio;
}
This is Entity Equip:
/**
* #ORM\Entity
* #ORM\Table(name="equips")
*/
class Equip{
/**
* #ORM\Column(type="string",length=30)
* #ORM\Id
*/
protected $nom;
/**
* #ORM\Column(type="integer")
*/
protected $punts_lliga;
/**
* #ORM\Column(type="integer")
*/
protected $num_jugadors;
/**
* #ORM\OneToOne(targetEntity="Estadi",inversedBy="nom")
* #ORM\JoinColumn(name="nom_estadi",referencedColumnName="nom",onDelete="SET NULL")
*/
protected $estadi;
/**
* #ORM\OneToOne(targetEntity="Entrenador",inversedBy="nom")
* #ORM\JoinColumn(name="nom_entrenador",referencedColumnName="nom",onDelete="SET NULL")
*/
protected $entrenador;
/**
* #ORM\ManyToOne(targetEntity="Lliga",inversedBy="equips")
* #ORM\JoinColumn(name="nom_lliga",referencedColumnName="nom",onDelete="SET NULL")
*/
protected $lliga;
/**
* #ORM\OneToMany(targetEntity="Jugador",mappedBy="nom_equip")
*/
protected $jugadors;
public function __construct(){
$this->jugadors = new ArrayCollection();
}
}
When you define the #OneToOne annotation, it should not be on your primary key. Either the owning entity should contain a single association (unidirectional), or each entity should contain an association to the other - as entities, not connected to a primary key.
Your Equip mapping should instead look like this:
/**
* #ORM\OneToOne(targetEntity="Estadi", inversedBy="equip")
* #ORM\JoinColumn(name="nom_estadi", referencedColumnName="nom")
*/
protected $estadi;
public function setEstadi(Estadi $estadi)
{
$this->estadi = $estadi;
return $this;
}
public function getEstadi()
{
return $this->estadi;
}
Your Estadi mapping should instead look like this:
/**
* #OneToOne(targetEntity="Equip", mappedBy="estadi")
*/
protected $equip;
public function getEquip(Equip $equip)
{
return $this->equip;
}
I removed the cascade and onDelete, because if you're handling everything through Doctrine it should handle that for you automatically, but you still might have a use for them. I also only put the setter on the owning entity from the way you described, but you could put it back on your Estadi entity as well - that's up to you.

Symfony2 + OneToMany - read from database error

I'm trying to learn read/write data to/from database, and I have a huge problem :
My entities looks :
Kategorie:
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="kategorie")
*/
class Kategorie
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $idkategorii;
/**
* #ORM\Column(type="string", length=50)
*/
protected $nazwa;
/**
* #ORM\OneToMany(targetEntity="Ogloszenia", mappedBy="ogloszenia")
*/
protected $ogloszenia;
Ogloszenia:
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="ogloszenia")
*/
class Ogloszenia
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string",length=100)
*/
protected $tytul;
/**
* #ORM\Column(type="string", length=120)
*/
protected $tytul_seo; //tytul bez polskich znaków etc.
/**
* #ORM\Column(type="text")
*/
protected $tresc; //tresc ogloszenia
/**
* #ORM\Column(type="string",length=50)
*/
protected $dodal; //imie osoby ktora dodala ogloszenie
/**
* #ORM\Column(type="string", length=50)
*/
protected $kontakt; //nr tel lub mail
/**
* #ORM\ManyToOne(targetEntity="Kategorie", inversedBy="kategoria")
* #ORM\JoinColumn(name="kategoria", referencedColumnName="idkategorii")
*/
protected $kategoria;
Now, in my controller i'm trying to read all values :
public function odczytajAction()
{
$id = 1;
$kategoria = $this->getDoctrine()
->getRepository('FrontendOgloszeniaBundle:Kategorie')
->find($id);
$ogl = $kategoria->getOgloszenia();
foreach($ogl as $o)
{
print_r($o);
}
return new Response('test odczytu');
}
And unfortunately symfony2 gives me an following error
Notice: Undefined index: ogloszenia in /home/sl4sh/public_html/Projekt1/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1574
500 Internal Server Error - ErrorException
So, please tell me what's wrong with mine code ?
You have invalid mappedBy annotation:
/**
* #ORM\OneToMany(targetEntity="Ogloszenia", mappedBy="kategoria")
*/
protected $ogloszenia;
And also inverse side:
/**
* #ORM\ManyToOne(targetEntity="Kategorie", inversedBy="ogloszenia")
* #ORM\JoinColumn(name="kategoria", referencedColumnName="idkategorii")
*/
protected $kategoria;

Symfony2 and Doctrine ManyToMany realtionship

I am heaving some headache about this and I don't find the solution.
I have 2 entities: Movie.php and Category.php
I want one Movie to have multiple Categories and vice versa. That's why I chose a ManyToMany relationship.
Now I am wondering... What happens on the database site? Is there a "in-between" table that maps movie_ids to category_ids? But that's not what happened. Actually my first try was to make a MovieCategory entity - I mapped one movie to multiple categories with OneToMany and in the MovieCategory entity I made a OneToOne connection to get the category name from my Category entity. But I guess that's not how it should work, am I right?
Now here's my code of how i think it should work, I really appreciate any help I can get on this:
Movie.php
<?php
/**
* #ORM\Table(name="movies")
* #ORM\HasLifecycleCallbacks()
*/
class Movie
{
public function __construct()
{
$this->categories = new ArrayCollection();
}
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** #ORM\Column(type="string") */
protected $moviename;
/**
* #ORM\ManyToMany(targetEntity="Category", mappedBy="movie")
*/
protected $categories;
}
Category.php
<?php
/**
* #ORM\Table(name="categories")
* #ORM\HasLifecycleCallbacks()
*/
class Category
{
public function __construct()
{
$this->movies = new ArrayCollection();
}
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $name;
// ...
/**
* #ORM\ManyToMany(targetEntity="Movie", mappedBy="movie", cascade={"persist"})
*/
protected $movies;
}
According to Doctrine docs it should look this way:
// Movie.php
/**
* #ORM\ManyToMany(targetEntity="Category", inversedBy="movies")
* #ORM\JoinTable(name="movies_categories")
*/
protected $categories;
// ...
// Category.php
/**
* #ORM\ManyToMany(targetEntity="Movie", mappedBy="categories")
*/
protected $movies;
You're using the same value for mappedBy in both declarations. Plus, the value you use is singular, it should be plural. This cannot work.

Resources