Witam,
Próbuję zrobić rozwiązanie, w którym mam przepis, każdy przepis może mieć wiele kategorii oraz wiele składników. Składniki w danym przepisie mogą mieć różne wartości np.
Przepis: Jajecznica
Składniki:
Jajka - 2 (szt.)
Masło - 50 (g)
Próbuję to zrobić na obiektach. I przepis, kategoria i składnik zrobiłem jako encje. Ustawiłem w przepisie asocjacje many to many zarówno dla kategorii, jak i składników. I teraz nie wiem, jak zrobić, żeby wybrany w danym przepisie składnik miał w nim przypisaną wartość. Da się zrobić asocjację kolejnego poziomu? Albo czy trzeba zrobić kolejną encję np. Ilość - i pobierać wszystkie składniki z danego przepisu i tworzyć obiekty ilość, które mają powiązanie z przepisem i składnikiem?
Czy myślę w złym kierunku? I jest prostsze/inne rozwiązanie?
Piszę to w Symfony 3.4.
Poniżej moje encje na ten moment:
namespace App\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;
use Doctrine\Common\Collections\Collection as Collection;
use App\BlogBundle\Libs\Utils as Utils;
use App\BlogBundle\Entity\Category as Category;
use App\BlogBundle\Entity\Tag as Tag;
/**
* @ORM\Entity(repositoryClass="App\BlogBundle\Repository\PostRepository")
* @ORM\Table(name="blog_posts")
* @ORM\HasLifecycleCallbacks
*
* @UniqueEntity(fields={"title"})
* @UniqueEntity(fields={"slug"})
*/
class Post {
const DEFAULT_AVATAR = 'default-thumbnail.png';
const UPLOAD_DIR = 'uploads/thumbnails/';
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=120, unique=true)
*
* @Assert\NotBlank
*
* @Assert\Length(
* max = 120
* )
*/
private $title;
/**
* @ORM\Column(type="string", length=120, unique=true)
*
* @Assert\Length(
* max = 120
* )
*/
private $slug;
/**
* @ORM\Column(type="text")
*
* @Assert\NotBlank
*/
private $content;
/**
* @ORM\ManyToMany(
* targetEntity = "Category",
* inversedBy = "posts"
* )
*
* @ORM\JoinTable(
* name = "blog_posts_categories"
* )
*
*
*/
private $categories;
/**
* @ORM\ManyToMany(
* targetEntity = "Ingredient",
* inversedBy = "posts"
* )
*
* @ORM\JoinTable(
* name = "blog_posts_ingredients"
* )
*
* @Assert\Count(
* min=1
* )
*/
private $ingredients;
/**
* @ORM\Column(name="create_date", type="datetime")
*/
private $createDate;
/**
* @ORM\Column(name="update_date", type="datetime", nullable=true)
*/
private $updateDate = null;
/**
* Constructor
*/
public function __construct()
{
$this->ingredients = new ArrayCollection();
$this->categories = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set slug
*
* @param string $slug
* @return Post
*/
public function setSlug($slug)
{
$this->slug = Utils::sluggify($slug);
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set content
*
* @param string $content
* @return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Add category
*
* @param Category $category
*
* @return Post
*/
public function addCategory(Category $category)
{
$this->categories[] = $category;
return $this;
}
/**
* Remove category
*
* @param Category $category
*/
public function removeCategory(Category $category)
{
$this->categories->removeElement($category);
}
/**
* Get categories
*
* @return Collection
*/
public function getCategories()
{
return $this->categories;
}
/**
* Add ingredients
*
* @param Ingredient $ingredients
* @return Post
*/
public function addIngredient(Tag $ingredients)
{
$this->ingredients[] = $ingredients;
return $this;
}
/**
* Remove ingredients
*
* @param Ingredient $ingredients
*/
public function removeIngredient(Ingredient $ingredients)
{
$this->ingredients->removeElement($ingredients);
}
/**
* Get ingredients
*
* @return Collection
*/
public function getIngredients()
{
return $this->ingredients;
}
/**
* Set createDate
*
* @param \DateTime $createDate
* @return Post
*/
public function setCreateDate($createDate)
{
$this->createDate = $createDate;
return $this;
}
/**
* Get createDate
*
* @return \DateTime
*/
public function getCreateDate()
{
return $this->createDate;
}
/**
* Set updateDate
*
* @param \DateTime $updateDate
*
* @return Post
*/
public function setUpdateDate($updateDate)
{
$this->updateDate = $updateDate;
return $this;
}
/**
* Get updateDate
*
* @return \DateTime
*/
public function getUpdateDate()
{
return $this->updateDate;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function preSave(){
if(null === $this->slug){
$this->setSlug($this->getTitle());
}
if(null == $this->createDate){
$this->createDate = new \DateTime();
}
}
/**
* @ORM\PostPersist
* @ORM\PostUpdate
*/
public function postSave(){
}
/**
* @ORM\PostRemove
*/
public function postRemove() {
}
}
namespace App\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\BlogBundle\Repository\CategoryRepository")
* @ORM\Table(name="blog_categories")
*/
class Category {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=120, unique=true)
*/
private $name;
/**
* @ORM\Column(type="string", length=120, unique=true)
*/
private $slug;
/**
* @ORM\ManyToMany(
* targetEntity = "Post",
* mappedBy = "categories"
* )
*/
protected $posts;
/**
* Constructor
*/
public function __construct()
{
$this->posts = new ArrayCollection();
}
public function __toString()
{
return (string) $this->name;
}
/**
* Add posts
*
* @param Post $posts
* @return Category
*/
public function addPost(Post $posts)
{
$this->posts[] = $posts;
return $this;
}
/**
* Remove posts
*
* @param Post $posts
*/
public function removePost(Post $posts)
{
$this->posts->removeElement($posts);
}
/**
* Get posts
*
* @return Collection
*/
public function getPosts()
{
return $this->posts;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set slug
*
* @param string $slug
* @return Category
*/
public function setSlug($slug)
{
$this->slug = Utils::sluggify($slug);
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function preSave(){
if(null === $this->slug){
$this->setSlug($this->getName());
}
}
}
/**
* @ORM\Entity(repositoryClass="App\BlogBundle\Repository\IngredientRepository")
* @ORM\Table(name="blog_ingredients")
*/
class Ingredient {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=120, unique=true)
*/
private $name;
/**
* @ORM\Column(type="string", length=120, unique=true)
*/
private $slug;
/**
* @ORM\ManyToMany(
* targetEntity = "Post",
* mappedBy = "ingredients"
* )
*/
protected $posts;
/**
* Constructor
*/
public function __construct()
{
$this->posts = new ArrayCollection();
}
public function __toString()
{
return (string) $this->name;
}
/**
* Add posts
*
* @param Post $posts
* @return Ingredient
*/
public function addPost(Post $posts)
{
$this->posts[] = $posts;
return $this;
}
/**
* Remove posts
*
* @param Post $posts
*/
public function removePost(Post $posts)
{
$this->posts->removeElement($posts);
}
/**
* Get posts
*
* @return Collection
*/
public function getPosts()
{
return $this->posts;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Ingredient
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set slug
*
* @param string $slug
* @return Ingredient
*/
public function setSlug($slug)
{
$this->slug = Utils::sluggify($slug);
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function preSave(){
if(null === $this->slug){
$this->setSlug($this->getName());
}
}
}