Buenas tardes, de nuevo vengo con un problemilla que llevo un buen rato dándole vuelta. Lo mejor creo es pegar el código y ahora explico cual es mi problema.
Tengo una entidad
Branch.php Código PHP:
<?php
namespace BranchMainBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
/**
* Book
* @ORM\Table()
* @ORM\Entity(repositoryClass="Branch\MainBundle\Entity\BranchRepository")
*/
class Branch {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var intenger
*
* @ORM\Column(name="book_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Book", inversedBy="Branch")
* @ORM\JoinColumn(name="book_id", referencedColumnName="id")
*/
private $book_id;
public function __construct($id, $book_id) {
$this->id = $id; // The branch 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 $parentBranch;
/**
* @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;
/**
* 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;
}
public function __toString() {
return $this->phrase;
}
Y lo que quiero es conseguir una clave primaria compuesta; en la bd he establecido dos campos como claves primarias id, y book_id (esto sería la id de branch+ la id de book). Algo debo hacer mal, porque obtengo un error.
Pongo también el controlador por si acaso.
DefaultController.php
Código PHP:
<?php
namespace BranchMainBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use BranchMainBundleEntityBranch;
use BranchMainBundleEntityBook;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use BranchMainBundleFormPhraseNewPhrase;
use BranchMainBundleFormBookNewBook;
// Custom messages.
use BranchMainBundleMessagesMessages;
class DefaultController extends Controller {
public function createBranchAction(Request $request, $book_id, $parent_branch_id) {
$em = $this->getDoctrine()->getManager();
// Parent branch.
$parent_branch = $em->getRepository('BranchMainBundle:Branch')->find($parent_branch_id, $book_id);
// Presist parent branch
$em->persist($parent_branch);
// Actual book
$actual_book = $em->getRepository('BranchMainBundle:Book')->find($book_id);
// Presist book
$em->persist($actual_book);
// Create new child branch.
$branch = new Branch();
$branch->setPhrase('Write a blog post');
$branch->setParentBranch($parent_branch);
$branch->setBookId($actual_book);
// $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(),
));
}
function createBookAction(Request $request) {
$book = New Book();
$book->setTitle('Inserta un título');
$book->setDescription('');
$book->setPublic(1);
$book->setOwner(1);
$em = $this->getDoctrine()->getManager();
$em->persist($book);
$form = $this->createForm(new NewBook(), $book);
$form->handleRequest($request);
if ($form->isValid()) {
// Save new book in db.
$em = $this->getDoctrine()->getManager();
$em->persist($book);
$em->flush();
return $this->redirect($this->generateUrl('task_success'));
}
// Default view.
return $this->render('BranchMainBundle:Default:new_book.html.twig', array(
'form' => $form->createView(),
));
}
}
El
routing.yml sería este:
Código PHP:
book_main_homepage:
path: /books/{book}
defaults: { _controller: BranchMainBundle:Default:index, book: 0 }
book_create_branch:
path: /create-branch/{book_id}/{parent_branch_id}
defaults: { _controller: BranchMainBundle:Default:createBranch, book_id: 1, parent_branch_id : 1}
book_create_book:
path: /create-book
defaults: { _controller: BranchMainBundle:Default:createBook}
task_success:
path: /success
defaults: { _controller: BranchMainBundle:Default:success }