我的系统中存在一个关系问题。我很确定我正确地做到了这一点,并且我在代码中看到了一个我看不到的错误。
我在下面贴了我的课。基本上当我做fighter.getFighterAttributes()时,我得到一个空数组。它不会发生在班级中的其他关系中。此外,当我查看日志时,我看到其他关系被调用但不是FighterAttributes()。
该表包含使用实体插入的条目。
它必须是我的代码中的BUG,但不确定是否:(
在课程和日志之下。
<?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)
感谢您的帮助!!
答案 0 :(得分:0)
确定。所以我发现了这个问题。我一直在玩的缓存设置给我带来了各种各样的问题。
我看到即使我更新了一些对象,该应用程序的表现也不规律而且没有识别出一些变化。即使我已多次清除应用程序缓存,也就是APC缓存需要清除。
我现在决定禁用它,因为我还在开发中,但我会假设清除APC缓存也会解决问题。在config_dev.yml中,我评论了以下内容:
#doctrine:
# orm:
# metadata_cache_driver: apc
# result_cache_driver: apc
# query_cache_driver: apc
@Patt,感谢您的帮助,并通过您的解决方案帮助我解决了问题,但是您执行此更改后,您建议的代码更改没有增加价值。我怀疑没有必要更新关系的非拥有方,除非有计划使用它或者做某种类型的级联持久性,我不是。