这是一个简单的问题,但在经历了Stackoverflow和其他地方的所有类似线程都没有成功之后,问题仍然存在。
我无法建立双向的一对多关系。Biomass.field是可访问的(拥有方),但field.Biomass(相反方)不是。
表格(删除消耗性字段)
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 AcmeMainBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* AcmeMainBundleEntityField
*
* @ORMTable(name="field")
* @ORMEntity
*/
class Field {
public function __construct() {
$this->biomasses = new ArrayCollection();
}
/**
* @var string $id
*
* @ORMColumn(name="field_id", type="string", length=255, nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/*
* var ArrayCollection $biomasses
*
* @ORMOneToMany(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();
}
}
Biomas.php
<?php
namespace AcmeMainBundleEntity;
use DoctrineORMMapping as ORM;
/**
* AcmeMainBundleEntityBiomass
*
* @ORMTable(name="biomass")
* @ORMEntity
*/
class Biomass {
/**
* @var integer $id
*
* @ORMColumn(name="biomass_id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(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不调用构造函数,它使用序列化/非序列化技巧来实例化类。
我在这些表中有伪数据,这些数据是可以访问的,所以看起来原理根本没有创建biomass对象。
环境Symfony2.1.2、Apache/2.4.3(Win64)、PHP/5.4.6-Win64、MySQL Community Server 5.5.28
编辑:修复了Biomass.field引用列名的拼写错误。
在BioMass.php中,@JoinColumn应指向BioMass表中的field_id,而不是id。
/**
* @var Field
*
* @ManyToOne(targetEntity="Field", inversedBy="biomasses")
* @JoinColumn(name="field_id", referencedColumnName="field_id")
*/