I'm having an issue with one relationship in my system. I'm pretty sure I'm doing this correctly and that I have a bug in the code I can't see.
I've pasted my class below. Basically when I do fighter.getFighterAttributes() i get a null array. It does not happen with the other relationships in the class. Furthermore when I look at the logs I see that the other relationships are being called but not FighterAttributes().
The table has entries that have been inserted using the entity.
It has to be a BUG IN MY CODE, not sure were though :(
Below Class and logs.
<?php
namespace Acme\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="Acme\AppBundle\Repository\FighterRepository")
*/
class Fighter
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $firstName;
/**
* @ORM\OneToMany(targetEntity="Contract", mappedBy="fighter")
*/
protected $contracts;
/**
* @ORM\ManyToMany(targetEntity="Bout", mappedBy="fighters")
*/
protected $bouts;
/**
* @ORM\OneToMany(targetEntity="FighterAttribute", mappedBy="fighter")
*/
protected $fighterAttributes;
// Custom ------------------------------------------------
/**
* Set createDT
*/
public function setCreateDT()
{
$this->createDT = new \DateTime("now");
}
// Automated ---------------------------------------------
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* @param string $firstName
* @return Fighter
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Add bouts
*
* @param \Acme\AppBundle\Entity\Bout $bouts
* @return Fighter
*/
public function addBout(\Acme\AppBundle\Entity\Bout $bouts)
{
$this->bouts[] = $bouts;
return $this;
}
/**
* Remove bouts
*
* @param \Acme\AppBundle\Entity\Bout $bouts
*/
public function removeBout(\Acme\AppBundle\Entity\Bout $bouts)
{
$this->bouts->removeElement($bouts);
}
/**
* Get bouts
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBouts()
{
return $this->bouts;
}
/**
* Add contracts
*
* @param \Acme\AppBundle\Entity\Contract $contracts
* @return Fighter
*/
public function addContract(\Acme\AppBundle\Entity\Contract $contracts)
{
$this->contracts[] = $contracts;
return $this;
}
/**
* Remove contracts
*
* @param \Acme\AppBundle\Entity\Contract $contracts
*/
public function removeContract(\Acme\AppBundle\Entity\Contract $contracts)
{
$this->contracts->removeElement($contracts);
}
/**
* Get contracts
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getContracts()
{
return $this->contracts;
}
/**
* Constructor
*/
public function __construct()
{
$this->contracts = new \Doctrine\Common\Collections\ArrayCollection();
$this->bouts = new \Doctrine\Common\Collections\ArrayCollection();
$this->fighterAttributes = new \Doctrine\Common\Collections\ArrayCollection();
$this->setCreateDT();
}
/**
* Add fighterAttributes
*
* @param \Acme\AppBundle\Entity\FighterAttribute $fighterAttributes
* @return Fighter
*/
public function addFighterAttribute(\Acme\AppBundle\Entity\FighterAttribute $fighterAttributes)
{
$this->fighterAttributes[] = $fighterAttributes;
return $this;
}
/**
* Remove fighterAttributes
*
* @param \Acme\AppBundle\Entity\FighterAttribute $fighterAttributes
*/
public function removeFighterAttribute(\Acme\AppBundle\Entity\FighterAttribute $fighterAttributes)
{
$this->fighterAttributes->removeElement($fighterAttributes);
}
/**
* Get fighterAttributes
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getFighterAttributes()
{
return $this->fighterAttributes;
}
}
Log Below:
[2014-02-14 01:36:13] doctrine.DEBUG: SELECT ... FROM Fighter t0 WHERE t0.id = ? LIMIT 1 ["110"] []
[2014-02-14 01:36:13] doctrine.DEBUG: SELECT ... FROM Contract t0 WHERE t0.fighter_id = ? [110] []
[2014-02-14 01:36:13] doctrine.DEBUG: SELECT ... FROM Bout t0 INNER JOIN BoutFighter ON t0.id = BoutFighter.bout_id WHERE BoutFighter.fighter_id = ? [110] []
Note that there is no entry for FighterAttribute.
Class FighterAttribute below:
<?php
namespace Acme\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class FighterAttribute
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="integer")
*/
protected $amount;
/**
* @ORM\ManyToOne(targetEntity="Fighter", inversedBy="fighterAttributes")
*/
protected $fighter;
/**
* @ORM\ManyToOne(targetEntity="Attribute")
*/
protected $attribute;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set fighter
*
* @param \Acme\AppBundle\Entity\Fighter $fighter
* @return FighterAttribute
*/
public function setFighter(\Acme\AppBundle\Entity\Fighter $fighter = null)
{
$this->fighter = $fighter;
return $this;
}
/**
* Get fighter
*
* @return \Acme\AppBundle\Entity\Fighter
*/
public function getFighter()
{
return $this->fighter;
}
/**
* Set attribute
*
* @param \Acme\AppBundle\Entity\Attribute $attribute
* @return FighterAttribute
*/
public function setAttribute(\Acme\AppBundle\Entity\Attribute $attribute = null)
{
$this->attribute = $attribute;
return $this;
}
/**
* Get attribute
*
* @return \Acme\AppBundle\Entity\Attribute
*/
public function getAttribute()
{
return $this->attribute;
}
/**
* Set amount
*
* @param integer $amount
* @return FighterAttribute
*/
public function setAmount($amount)
{
$this->amount = $amount;
return $this;
}
/**
* Get amount
*
* @return integer
*/
public function getAmount()
{
return $this->amount;
}
}
Update
This code prints "null"
$em = $this->getDoctrine()->getManager();
$fighter = $em->getRepository('AcmeAppBundle:Fighter')
->findOneById($fighter_id);
\Doctrine\Common\Util\Debug::dump($fighter->getFighterAttributes(), 1);
die();
This code prints the fighterAttributes:
$em = $this->getDoctrine()->getManager();
$fighter = $em->getRepository('AcmeAppBundle:Fighter')
->findOneById($fighter_id);
$attributes = $em->getRepository('AcmeAppBundle:FighterAttribute')
->findByFighter($fighter);
\Doctrine\Common\Util\Debug::dump($attributes, 1);
die();
OutPut:
array (size=12) 0 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 1 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 2 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 3 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 4 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 5 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 6 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 7 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 8 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 9 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 10 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 11 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38)
This code:
$em = $this->getDoctrine()->getManager();
$fighter = $em->getRepository('AcmeAppBundle:Fighter')
->findOneById($fighter_id);
$attributes = $em->getRepository('AcmeAppBundle:FighterAttribute')
->findByFighter($fighter);
foreach ($attributes as $attribute)
{
$fighter->addFighterAttribute($attribute);
}
$em->persist($fighter);
$em->flush();
$fighter1 = $em->getRepository('AcmeAppBundle:Fighter')
->findOneById($fighter_id);
\Doctrine\Common\Util\Debug::dump($fighter1->getFighterAttributes(), 1);
die();
Output:
array (size=12) 0 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 1 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 2 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 3 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 4 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 5 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 6 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 7 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 8 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 9 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 10 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38) 11 => string 'Acme\AppBundle\Entity\FighterAttribute' (length=38)
Thanks for the help!!