ContextErrorException: Undefined Pointer when using a composite key for doctrine associations with Symfony 3 objects

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:

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

Product object

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

Aspect structure

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...
}
+4
source share
2 answers

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

+2

, , $this- > [$ name] = $aspect; ArrayCollection, , , . , ArrayCollections , . $this- > aspect- > add ($ aspect);

manyToOne, , , , .

, : , , , . ( ), , //etc.... , . , , .

, , , ( , ), key ( ), getAspects() arraycollection, ( ), . - , , , , , , , .

+1

Source: https://habr.com/ru/post/1665558/


All Articles