Ver Mensaje Individual
  #1 (permalink)  
Antiguo 21/09/2011, 15:51
MichaelParra
 
Fecha de Ingreso: enero-2009
Mensajes: 24
Antigüedad: 15 años, 3 meses
Puntos: 1
Doctrine 2 Symfony2

Hola, que tal ¿?

Estoy haciendo un proyecto en Symfony2.

con Symfony2 y Twig todo muy bonito :)

pero el problema va más por el lado de Doctrine 2.1

Leyendo un poco las asociaciones, me estoy complicando un poco...

Por ejemplo:

Tengo dos Tablas: y la asociación se Bidireccional.

Entidad PostCategory

Código PHP:

namespace LmsPostBundleEntity
;

use 
DoctrineORMMapping as ORM;
use 
SymfonyComponentValidatorConstraints as Assert;
use 
DoctrineCommonCollectionsArrayCollection;

/**
 * Lms\PostBundle\Entity
 *
 * @ORM\Table(name="postcategory")
 * @ORM\Entity(repositoryClass="Lms\PostBundle\Entity\PostCategoryRepository")
 */
class PostCategory
{

   
/**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="IDENTITY")
    */
    
protected $id;

   
/**
    * @ORM\Column(type="string")
    */
    
protected $name;
    
    
   
/**
    * @ORM\OneToMany(targetEntity="Post", mappedBy="postcategory")
    */
    
protected $posts;
    
    public function 
__construct()
    {
        
$this->posts = new ArrayCollection();
    }


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

    
/**
     * Set name
     *
     * @param datetime $name
     */
    
public function setName($name)
    {
        
$this->name $name;
    }

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

    
/**
     * Add posts
     *
     * @param Lms\PostBundle\Entity\Post $posts
     */
    
public function addPost(LmsPostBundleEntityPost $posts)
    {
        
$this->posts[] = $posts;
    }

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

Entidad Post

Código PHP:


namespace LmsPostBundleEntity
;

use 
DoctrineORMMapping as ORM;
use 
SymfonyComponentValidatorConstraints as Assert;
use 
DoctrineCommonCollectionsArrayCollection;
use 
SymfonyComponentValidatorMappingClassMetadata;
use 
SymfonyComponentValidatorConstraintsNotBlank;


/**
 * Lms\PostBundle\Entity
 *
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="Lms\PostBundle\Entity\PostRepository")
 */
class Post
{

   
/**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="IDENTITY")
    */
    
protected $idpost;
    
   
/**
    *
    * @ORM\Column(type="string") 
    */
    
protected $subject;
    
    
/**
    * @ORM\Column(type="integer")
    */  
    
protected $idpostcategory;
    
    
/**
     * @ORM\ManyToOne(targetEntity="PostCategory", inversedBy="posts", cascade={"remove"})
     * @ORM\JoinColumn(name="idpostcategory", referencedColumnName="id")
     */
    
protected $postcategory;
    

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

    
/**
     * Set subject
     *
     * @param string $subject
     */
    
public function setSubject($subject)
    {
        
$this->subject $subject;
    }

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


    

    
/**
     * Set idpostcategory
     *
     * @param integer $idpostcategory
     */
    
public function setIdpostcategory($idpostcategory)
    {
        
$this->idpostcategory $idpostcategory;
    }

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

    
/**
     * Set postcategory
     *
     * @param Lms\PostBundle\Entity\PostCategory $postcategory
     */
    
public function setPostcategory(LmsPostBundleEntityPostCategory $postcategory)
    {
        
$this->postcategory $postcategory;
    }

    
/**
     * Get postcategory
     *
     * @return Lms\PostBundle\Entity\PostCategory 
     */
    
public function getPostcategory()
    {
        return 
$this->postcategory;
    }


En ese efecto el Inner Join con DQL me funciona perfecto !
La insercción de las dos tablas me funciona perfecto también!

Por ejemplo:

Código PHP:

       $categories 
= new PostCategory();
        
$categories->setName('Nueva Categoria !');
        
        
$post = new Post();
        
$post->setSubject('Nuevo Post');
        
        
$post->setPostcategory($categories);

        
$em $this->get('doctrine')->getEntityManager();
        
$em->persist($categories);
        
$em->persist($post);
        
$em->flush(); 
        
        echo 
$post->getIdpost();
        
        exit(); 
1. Pero mi problema es, como inserto la tabla POST sin la necesidad de insertar la tabla Padre, PostCategory;

2. Hay alguna manera más fácil de trabajar o este es el único modo?

3. Al final los beneficios son grandes? me refiero al tiempo, performance, rendimiento.

4. eh tratado de insertar la tabla Post de todas formas pero no me sale, cual creen que sea el problema?