no se como has implementado D2, porque éste mapea las entidades a clases autenticas PHP y el uso por ejemplo de una clase Articulo 1:n con Comentarios sería más u menos así:
Código PHP:
$em = $doctrineEntityManager;//obtener el Entity Manager
$articulo = $em->getReference('Articulo', 1);
//obtener comentarios:
$comments = $articulo->getComments();
clases:
Articulo:
Código PHP:
Ver original<?php
namespace Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use \Doctrine\Common\Collections\ArrayCollection;
/**
* Articulo
*
*
*
* @Entity(repositoryClass="Repository\ArticleRepository")
* @Table(name="prueba.article")
* @author maycolalvarez
*/
class Articulo
{
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* @Column(type="string", name="_name", length=100, nullable=false)
*/
protected $name;
/**
*
* @var type
* @Gedmo\Timestampable(on="create")
* @Column(type="datetime")
*/
protected $created;
/**
*
* @var type
* @Gedmo\Timestampable(on="update")
* @Column(type="datetime")
*/
protected $updated;
/**
* @OneToMany(targetEntity="Comentario", mappedBy="article")
*/
private $comments;
public function __construct()
{
$this->comments = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set created
*
* @param datetime $created
*/
public function setCreated($created)
{
$this->created = $created;
}
/**
* Get created
*
* @return datetime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param datetime $updated
*/
public function setUpdated($updated)
{
$this->updated = $updated;
}
/**
* Get updated
*
* @return datetime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Add comments
*
* @param Entity\Comentario $comments
*/
public function addComentario(Entity\Comentario $comments)
{
$this->comments[] = $comments;
}
/**
* Get comments
*
* @return Doctrine\Common\Collections\Collection
*/
public function getComments()
{
return $this->comments;
}
}
comentario:
Código PHP:
Ver original<?php
namespace Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use \Doctrine\Common\Collections\ArrayCollection;
/**
* Comentario
*
*
* @Entity(repositoryClass="Repository\CommentRepository")
* @Table(name="prueba.comment")
* @author maycolalvarez
*/
class Comentario
{
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* @Column(type="string", name="_name", length=100, nullable=false)
*/
protected $name;
/**
*
* @var type
* @Gedmo\Timestampable(on="create")
* @Column(type="datetime")
*/
protected $created;
/**
*
* @var type
* @Gedmo\Timestampable(on="update")
* @Column(type="datetime")
*/
protected $updated;
/**
* @ManyToOne(targetEntity="Articulo", inversedBy="comments")
* @JoinColumn(nullable=false)
*
*/
private $article;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set created
*
* @param datetime $created
*/
public function setCreated($created)
{
$this->created = $created;
}
/**
* Get created
*
* @return datetime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param datetime $updated
*/
public function setUpdated($updated)
{
$this->updated = $updated;
}
/**
* Get updated
*
* @return datetime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set article
*
* @param Entity\Articulo $article
*/
public function setArticle(Entity\Articulo $article)
{
$this->article = $article;
}
/**
* Get article
*
* @return Entity\Articulo
*/
public function getArticle()
{
return $this->article;
}
}