这是一个简单的问题,但在经过Stackoverflow中的所有类似线程和其他地方没有成功之后,问题仍然存在。
我无法获得双向的一对多关系。 Biomass.field是可访问的(拥有方),但Field.biomasses(反方)不是。
表格(删除的消耗性字段)
CREATE TABLE `field` (
`field_id` varchar(255) NOT NULL,
PRIMARY KEY (`field_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `biomass` (
`biomass_id` int(11) NOT NULL AUTO_INCREMENT,
`field_id` varchar(255) NOT NULL,
PRIMARY KEY (`biomass_id`),
KEY `FK_field_has_0m_biomasses_idx` (`field_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `biomass`
ADD CONSTRAINT `FK_field_has_0m_biomasses` FOREIGN KEY (`field_id`) REFERENCES
`field` (`field_id`) ON DELETE CASCADE ON UPDATE CASCADE;
Field.php
<?php
namespace Acme\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Acme\MainBundle\Entity\Field
*
* @ORM\Table(name="field")
* @ORM\Entity
*/
class Field {
public function __construct() {
$this->biomasses = new ArrayCollection();
}
/**
* @var string $id
*
* @ORM\Column(name="field_id", type="string", length=255, nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/*
* var ArrayCollection $biomasses
*
* @ORM\OneToMany(targetEntity="Biomass", mappedBy="field", cascade={"all"})
*/
private $biomasses;
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Get biomasses
*
* @return ArrayCollection
*/
public function getBiomasses() {
return $this->biomasses;
}
/**
* Set biomasses
*
* @param ArrayCollection $biomasses
* @return ArrayCollection
*/
public function setBiomasses(ArrayCollection $biomasses) {
$this->biomasses = $biomasses;
return $this->biomasses;
}
/**
* Add Biomass
*
* @param Biomass $biomass
* @return ArrayCollection
*/
public function addBiomass(Biomass $biomass) {
$biomass->setField($this);
return $this->biomasses[] = $biomass;
}
/**
* Set biomass
*
* @param Biomass $biomass
* @return ArrayCollection
*/
public function removeBiomass(Biomass $biomass) {
return $this->biomasses->removeElement($biomass);
}
function __toString() {
return $this->getId();
}
}
Biomass.php
<?php
namespace Acme\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Acme\MainBundle\Entity\Biomass
*
* @ORM\Table(name="biomass")
* @ORM\Entity
*/
class Biomass {
/**
* @var integer $id
*
* @ORM\Column(name="biomass_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var Field
*
* @ManyToOne(targetEntity="Field", inversedBy="biomasses")
* @JoinColumn(name="field_id", referencedColumnName="field_id")
*/
private $field;
/**
* Get id
*
* @return string
*/
public function getId() {
return $this->id;
}
/**
* Set field
*
* @param Field $field
* @return Biomass
*/
public function setField(Field $field = null) {
//$field->addBiomass($this);
$this->field = $field;
return $this;
}
/**
* Get field
*
* @return Field
*/
public function getField() {
return $this->field;
}
function __toString() {
return $this->id;
}
}
FieldController.php
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository("AcmeMainBundle:Field")->findAll();
foreach($entities as $field) {
foreach($field->getBiomasses() as $biomass) {
print_r($biomass->getId());
}
}
结果如下:警告:在C:[...] \ MainBundle \ Controller \ FieldController.php第39行中为foreach()提供的参数无效
没有创建ArrayCollecion,因为Doctrine不调用构造函数,它使用序列化/反序列化技巧来实例化类。
我在这些表中有虚拟数据并且数据是可访问的,因此似乎主义根本没有创建生物质对象。
环境 Symfony-2.1.2,Apache / 2.4.3(Win64),PHP / 5.4.6-Win64,MySQL社区服务器5.5.28
编辑:修复Biomass.field引用列名称拼写错误。
答案 0 :(得分:2)
在BioMass.php中,@ JoinColumn应指向BioMass表中的 field_id 而不是 id 。
/**
* @var Field
*
* @ManyToOne(targetEntity="Field", inversedBy="biomasses")
* @JoinColumn(name="field_id", referencedColumnName="field_id")
*/