0

I am currently facing one problem with mapping I hope some one would helpe me. I have two table describe bellow.. unitid(primary_key, column_1) For example data here

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

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

For example data here

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

unitid has some kind of units defines while this mappaths table define the specific paths based on the unitid Now when I run the doctrain2 mapping It give me some kind these entities pluse these following mapping information

Entity class

<?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;
    }
}

and 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;
    }
}

now when I run the project it give me the following error

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. }

It seems to me that this primary key of unitsid could not get by mapping

Any solution would be appreciated

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
zahid
  • 113
  • 12

1 Answers1

0

First I think you haven't understand the ORM (object relational mapping) concept in your entities you don't map these relations by foreign keys, doctrine will do it for you, in the entities layer you have to map objects and only objects

Second you need to map your entities relations by using orm annotations or xml configuration to describe those relations if they are oneToOne or ManyToOne or OneToMany or ManyToMany relation

Third You are having this error because you haven't set a value to the entity id, to do that you have two options : auto generate the id or set it manually before persisting object

see this

Anas EL KORCHI
  • 2,008
  • 18
  • 25
  • thanks for your reply elkorchi. i alreay have these both file i means unitids.orm.yml and this mappaths.orm.yml. for boths file. the id there take the strategy:SEQUENCE... so you means i need to make it auto – zahid Jan 06 '16 at 09:33
  • I think so check this : http://stackoverflow.com/questions/14022374/the-differences-between-generatedvalue-strategies – Anas EL KORCHI Jan 06 '16 at 10:42
  • when i validate my schema they give the following error t ...... The referenced column name 'databaseid' has to be a primary key column on the target entity class 'AppBundle\Entity\Unitids'. .... in my mapping the reference column is not primary key it is just not nullable column..... – zahid Jan 06 '16 at 10:59
  • @ORM\JoinColumn(name="ref_unitids2", referencedColumnName="databaseid") Because of this a referencedColumn must be the column who has the id of the entity – Anas EL KORCHI Jan 06 '16 at 11:14