Symfony2.3 persist related object with foregin key

I am looking for a good way to transfer 2 objects to db via doctrine in symfony 2.3

class CatController extends Controller
{
/**
 * Creates a new Cat entity.
 *
 */
public function createAction(Request $request)
{
    $entity = new Cat();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity); <-- Split it or what ?
        $em->flush();

        return $this->redirect($this->generateUrl('cat_show', array('id' => $entity->getId())));
    }

    return $this->render('ViszmanCatBundle:Cat:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

      

when the form is validated I can get the post data and create 2 objects with that data, but I think there should be a clearer way to do this, the code above doesn't work the way I wanted, it only inserts the foreign key into the related entity when i do the following:

class CatType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text')
        ->add('meetings','collection',
                array('type' => new MeetingType(),
                        'allow_add' => true,
                        'allow_delete' => true,
                        'prototype' => true,
                )
        );
}

class MeetingType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $plDays = array('Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela');
    $builder
        ->add('meetingDay', 'choice', array('choices' => $plDays))
        ->add('meetingTime', 'time',)
        ->add('cat', 'entity', array('class' => 'ViszmanCatBundle:Cat', 'property' => 'name'))
    ;
}

      

objects: Cat

namespace Viszman\CatBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Cat
 *
 * @ORM\Table()
 * @ORM\Entity
*/
class Congregation
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @ORM\OneToMany(targetEntity="Viszman\CatBundle\Entity\Member", mappedBy="cat")
 */
private $members;

/**
 * @ORM\OneToMany(targetEntity="Viszman\CatBundle\Entity\Meeting", mappedBy="cat", cascade={"persist"})
 */
private $meetings;

public function __construct(){
    $this->members = new \Doctrine\Common\Collections\ArrayCollection();
    $this->meetings = new \Doctrine\Common\Collections\ArrayCollection();
}

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

/**
 * Set name
 *
 * @param string $name
 * @return Cat
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Add members
 *
 * @param \Viszman\CatBundle\Entity\Member $members
 * @return Cat
 */
public function addMember(\Viszman\CatBundle\Entity\Member $members)
{
    $this->members[] = $members;

    return $this;
}

/**
 * Remove members
 *
 * @param \Viszman\CatBundle\Entity\Member $members
 */
public function removeMember(\Viszman\CatBundle\Entity\Member $members)
{
    $this->members->removeElement($members);
}

/**
 * Get members
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getMembers()
{
    return $this->members;
}
/**
 * Add meetings
 *
 * @param \Viszman\CationBundle\Entity\Meeting $meetings
 * @return Cat
 */
public function addMeeting(\Viszman\CatBundle\Entity\Meeting $meetings)
{
    $this->meetings[] = $meetings;

    return $this;
}

/**
 * Remove meetings
 *
 * @param \Viszman\CatBundle\Entity\Meeting $meetings
 */
public function removeMeeting(\Viszman\CatBundle\Entity\Meeting $meetings)
{
    $this->meetings->removeElement($meetings);
}

/**
 * Get meetings
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getMeetings()
{
    return $this->meetings;
}

namespace Viszman\CatBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Meeting
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Meeting
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var integer
 *
 * @ORM\Column(name="meeting_day", type="smallint")
 */
private $meetingDay;


/**
 * @var \DateTime
 *
 * @ORM\Column(name="meeting_time", type="time")
 */
private $meetingTime;

/**
 * @ORM\ManyToOne(targetEntity="Viszman\CatBundle\Entity\Cat", inversedBy="meetings")
 * @ORM\JoinColumn(name="cat_id", referencedColumnName="id")
 */
private $cat;

public function __construct()
{
    $this->created = new \DateTime();
}


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

/**
 * Set meetingDay
 *
 * @param integer $meetingDay
 * @return Meeting
 */
public function setMeetingDay($meetingDay)
{
    $this->meetingDay = $meetingDay;

    return $this;
}

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

/**
 * Set cat
 *
 * @param \Viszman\CatBundle\Entity\Cat $cat
 * @return Member
 */
public function setCat(\Viszman\CatBundle\Entity\Cat $cat = null)
{
    $this->cat = $cat;

    return $this;
}

/**
 * Get cat
 *
 * @return \stdClass 
 */
public function getCat()
{
    return $this->cat;
}

/**
 * Set meetingTime
 *
 * @param \DateTime $meetingTime
 * @return Meeting
 */
public function setMeetingTime($meetingTime)
{
    $this->meetingTime = $meetingTime;

    return $this;
}

/**
 * Get meetingTime
 *
 * @return \DateTime 
 */
public function getMeetingTime()
{
    return $this->meetingTime;
}

      

this generates an inline form with unwanted data, i.e. in the section Meeting

I need to select Cat

but I dont want what I want is that the default appointment is bound to Cat for create, update. Do I need to change something in Cat

or Meeting

Entity? I don't know if I'm clear, sorry for my bad english

+3


source to share


1 answer


You need to remove this line in MeetingType:

->add('cat', 'entity', array('class' => 'ViszmanCatBundle:Cat', 'property' => 'name'))

      



Then, your Cat objects and your Meeting object (which you can find with the cat getMeetings method) are saved in yout Controller.

If you want both to be saved in one shot, take a look at cascading Doctrine objects.

0


source







All Articles