Skip to content

Instantly share code, notes, and snippets.

@ahonymous
Created December 9, 2015 20:59
Show Gist options
  • Save ahonymous/e20f6ce2a9e413bb671b to your computer and use it in GitHub Desktop.
Save ahonymous/e20f6ce2a9e413bb671b to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Article
*
* @ORM\Table(name="article")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ArticleRepository")
*/
class Article
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
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 Author
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Author", inversedBy="articles")
*/
private $author;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Article
*/
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 Article
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set author
*
* @param \AppBundle\Entity\Author $author
*
* @return Article
*/
public function setAuthor(\AppBundle\Entity\Author $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return \AppBundle\Entity\Author
*/
public function getAuthor()
{
return $this->author;
}
}
<?php
namespace AppBundle\Repository;
/**
* ArticleRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ArticleRepository extends \Doctrine\ORM\EntityRepository
{
public function getArticles()
{
return $this->createQueryBuilder('a')
->join('a.author', 'au')
->getQuery()
->getResult();
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* Author
*
* @ORM\Table(name="author")
* @ORM\Entity(repositoryClass="AppBundle\Repository\AuthorRepository")
*/
class Author
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="surname", type="string", length=255)
*/
private $surname;
/**
* @var Collection
*
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Article", mappedBy="author")
*/
private $articles;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Author
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set surname
*
* @param string $surname
*
* @return Author
*/
public function setSurname($surname)
{
$this->surname = $surname;
return $this;
}
/**
* Get surname
*
* @return string
*/
public function getSurname()
{
return $this->surname;
}
/**
* Constructor
*/
public function __construct()
{
$this->articles = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add article
*
* @param \AppBundle\Entity\Article $article
*
* @return Author
*/
public function addArticle(\AppBundle\Entity\Article $article)
{
$this->articles[] = $article;
return $this;
}
/**
* Remove article
*
* @param \AppBundle\Entity\Article $article
*/
public function removeArticle(\AppBundle\Entity\Article $article)
{
$this->articles->removeElement($article);
}
/**
* Get articles
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getArticles()
{
return $this->articles;
}
}
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$articles = $this->getDoctrine()->getRepository('AppBundle:Article')->getArticles();
dump($articles);
// replace this example code with whatever you need
return $this->render('default/index.html.twig', array(
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment