El tema es que cuando intento trabajar con dos entidade relacionadas me peta el "invento" con este error.
Código PHP:
An exception occurred while executing 'INSERT INTO Branch (book_id, parent_branch, phrase, creator_uid, level) VALUES (?, ?, ?, ?, ?)' with params [{}, null, "Write a blog post", 4, 1]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'parent_branch' cannot be null
Código en cuestión
DefaultController.php
Código PHP:
public function createBranchAction(Request $request, $book_id, $parent_branch_id) {
$em = $this->getDoctrine()->getManager();
$parent_branch = $em->getRepository('BranchMainBundle:Branch')->find(array(
'id' => $parent_branch_id,
));
// Actual book
$actual_book = $em->getRepository('BranchMainBundle:Book')->find($book_id);
// Presist book
$em->persist($actual_book);
$parent_level = $parent_branch->getLevel();
if ($parent_level <= 0){
$level = 1;
}else{
$level = $parent_level - 1;
}
// Create new child branch.
$branch = new Branch();
$branch->setPhrase('Write a blog post');
$branch->setBookId($actual_book);
$branch->setParentBranch($parent_branch);
$branch->setBookId($actual_book);
$branch->setLevel($level);
// $book->setDueDate(new \DateTime('tomorrow'));
$branch->setCreatorUid(1);
$form = $this->createForm(new NewPhrase(), $branch);
$form->handleRequest($request);
if ($form->isValid()) {
// Save new branch in db.
$em = $this->getDoctrine()->getManager();
$em->persist($branch);
$em->flush();
//return $this->redirect($this->generateUrl('task_success'));
$Message = new Messages;
return $Message->successAction();
}
// Default view.
return $this->render('BranchMainBundle:Default:new_branch.html.twig', array(
'form' => $form->createView(),
));
}
Branch.php
Código PHP:
/**
* Book
* @ORM\Table()
* @ORM\Entity(repositoryClass="Branch\MainBundle\Entity\BranchRepository")
*/
class Branch {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Id
*/
private $id;
/**
* @var intenger
*
* @ORM\Column(name="book_id", type="integer", nullable=false)
* @ORM\ManyToOne(targetEntity="Book", inversedBy="Branch")
*/
private $book_id;
/*
public function __construct($book_id) {
$this->book_id = $book_id; // The book id.
}
*/
/**
* @var integer
* @ORM\OneToOne(targetEntity="Branch")
* @ORM\Column(type="integer")
* @ORM\JoinColumn(name="parent_branch", referencedColumnName="id")
*/
private $parent_branch;
/**
* @var string
* @Assert\NotBlank()
* @Assert\Length(
* min = 10,
* max = 500,
* minMessage = "Your phrase must be at least {{ limit }} characters long",
* maxMessage = "Your phrase cannot be longer than {{ limit }} characters long"
* )
* @ORM\Column(name="phrase", type="string", length=255)
*/
private $phrase;
/**
* @var integer
* @Assert\NotBlank()
* @ORM\Column(name="creator_uid", type="integer")
*/
private $creatorUid;
/**
* @var integer
*
* @ORM\Column(name="level", type="integer")
*/
private $level;
/**
* Get book_id
*
* @return \Branch\MainBundle\Entity\Book
*/
public function getBookId() {
return $this->book_id;
}
/**
* Set phrase
*
* @param string $phrase
* @return Book
*/
public function setPhrase($phrase) {
$this->phrase = $phrase;
return $this;
}
/**
* Get phrase
*
* @return string
*/
public function getPhrase() {
return $this->phrase;
}
/**
* Set parentBranch
*
* @param integer $parentBranch
* @return Branch
*/
public function setParentBranch($parentBranch) {
$this->parentBranch = $parentBranch;
return $this;
}
/**
* Get parentBranch
*
* @return integer
*/
public function getParentBranch() {
return $this->parentBranch;
}
/**
* Set creatorUid
*
* @param integer $creatorUid
* @return Branch
*/
public function setCreatorUid($creatorUid) {
$this->creatorUid = $creatorUid;
return $this;
}
/**
* Get creatorUid
*
* @return integer
*/
public function getCreatorUid() {
return $this->creatorUid;
}
/**
* Set book_id
*
* @param \Branch\MainBundle\Entity\Book $bookId
* @return Branch
*/
public function setBookId(BranchMainBundleEntityBook $bookId = null) {
$this->book_id = $bookId;
return $this;
}
}