我的系统中有一个关系有问题。我很确定我这样做是正确的,并且我看不到代码中的错误。
我在下面粘贴了我的课程。基本上,当我执行 fighter.getFighterAttributes() 时,我得到一个空数组。类中的其他关系不会发生这种情况。此外,当我查看日志时,我看到正在调用其他关系,但没有调用 FighterAttributes()。
该表具有已使用实体插入的条目。
它必须是我的代码中的一个错误,但不确定:(
下面的类和日志。
<?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;
}
}
日志如下:
[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] []
请注意,没有 FighterAttribute 条目。
下面的类 FighterAttribute:
<?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;
}
}
更新
此代码打印“null”
$em = $this->getDoctrine()->getManager();
$fighter = $em->getRepository('AcmeAppBundle:Fighter')
->findOneById($fighter_id);
\Doctrine\Common\Util\Debug::dump($fighter->getFighterAttributes(), 1);
die();
此代码打印 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();
输出:
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)
这段代码:
$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();
输出:
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)
谢谢您的帮助!!