Ver Mensaje Individual
  #8 (permalink)  
Antiguo 24/11/2014, 07:55
Avatar de Dundee
Dundee
 
Fecha de Ingreso: junio-2002
Ubicación: El Médano
Mensajes: 1.310
Antigüedad: 21 años, 10 meses
Puntos: 8
Respuesta: Alguna aplicación de ejemplo con varias relaciones

Gracias por tu respuesta, de hecho lo hice así, pero el error que tengo es este:

Código PHP:
Single id is not allowed on composite primary key in entity BranchMainBundleEntityBranch 
Branch.php
Código PHP:
<?php
namespace BranchMainBundleEntity
;
use 
DoctrineORMMapping as ORM;
use 
DoctrineORMQuery;
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\GeneratedValue(strategy="AUTO")
   * @ORM\Id
   */
  
private $id;

  
/**
   * @var intenger
   * 
   * @ORM\Column(name="book_id", type="integer", nullable=false)
   * @ORM\Id
   * @ORM\ManyToOne(targetEntity="Book", inversedBy="Branch")
   */
  
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 $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;
  }

  public function 
__toString() {
    return 
$this->phrase;
  }

    
/**
     * Set id
     *
     * @param integer $id
     * @return Branch
     */
    
public function setId($id)
    {
        
$this->id $id;

        return 
$this;
    }

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

    
/**
     * Set level
     *
     * @param integer $level
     * @return Branch
     */
    
public function setLevel($level)
    {
        
$this->level $level;

        return 
$this;
    }

    
/**
     * Get level
     *
     * @return integer 
     */
    
public function getLevel()
    {
        return 
$this->level;
    }
}
Código PHP:
<?php
[B]
Book.php[/B]
namespace BranchMainBundleEntity;

use 
DoctrineORMMapping as ORM;
use 
DoctrineORMQuery;
use 
DoctrineCommonCollectionsArrayCollection;

/**
 * Book
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Branch\MainBundle\Entity\BookRepository")
 */
class Book {

  
/**
   * @ORM\OneToMany(targetEntity="Branch", mappedBy="book")
   */
  
protected $branches;

  public function 
__construct() {
    
$this->branches = new ArrayCollection();
  }

  
/**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   * @ORM\OneToMany(targetEntity="Branch", mappedBy="Book")
   */
  
private $id;

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

  
/**
   * @var string
   *
   * @ORM\Column(name="description", type="text")
   */
  
private $description;

  
/**
   * @var integer
   *
   * @ORM\Column(name="public", type="integer")
   */
  
private $public;
  
  
  
/**
   * @var integer
   *
   * @ORM\Column(name="maxLevel", type="integer")
   */
  
private $maxLevel;

  
/**
   * Set title
   *
   * @param string $title
   * @return Book
   */
  
public function setTitle($title) {
    
$this->title $title;

    return 
$this;
  }

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

  
/**
   * Set description
   *
   * @param string $description
   * @return Book
   */
  
public function setDescription($description) {
    
$this->description $description;

    return 
$this;
  }

  
/**
   * @return string 
   */
  
public function getDescription() {
    return 
$this->description;
  }

  
   * 
Set public
   *
   * @
param integer $public
   
* @return Book
   
*/
  public function 
setPublic($public) {
    
$this->public $public;

    return 
$this;
  }

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

  
/**
   * Set owner
   *
   * @param integer $owner
   * @return Book
   */
 

  
public function __toString() {
    return 
$this->title;
  }

     * @
param BranchMainBundleEntityBranch $branches
     
* @return Book
     
*/
    public function 
addBranch(BranchMainBundleEntityBranch $branches)
    {
        
$this->branches[] = $branches;

        return 
$this;
    }

    
/**
     * @param \Branch\MainBundle\Entity\Branch $branches
     */
    
public function removeBranch(BranchMainBundleEntityBranch $branches)
    {
        
$this->branches->removeElement($branches);
    }

    
/**
     * @return \Doctrine\Common\Collections\Collection 
     */
    
public function getBranches()
    {
        return 
$this->branches;
    }

    
/**
     * @return integer 
     */
    /**
     * @param integer $maxLevel
     * @return Book
     */
    
public function setMaxLevel($maxLevel)
    {
        
$this->maxLevel $maxLevel;

        return 
$this;
    }

    
/**
     * @return integer 
     */
    
public function getMaxLevel()
    {
        return 
$this->maxLevel;
    }
}

Y el 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 
indexAction() {


    return 
$this->render('BranchMainBundle:Default:index.html.twig');
  }

  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
     
'book_id' => $book_id
       
));
   
    
$actual_book $em->getRepository('BranchMainBundle:Book')->find($book_id);
    
$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(),
    ));
  }



}
__________________
Videotutoriales de Drupal