0

这是一个简单的问题,但是在通过 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;

字段.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();
    }
}

生物质.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 Community Server 5.5.28

编辑:固定 Biomass.field 引用列名拼写错误。

4

1 回答 1

2

在 BioMass.php 中,@JoinColumn 应指向BioMass表中的 field_id而不是id

 /**
 * @var Field
 *
 * @ManyToOne(targetEntity="Field", inversedBy="biomasses")
 * @JoinColumn(name="field_id", referencedColumnName="field_id")
 */
于 2012-10-11T20:33:57.273 回答