I have two entities where each Product can have oneToMany Aspect entities associated to it.
As the Products table is very large I am using bigint
for it's ID, and consequently, I am trying to build a composite key for Aspect to use Product ID and a smallint
(which I am trying to increment with Product#aspectsCount
). However, I am getting a ContextErrorException:
Notice: Undefined index: aspect
My entities are as below (I originally tried indexBy="id")
in the hope of using Aspect's numeric ID but I can't seem to get that working either so used name
below to be more consistent with examples I've read online):
Product Entity
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 Entity
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;
}
}
By extension, if another table should exist "underneath" Aspect, how would such an association be made? Would Doctrine handle the composite key internally or would I need to do something such as:
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 product Entity change the anotations of $Aspets to this
Then you need to update your database schema