I have two objects in which everyone Productcan have unique Aspectobjects associated with it .
Since the product table is very large, I use an bigintidentifier for this, and therefore I am trying to create a composite key Aspectto use the product identifier and smallint(which I am trying with a step Product#aspectsCount). However, I get a ContextErrorException:
bigint
smallint
Product#aspectsCount
Note: Undefined index: aspect
My entities are as shown below (I initially tried indexBy="id")in the hope of using a numerical identifier Aspect, but it seems I can not get this work to use either the one used namebelow to be more compatible with the examples I've read online):
indexBy="id")
name
class Product { /** * @ORM\Column(type="bigint", options={"unsigned"=true}) * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\OneToMany(targetEntity="Aspect", mappedBy="product", cascade={"all"}, indexBy="name") */ private $aspects; /** * @ORM\Column(name="aspectsCount", type="smallint", options={"unsigned"=true}, nullable=false) */ private $aspectsCount; public function __construct() { $this->aspects = new ArrayCollection(); $this->setCreateDT(new \Datetime); $this->setUpdateDT(new \Datetime); $this->aspectsCount = 0; } /** * Add aspect * * @param \AppBundle\Entity\Aspect $aspect * * @return product */ public function addAspect($name) { $aspect = new Aspect($this, $name); $this->aspects[$name] = $aspect; return $this; } /** * Remove aspect * * @param \AppBundle\Entity\Aspect $aspect */ public function removeAspect(\AppBundle\Entity\Aspect $aspect) { $this->aspects->removeElement($aspect); $this->setAspectsCount($this->aspectsCount-1); } }
class Aspect { /** * @ORM\Id * @ORM\ManyToOne(targetEntity="Product", inversedBy="aspects") * @ORM\JoinColumn(name="product_id", referencedColumnName="id") */ private $product; /** * @ORM\Id * @ORM\Column(type="smallint", options={"unsigned"=true}, nullable=false) */ private $id; /** * @ORM\Column(name="name", type="text") */ private $name; public function __construct($product, $name) { $product->setAspectsCount($product->getAspectsCount()+1); $this->product = $product; $this->id = $product->getAspectsCount(); $this->name = $name; } }
In addition, if another table should exist βunderβ Aspect, how could such an association be created? Will Doctrine handle the composite key internally or will I need to do something, for example:
class Aspect_subtype { /** * @ORM\Id * @ORM\ManyToOne(targetEntity="Product") * @ORM\JoinColumn(name="product_id", referencedColumnName="id") */ private $product; /** * @ORM\Id * @ORM\ManyToOne(targetEntity="Aspect") * @ORM\JoinColumn(name="aspect_id", referencedColumnName="id") */ private $aspect; /** * @ORM\Id * @ORM\Column(type="smallint", options={"unsigned"=true}, nullable=false) */ private $id; /** * @ORM\Column(name="name", type="text") */ private $name; // etc... }
in the product Entity changes $ Aspets annotations to this
/** *@ORM\ManyToOne(targetEntity="Aspect") *@ORM\JoinColumn(name="Client", referencedColumnName="product_id",onDelete="CASCADE") */ private $aspects;
Then you need to update your database schema
, , $this- > [$ name] = $aspect; ArrayCollection, , , . , ArrayCollections , . $this- > aspect- > add ($ aspect);
manyToOne, , , , .
, : , , , . ( ), , //etc.... , . , , .
, , , ( , ), key ( ), getAspects() arraycollection, ( ), . - , , , , , , , .
Source: https://habr.com/ru/post/1665558/More articles:Is it possible to have different orientations for viewing controllers inside a UINavigationController? - iosAWS cloud information: how to reuse a bash script placed in a user data parameter when creating EC2? - linuxSwitching a Git merge workflow to rebase: how to clear merge history? - gitReading Azure Service Bus Queue - node.jsStatus 400: Invalid multiplayer payload format for testing - payloadpandas sort data by date - pythonUsing Ref as the first argument to the Fn :: Sub intrinsic function - amazon-web-servicesHow to properly generate the R.java file for an Android library project when creating an APK from a pure Makefile? - androidemail in microservice architecture - restHow to create the following resolver for GraphQL server? - javascriptAll Articles