使用OneToOne关系在外键上的空值

时间:2016-10-11 05:56:08

标签: doctrine-orm doctrine symfony

除了Resume表中person_id上的NULL值之外,代码正在运行。 我无法弄清楚如何设置它的价值。 我试试这个:

$resume->setPerson($person); --> not working.

\的appbundle \实体\人

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

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

/**
 * @ORM\OneToOne(targetEntity="Resume", mappedBy="person", cascade={"persist", "remove"})
 */
private $resume;

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

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

    return $this;
}

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

/**
 * Set resume
 *
 * @param \AppBundle\Entity\Resume $resume
 *
 * @return Person
 */
public function setResume(\AppBundle\Entity\Resume $resume = null)
{
    $this->resume = $resume;

    return $this;
}

/**
 * Get resume
 *
 * @return \AppBundle\Entity\Resume
 */
public function getResume()
{
    return $this->resume;
}
}

\的appbundle \实体\恢复

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

/**
 * @ORM\Column(type="string", length=100)
 */
private $address;

/**
 * @ORM\OneToOne(targetEntity="Person", inversedBy="resume")
 * @ORM\JoinColumn(name="person_id", referencedColumnName="id")
 */
private $person;

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

/**
 * Set address
 *
 * @param string $address
 *
 * @return Resume
 */
public function setAddress($address)
{
    $this->address = $address;

    return $this;
}

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

/**
 * Set person
 *
 * @param \AppBundle\Entity\Person $person
 *
 * @return Resume
 */
public function setPerson(\AppBundle\Entity\Person $person = null)
{
    $this->person = $person;

    return $this;
}

/**
 * Get person
 *
 * @return \AppBundle\Entity\Person
 */
public function getPerson()
{
    return $this->person;
}
}

\的appbundle \控制器\ PersonController

class PersonController extends Controller
{
/**
 * @Route("/person/new", name="person_new")
 *
 */
public function createProductAction(Request $request)
{
    $person = new Person();

    $resume = new Resume();
    $resume->setPerson($person);


    $form = $this->createForm(PersonType::class, $person);

    $form->handleRequest($request);

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

    return $this->render(
        'person/new.html.twig',
        array(
            'form' => $form->createView(),
        )
    );
}

/person/new.html.twig

{% extends 'base.html.twig' %}

{% block body %}

{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="btn btn-primary">Save</button>
{{ form_end(form) }}

{% endblock %}

1 个答案:

答案 0 :(得分:0)

$resume->setPerson($this);中添加setResume。现在你没有任何代码可以更新一个人的简历,因为你在控制器中编辑人(而不是简历)。

/**
 * Set resume
 *
 * @param \AppBundle\Entity\Resume $resume
 *
 * @return Person
 */
public function setResume(\AppBundle\Entity\Resume $resume = null)
{
    $resume->setPerson($this);
    $this->resume = $resume;

    return $this;
}