用Symfony 2作为外键的学说映射

时间:2016-01-06 08:40:42

标签: symfony doctrine

我目前面临着一个映射问题,我希望有人会帮助我。 我有两张表描述吼叫.. unitid(primary_key,column_1) 例如这里的数据

unitid(1,22)
unitid (2,33)

mappaths(primary key, forign_key (column_1), forign_key(column_1))

例如这里的数据

mappaths (1,22,33)
mappaths (2,33,22)

unitid有一些单元定义,而这个mappaths表定义了基于unitid的特定路径 现在,当我运行doctrain2映射时,它给了我一些这些实体来获取这些以下映射信息

实体类

<?php

namespace ApiMaps\ApiMapBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Mappaths
 *
 * @ORM\Table(name="mappaths", indexes={@ORM\Index(name="idx_45b7519fa3995845", columns={"ref_unitids1"}), @ORM\Index(name="idx_45b7519f3a9009ff", columns={"ref_unitids2"})})
 * @ORM\Entity
 */
class Mappaths
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="mappaths_id_seq", allocationSize=1, initialValue=1)
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="normalvalue", type="smallint", nullable=true)
     */
    private $normalvalue;

    /**
     *
     * @var \Unitids
     * @ORM\ManyToOne(targetEntity="Unitids",cascade={"all"},fetch="LAZY")
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="ref_unitids2", referencedColumnName="databaseid")
     * })
     */
    private $refUnitids2;

    /**
     * @var \Unitids
     * @ORM\ManyToOne(targetEntity="Unitids",cascade={"all"},fetch="LAZY")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="ref_unitids1", referencedColumnName="databaseid")
     * })
     */
    private $refUnitids1;



    /**
     * Set normalvalue
     *
     * @param integer $normalvalue
     *
     * @return Mappaths
     */
    public function setNormalvalue($normalvalue)
    {
        $this->normalvalue = $normalvalue;

        return $this;
    }

    /**
     * Get normalvalue
     *
     * @return integer
     */
    public function getNormalvalue()
    {
        return $this->normalvalue;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set refUnitids1
     *
     * @param \ApiMaps\ApiMapBundle\Entity\Unitids $refUnitids1
     *
     * @return Mappaths
     */
    public function setRefUnitids1(\ApiMaps\ApiMapBundle\Entity\Unitids $refUnitids1 = null)
    {
        $this->refUnitids1 = $refUnitids1;

        return $this;
    }

    /**
     * Get refUnitids1
     *
     * @return \ApiMaps\ApiMapBundle\Entity\Unitids
     */
    public function getRefUnitids1()
    {
        return $this->refUnitids1;
    }

    /**
     * Set refUnitids2
     *
     * @param \ApiMaps\ApiMapBundle\Entity\Unitids $refUnitids2
     *
     * @return Mappaths
     */
    public function setRefUnitids2(\ApiMaps\ApiMapBundle\Entity\Unitids $refUnitids2 = null)
    {
        $this->refUnitids2 = $refUnitids2;

        return $this;
    }

    /**
     * Get refUnitids2
     *
     * @return \ApiMaps\ApiMapBundle\Entity\Unitids
     */
    public function getRefUnitids2()
    {
        return $this->refUnitids2;
    }
}

和mappaths.php

<?php

namespace ApiMaps\ApiMapBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Mappaths
 *
 * @ORM\Table(name="mappaths", indexes={@ORM\Index(name="idx_45b7519fa3995845", columns={"ref_unitids1"}), @ORM\Index(name="idx_45b7519f3a9009ff", columns={"ref_unitids2"})})
 * @ORM\Entity
 */
class Mappaths
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="mappaths_id_seq", allocationSize=1, initialValue=1)
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="normalvalue", type="smallint", nullable=true)
     */
    private $normalvalue;

    /**
     *
     * @var \Unitids
     * @ORM\ManyToOne(targetEntity="Unitids",cascade={"all"},fetch="LAZY")
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="ref_unitids2", referencedColumnName="databaseid")
     * })
     */
    private $refUnitids2;

    /**
     * @var \Unitids
     * @ORM\ManyToOne(targetEntity="Unitids",cascade={"all"},fetch="LAZY")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="ref_unitids1", referencedColumnName="databaseid")
     * })
     */
    private $refUnitids1;



    /**
     * Set normalvalue
     *
     * @param integer $normalvalue
     *
     * @return Mappaths
     */
    public function setNormalvalue($normalvalue)
    {
        $this->normalvalue = $normalvalue;

        return $this;
    }

    /**
     * Get normalvalue
     *
     * @return integer
     */
    public function getNormalvalue()
    {
        return $this->normalvalue;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set refUnitids1
     *
     * @param \ApiMaps\ApiMapBundle\Entity\Unitids $refUnitids1
     *
     * @return Mappaths
     */
    public function setRefUnitids1(\ApiMaps\ApiMapBundle\Entity\Unitids $refUnitids1 = null)
    {
        $this->refUnitids1 = $refUnitids1;

        return $this;
    }

    /**
     * Get refUnitids1
     *
     * @return \ApiMaps\ApiMapBundle\Entity\Unitids
     */
    public function getRefUnitids1()
    {
        return $this->refUnitids1;
    }

    /**
     * Set refUnitids2
     *
     * @param \ApiMaps\ApiMapBundle\Entity\Unitids $refUnitids2
     *
     * @return Mappaths
     */
    public function setRefUnitids2(\ApiMaps\ApiMapBundle\Entity\Unitids $refUnitids2 = null)
    {
        $this->refUnitids2 = $refUnitids2;

        return $this;
    }

    /**
     * Get refUnitids2
     *
     * @return \ApiMaps\ApiMapBundle\Entity\Unitids
     */
    public function getRefUnitids2()
    {
        return $this->refUnitids2;
    }
}

现在当我运行项目时,它会给我以下错误

37.    public static function missingPrimaryKeyValue($className, $idField)
38.     {
39.         return new self(sprintf("Missing value for primary key %s on %s", $idField, $className));
40.     }
41. }

在我看来,unitid的这个主键无法通过映射获得

任何解决方案都将不胜感激

1 个答案:

答案 0 :(得分:0)

首先,我认为您不了解实体中的ORM(对象关系映射)概念,您不通过外键映射这些关系,doctrine将为您执行,在实体层中您必须映射对象和只有对象

其次,您需要使用orm注释或xml配置来映射实体关系,以描述这些关系,如果它们是oneToOne或ManyToOne或OneToMany或ManyToMany关系

第三个你有这个错误,因为你没有为实体id设置一个值,为此你有两个选择:自动生成id或在持久化对象之前手动设置它

请参阅this