-1

Here my code have simple two class, when i'm trying to insert data by this. i can get some error on this..

First Class

    /**
     * @Entity @Table(name="dpf_post_template")
     */
    class DpfPostTemplate
    {
        /**
         * @Id @GeneratedValue @Column(type="integer")
         * @var string
         */
        private $id;

        /**
         * @Column(type="string")
         * @var string
         */
        private $NAME;


        /**
         * @Column(type="datetime")
         */
        private $CDATE;

        /**
         * @Column(type="datetime")
         */
        private $UDATE;

        /**
         * @Column(type="string")
         * @var string
         */
        private $CBY;


        /**
         * @Column(type="string")
         * @var string
         */
        private $UBY;


        /**
         * @Column(type="integer")
         * @var string
         */
        private $ISACTIVE;


        /**
        * @OneToMany(targetEntity="DpfPostTemplateAssigned", mappedBy="assignedProperty")
        * @var asProp[]
        */
        private $assignedProperties = null;


        public function __construct(){
            $this->assignedProp = new ArrayCollection();
        }

         public function assignedProp($prop)
        {
            $this->assignedProperties[] = $prop;
        }

        public function getId(){
            return $this->id;
        }
        public function getName(){
            return $this->NAME;
        }
        public function setName($name){
            $this->NAME = $name;
        }
        public function setCDate(DateTime $date){
            $this->CDATE = $date;
        }
        public function setUDate(DateTime $date){
            $this->UDATE = $date;
        }
        public function setIsActive($act){
            $this->ISACTIVE = $act;
        }
        public function setCBy($cby){
            $this->CBY = $cby;
        }
        public function setUBy($uby){
            $this->UBY = $uby;
        }
    }

Second Class

    use Doctrine\Common\Collections\ArrayCollection;
    /**
     * @Entity @Table(name="dpf_post_template_assigned")
     */
    class DpfPostTemplateAssigned
    {
        /**
         * @Id @GeneratedValue @Column(type="integer")
         * @var string
         */
        private $id;

        /**
         * @Column(type="integer")
         * @var string
         */
        private $POSTID;

        /**
         * @Column(type="integer")
         * @var string
         */
        private $PROPID;

        /**
         * @Column(type="string")
         * @var string
         */
        private $TEMPLATE;

        /**
         * @Column(type="string")
         * @var string
         */
        private $NAME;

        /**
         * @Column(type="string")
         * @var string
         */
        private $SIZE;

        /**
         * @Column(type="integer")
         * @var string
         */
        private $ISACTIVE;

        /**
         * @ManyToOne(targetEntity="DpfPostTemplate", inversedBy="assignedProperties")
         */
        protected $assignedProperty;

        public function getId(){
            return $this->id;
        }

        public function getName(){
            return $this->NAME;
        }
        public function setName($name){
            $this->NAME = $name;
        }
        public function setPost($post)
        {
            $post->assignedProp($this);
            $this->assignedProperty = $post;
        }
    }

Save operations

    $post = new DpfPostTemplate();
    $post->setName("dinesh");
    $post->setCDate(new DateTime("Now"));
    $post->setUDate(new DateTime("Now"));
    $post->setCBy("dinesh");
    $post->setUBy("dinesh");
    $post->setIsActive(1);
    //$em->persist($post);
    //$em->flush();

    $prop = new DpfPostTemplateAssigned();
    $prop->setName("dinesh");
    $prop->setPost($post);
    $em->persist($prop);
    $em->flush();

When run this code i'm getting error like this.....

Fatal error: Uncaught exception 'Doctrine\ORM\ORMInvalidArgumentException' with message 'A new entity was found through the relationship 'DpfPostTemplateAssigned#assignedProperty' that was not configured to cascade persist operations for entity: DpfPostTemplate@00000000149c8a9f0000000036fa642b. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'DpfPostTemplate#__toString()' to get a clue.' in C:\xampp\htdocs\dpf\vendor\doctrine\orm\lib\Doctrine\ORM\ORMInvalidArgumentException.php:91 Stack trace: #0 C:\xampp\htdocs\dpf\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(797): Doctrine\ORM\ORMInvalidArgumentException::newEntityFoundThroughRelationship(Array, Object(DpfPostTemplate)) #1 C:\xampp\htdocs\dpf\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(679): Doctrine\ORM\UnitOfWork->computeAssociationChanges in C:\xampp\htdocs\dpf\vendor\doctrine\orm\lib\Doctrine\ORM\ORMInvalidArgumentException.php on line 91

zzlalani
  • 22,960
  • 16
  • 44
  • 73
dinesh Kumar
  • 61
  • 4
  • 16

1 Answers1

0

The Doctrine had said to you, what you must to do:

Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"})

Your code must looks like this:

    /**
     * @ManyToOne(targetEntity="DpfPostTemplate", inversedBy="assignedProperties", cascade={"persist", "remove"})
     */
    protected $assignedProperty;

Read this, please, for getting more information: Doctrine Docs

Nick Tsarev
  • 103
  • 1
  • 1
  • 10
  • Thanq its working.... But i had another doubt is how to insert mulitple DpfPostTemplateAssigned with DpfPostTemplate. please help me... – dinesh Kumar Nov 15 '13 at 09:03
  • If you speak about ManyToMany, try to read this: http://stackoverflow.com/questions/12743630/symfony2-doctrine-many-to-many-bidirectional-save-form-type/12744962#12744962 or give to me more specific – Nick Tsarev Nov 15 '13 at 11:42