Jaimin
Jaimin

Reputation: 811

one to many relationship problem in doctrine 2.0

I have an events table and events images table. I am having a form to add new event so i am also adding multiple images for that event. So that i am storing in event images table.

so how do i insert the data in both the tables..

<?php
/**
 * @Entity
 * @Table(name="events")
 */
class Default_Model_Event
{
     /**
     * @Id @Column(name="id", type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;


    /** 
    * @Column(name="title", type="string") 
    */
    private $title;


    /**
     * @OneToMany(targetEntity="Default_Model_EventImages" mappedBy="eventimage_mapper") 
     */
    private $images_mapper;


}//end class

Event images model

<?php
/**
 * @Entity
 * @Table(name="event_images")
 */
class Default_Model_EventImages
{
     /**
     * @Id @Column(name="id", type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** 
    * @Column(name="name", type="string") 
    */
    private $name;

    /**
     * @ManyToOne(targetEntity="Default_Model_Event", inversedBy="images_mapper")
     * @JoinColumn(name="event_id", referencedColumnName="id")
     */
    private $eventimage_mapper;



}//end class

From my controller how do i save the data now.. I am thinking of first saving the event data and than will get that id and than will save the events images by loop.. than i am not getting what will be the use of the one to many mapper i have created..

I am using zend framework 1.11 and Doctrine 2.0

Upvotes: 1

Views: 623

Answers (1)

Gedrox
Gedrox

Reputation: 3612

At first you need to create getters, setters to your objects. Then you just create

$event = new Default_Model_Event();
$em->persist($event);

while (something) {
  $image = new Default_Model_EventImages();
  $image->setEventimage_mapper($event);
  $em->persist($image);
}

$em->flush();

Just read the Doctrine documentation on associations. It will help for sure.

Upvotes: 1

Related Questions