DDC-1387: Doctrine 2 Symfony2 #1735

Closed
opened 2026-01-22 13:23:45 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Sep 21, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user michel.hinostroza:

Hello that such??

I'm doing a project in Symfony2.

Twig with Symfony2 and all very nice:)

but the problem is more one of Doctrine 2.1

Reading some associations, I'm a bit complicated ...

For example:

I have two tables, and the association is bidirectional.

entity PostCategory

/*********************************************************************************/


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; 
    } 
}  


/*********************************************************************************/

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; 
    } 
}  


/*******************************************************************************************************/

In that effect, the Inner Join with me DQL works perfect!
The insertion of the two tables perfect work for me too!

For example:

 $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. But my problem is, as the table insert POST without the need to insert the table Father PostCategory;
  2. There is an easier way to work or this is the only way?
  3. In the end the benefits are great? I mean the time, performance, performance.
  4. eh tried to insert the table anyway but Post does not come out, which they believe to be the problem?
Originally created by @doctrinebot on GitHub (Sep 21, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user michel.hinostroza: Hello that such?? I'm doing a project in Symfony2. Twig with Symfony2 and all very nice:) but the problem is more one of Doctrine 2.1 Reading some associations, I'm a bit complicated ... For example: I have two tables, and the association is bidirectional. entity PostCategory ``` /*********************************************************************************/ 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; } } /*********************************************************************************/ 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; } } /*******************************************************************************************************/ ``` In that effect, the Inner Join with me DQL works perfect! The insertion of the two tables perfect work for me too! For example: ``` $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. But my problem is, as the table insert POST without the need to insert the table Father PostCategory; 2. There is an easier way to work or this is the only way? 3. In the end the benefits are great? I mean the time, performance, performance. 4. eh tried to insert the table anyway but Post does not come out, which they believe to be the problem?
admin added the Bug label 2026-01-22 13:23:45 +01:00
admin closed this issue 2026-01-22 13:23:46 +01:00
Author
Owner

@doctrinebot commented on GitHub (Oct 15, 2011):

Comment created by @beberlei:

Formatting

@doctrinebot commented on GitHub (Oct 15, 2011): Comment created by @beberlei: Formatting
Author
Owner

@doctrinebot commented on GitHub (Oct 15, 2011):

Comment created by @beberlei:

I dont get your problem sorry. Is there a bug or is this just a general question?

@doctrinebot commented on GitHub (Oct 15, 2011): Comment created by @beberlei: I dont get your problem sorry. Is there a bug or is this just a general question?
Author
Owner

@doctrinebot commented on GitHub (Jan 21, 2012):

Comment created by @beberlei:

General question, no feedback, closing issue.

@doctrinebot commented on GitHub (Jan 21, 2012): Comment created by @beberlei: General question, no feedback, closing issue.
Author
Owner

@doctrinebot commented on GitHub (Jan 21, 2012):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jan 21, 2012): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1735