如何将数据保存到表Boxer但不保存到表Person?

时间:2015-01-12 13:37:35

标签: php symfony class inheritance

我已经在我的问题上工作了7天但我无法找到解决方案。问题是:为什么拳击手的数据会保存到桌子而不是Boxer?

拳击手的数据模型;

<?php

namespace boxebd\BoxingBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Boxer
 *
 * @ORM\Table(name = "boxer")
 * @ORM\Entity
 */
class Boxer extends Person
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="TABLE")
 */
private $id;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="career_start", type="date")
 */
private $careerStart;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="career_end", type="date")
 */
private $careerEnd;


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

/**
 * Set careerStart
 *
 * @param \DateTime $careerStart
 * @return Boxer
 */
public function setCareerStart($careerStart)
{
    $this->careerStart = $careerStart;

    return $this;
}

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

/**
 * Set careerEnd
 *
 * @param \DateTime $careerEnd
 * @return Boxer
 */
public function setCareerEnd($careerEnd)
{
    $this->careerEnd = $careerEnd;

    return $this;
}

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

public function __toString() {
    parent::__toString() ." ". $this->careerStart;
}
}

人员数据模型:     

namespace boxebd\BoxingBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Person
 *
 * @ORM\Table(name="person")
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="class_name", type="string")
 * @ORM\DiscriminatorMap({"person" = "Person", "boxer" = "Boxer"})
 */
class Person
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

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

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

/**
 * @var \DateTime
 *
 * @ORM\Column(name="dob", type="date")
 */
private $dob;


public function setId($id){
    $this->id = $id;

    return $this;
}

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

/**
 * Set firstname
 *
 * @param string $firstname
 * @return Person
 */
public function setFirstname($firstname)
{
    $this->firstname = $firstname;

    return $this;
}

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

/**
 * Set lastname
 *
 * @param string $lastname
 * @return Person
 */
public function setLastname($lastname)
{
    $this->lastname = $lastname;

    return $this;
}

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

/**
 * Set dob
 *
 * @param \DateTime $dob
 * @return Person
 */
public function setDob($dob)
{
    $this->dob = $dob;

    return $this;
}

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

public function __toString(){
    return $this->firstname." ".$this->lastname;
}
}

我不会发布PersonController和PersonType的代码,因为我相信他们的代码是正确的。我认为错误可能出现在BoxerController或BoxerType中。

BoxerController:

<?php

namespace boxebd\BoxingBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use boxebd\BoxingBundle\Entity\Boxer;
use boxebd\BoxingBundle\Form\BoxerType;

/**
 * Boxer controller.
 *
 * @Route("/boxer")
 */
class BoxerController extends Controller
{

/**
 * Lists all Boxer entities.
 *
 * @Route("/", name="boxer")
 * @Method("GET")
 * @Template()
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('boxebdBoxingBundle:Boxer')->findAll();

    return array(
        'entities' => $entities,
    );
}
/**
 * Creates a new Boxer entity.
 *
 * @Route("/", name="boxer_create")
 * @Method("POST")
 * @Template("boxebdBoxingBundle:Boxer:new.html.twig")
 */
public function createAction(Request $request)
{
    $entity = new Boxer();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

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

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

/**
 * Creates a form to create a Boxer entity.
 *
 * @param Boxer $entity The entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createCreateForm(Boxer $entity)
{
    $form = $this->createForm(new BoxerType(), $entity, array(
        'action' => $this->generateUrl('boxer_create'),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;
}

/**
 * Displays a form to create a new Boxer entity.
 *
 * @Route("/new", name="boxer_new")
 * @Method("GET")
 * @Template()
 */
public function newAction()
{
    $entity = new Boxer();
    $form   = $this->createCreateForm($entity);

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

/**
 * Finds and displays a Boxer entity.
 *
 * @Route("/{id}", name="boxer_show")
 * @Method("GET")
 * @Template()
 */
public function showAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('boxebdBoxingBundle:Boxer')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Boxer entity.');
    }

    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity'      => $entity,
        'delete_form' => $deleteForm->createView(),
    );
}

/**
 * Displays a form to edit an existing Boxer entity.
 *
 * @Route("/{id}/edit", name="boxer_edit")
 * @Method("GET")
 * @Template()
 */
public function editAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('boxebdBoxingBundle:Boxer')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Boxer entity.');
    }

    $editForm = $this->createEditForm($entity);
    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
}

/**
* Creates a form to edit a Boxer entity.
*
* @param Boxer $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Boxer $entity)
{
    $form = $this->createForm(new BoxerType(), $entity, array(
        'action' => $this->generateUrl('boxer_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}
/**
 * Edits an existing Boxer entity.
 *
 * @Route("/{id}", name="boxer_update")
 * @Method("PUT")
 * @Template("boxebdBoxingBundle:Boxer:edit.html.twig")
 */
public function updateAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('boxebdBoxingBundle:Boxer')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Boxer entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createEditForm($entity);
    $editForm->handleRequest($request);

    if ($editForm->isValid()) {
        $em->flush();

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

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
}
/**
 * Deletes a Boxer entity.
 *
 * @Route("/{id}", name="boxer_delete")
 * @Method("DELETE")
 */
public function deleteAction(Request $request, $id)
{
    $form = $this->createDeleteForm($id);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('boxebdBoxingBundle:Boxer')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Boxer entity.');
        }

        $em->remove($entity);
        $em->flush();
    }

    return $this->redirect($this->generateUrl('boxer'));
}

/**
 * Creates a form to delete a Boxer entity by id.
 *
 * @param mixed $id The entity id
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createDeleteForm($id)
{
    return $this->createFormBuilder()
        ->setAction($this->generateUrl('boxer_delete', array('id' => $id)))
        ->setMethod('DELETE')
        ->add('submit', 'submit', array('label' => 'Delete'))
        ->getForm()
    ;
}
}

BoxerType:

<?php

 namespace boxebd\BoxingBundle\Form;

 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormBuilderInterface;
 use Symfony\Component\OptionsResolver\OptionsResolverInterface;

 class BoxerType extends AbstractType
 {
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('careerStart')
        ->add('careerEnd')
        ->add('id','entity',array('class'=>'boxebdBoxingBundle:Person'))
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'boxebd\BoxingBundle\Entity\Boxer'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'boxebd_boxingbundle_boxer';
}
}

我可以成功创建一个新的Person条目但我无法创建一个新的Boxer条目,因为消息错误说:

  

执行&#39; INSERT INTO person(firstname,lastname,dob,class_name)VALUES(?,?,?,?)&#39;使用params [null,null,null,&#34; boxer&#34;]:

     

SQLSTATE [23000]:违反完整性约束:1048列&#39;名字&#39;不能为空&#34;

该消息没有意义,因为我已经有两个Person条目,而且我不想创建另一个Person条目。我想要的是使用人员ID创建一个新的Boxer条目到一个新的Boxer条目。我在O / R Mapping上使用类表继承。

0 个答案:

没有答案