3

i'm looking for nice way to persist 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 form is validated i can get post data and create 2 objects with that data but i think there should be clearer way to do this, above code is working not as i wanted, it only inserts foreign key to related entity when i do this:

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'))
    ;
}

entities: 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 generate embedded form with unwanted data, meaning in Meeting section i need to choice Cat, but i dont want to, what i want is that meeting is on default attached to Cat on create, update. Do i need to change something in Cat or Meeting Entity? I don't know if i'm clear, sorry for my poor english

Viszman
  • 1,378
  • 1
  • 17
  • 47

1 Answers1

0

You need to remove this line in the MeetingType:

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

Then in yout Controller persist your Cat entity and yout Meeting entity (which you can find using cat's getMeetings method).

If you want both to be persisted in one shot, take a look at the cascade operation for Doctrine entities.

AntoineWDG
  • 539
  • 2
  • 11