I have a probleme for a job in Symfony 4.2.
I'm a beginner on Symfony and I need help for understand cascade deletion, and what I have a foreign key error
I have 3 entity like this :
The first is call Image
class Image
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\OneToOne(
* targetEntity="App\Entity\Thumbnail",
* mappedBy="Image",
* cascade={"persist"},
* orphanRemoval=true
* )
*/
private $Thumbnail;
}
the second is the Thumbnail of this image :
class Thumbnail
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\OneToOne(
* targetEntity="App\Entity\Image",
* inversedBy="Thumbnail",
* cascade={"persist", "remove"}
* )
* #ORM\JoinColumn(nullable=false)
*/
private $image;
}
and a Div which contain a Thumbnail :
class Div
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\OneToOne(targetEntity="App\Entity\Thumbnail", cascade={"persist", "remove"})
*/
private $Thumbnail;
}
this is my three entities, and my problem is when I try to delete one of this three entities because :
When I delete an Image I need to delete his Thumbnail, but the Div can live without Thumbnail so I mustn't delete the Div.
When I delete a Thumbnail, I mustn't delete the Image, and I mustn't delete the Div
When I delete the Div I mustn't delete the Thumbnail.
I would like to know the best practice for do this job.
Thank for help and sorry for my bad english.
Try below code in your Div entity:
/**
* #ORM\OneToOne(targetEntity="App\Entity\Thumbnail", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="thumbnail_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $Thumbnail;
Related
I am a beginner in API platform. I am working with two entities Author and Book with a OneToMany relationship:
a user can have several books
a book can belong to only one Author.
I've tried POST, GET and DELETE methods, they work. However, the PUT method does not work and returns this error:
"hydra:title": "An error occurred",
"hydra:description": **"An exception occurred while executing 'UPDATE Book SET author_id = ? WHERE id = ?' with params [null, 1]:\n\nSQLSTATE[23000]:
Integrity constraint violation: 1048 Column 'author_id' cannot be null"
Here's my code:
/**
* #ORM\Entity(repositoryClass="App\Repository\AuthorRepository")
* #ApiResource(
* normalizationContext={"groups"={"book:output"}},
* denormalizationContext={"groups"={"book:input"}}
* )
*/
class Author
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
* #Groups({"book:input"})
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
* #Groups({"book:output", "book:input"})
*/
private $firstname;
/**
* #ORM\Column(type="string", length=255)
* #Groups({"book:output", "book:input"})
*/
private $lastname;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Book", mappedBy="author", cascade={"all"})
* #Groups({"book:output", "book:input"})
*/
private $books;
/**
* #ORM\Entity(repositoryClass="App\Repository\BookRepository")
* #ApiResource()
*/
class Book
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
* #Groups({"book:output", "book:input"})
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
* #Groups({"book:output", "book:input"})
*/
private $name;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Author", inversedBy="books")
* #ORM\JoinColumn(nullable=false)
*/
private $author;
You seem like passing a null id for an updated entity which is essential for the ORM to know.
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;
}
I have this entity
class Category
{
/**
* #var int
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Product",mappedBy="category")
*/
private $products;
/**
* #var int
* #ORM\Column(name="orderNr",type="integer")
*/
private $order;
/**
* #ORM\OneToMany(targetEntity="Category", mappedBy="parent")
*/
private $children;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Category",inversedBy="children")
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
I want to make a query to get all categories and for each category, the childrens.
I tried many versions of queryes from other questions, but nothing seems to work.
Can anyone help me,please?
In the end i need something like this
[Category Parent 1]
[Childrens]
[0]
//details
[1]
//details
Trying to utilize inheritance, I've created the following entities:
/**
* #ORM\Table(name="persons")
* #ORM\Entity()
*/
class Person
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* #ORM\OneToOne(targetEntity="Image", cascade={"persist"})
* #ORM\JoinColumn(name="image_id", referencedColumnName="id")
*/
protected $image;
}
/**
* #ORM\Table(name="actors")
* #ORM\Entity()
*/
class Actor extends Person
{
/**
* #ORM\Column(name="character", type="string", length=255)
*/
private $character;
}
/**
* #ORM\Table(name="images")
* #ORM\Entity()
*/
class Image
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="path", type="string", length=255)
*/
private $path;
}
Which almost works perfectly. The generated actors-table contains all the persons-fields, except for the image-relation. I've tried to change the relation to a ManyToOne, which didn't help.
How to make the Actor-entity also inherit all joined fields? I'm open to other solutions, if the above isn't ideal.
You need a parent construct in your Actor class:
public function __construct()
{
parent::__construct();
// your own logic
}
It is advised that you add an ID aswell.
I'm working on a solution for adding tags to two differente entities.
in order to get data easily in the frontend i created a joinTable named Tag_Mapping like this :
class TagMapping
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Tag", inversedBy="tags")
*/
private $tag;
/**
* #ORM\ManyToOne(targetEntity="Feed", inversedBy="tags")
*/
private $feed;
/**
* #ORM\ManyToOne(targetEntity="Question", inversedBy="tags")
*/
private $question;
...
}
The Tag Entity :
class Tag
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255, unique=true)
*/
private $name;
/**
* #Gedmo\Slug(fields={"name"})
* #ORM\Column(unique=true)
*/
private $slug;
/**
* #ORM\OneToMany(targetEntity="TagMapping", mappedBy="tag", cascade="remove")
*/
private $tags;
...
}
and in both other entities (Feed and Question) I made reference to TagMapping entity like this
...
/**
* #ORM\ManyToMany(targetEntity="Tag")
* #JoinTable(name="tag_mapping",
* joinColumns={#JoinColumn(name="feed_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="tag_id", referencedColumnName="id")}
* )
*/
private $tags;
...
the problem I'm facing is that it's not a valid way to do it, as it shows an error when i'm trying to execute :
php app/console doctrine:schema:update --force
saying that tag_mapping table already exists.
do you have any idea how can i get it done using only one joinTable instead of one for each relation ?
Thanks.