2

I'm making my first small app in symfony, a simple blog.

Now I have been using the documentation for both symfony and doctrine and want to preform a simple, beginner task: display a json encoded simple table

Yet somehow I cant seem to get along with doctrine.

Here is my data (apart form the view which does nothing but display the value):

//AppBundle/Controller/DefaultController.php
<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Post;
use Doctrine\Common\Persistence;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        $database = new Post();
        $output = $database->findAll();

        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
            'x' => json_encode($output)
        ]);
    }
}


<?php
//AppBundle/Entity/Post.php

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityRepository;
/**
* @ORM\Entity
* @ORM\Table(name="sqltest")
*/
class Post extends EntityRepository
{
    //The post for now uses data from a temponary test table
    /**
    * @ORM\Column(type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

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

    /**
    * @ORM\Column(type="integer", scale=2)
    */
    private $number;



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

    /**
     * Set name
     * @param string $name
     * @return Post
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set number
     * @param integer $number
     * @return Post
     */
    public function setNumber($number)
    {
        $this->number = $number;

        return $this;
    }

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

}

Problem is when I try to display the website i get this exception

Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::__construct(), called in C:\Users\Alan\Desktop\symf-blog\src\AppBundle\Controller\DefaultController.php on line 19 and defined

Problematic line being the one with $database = new Post();

I am very new in this and am aware that the response is very simple and I just don't see it. When answering please provide and explanation which even a dead rabbit could understand.

Pretty thanks for your patience.

PS: Also an explanation about what the $em variable I've seen so much about is for and from where do I get it would be nice

7
  • Are you familiar with OOP? If so check here for the parameters required by EntityRepository the class you're inherit from. So you will need to pass them or don't extend at all (check here) Commented Dec 9, 2016 at 21:17
  • I understand the $class element, but still couldn't find any info on the origin of variable $em Commented Dec 9, 2016 at 21:23
  • This can give you a tip $em = $this->getDoctrine()->getManager(); $entities = $em->getRepository("CommonBundle:Bank")->findAll(); Commented Dec 9, 2016 at 21:31
  • Reset a bit and read the getting started chapter: symfony.com/doc/current/doctrine.html Doctrine is an ORM not an Active Record module so having an entity extend a repository makes zero sense. No need to fret. If you really want an Active Record approach, there are several to pick from. Commented Dec 9, 2016 at 21:52
  • @ReynierPM $this refers to what in that case? Commented Dec 9, 2016 at 22:27

2 Answers 2

2

If you're want a repository class for custom DB functions then this is the right way to do it:

namespace AppBundle\Entity;    
use Doctrine\ORM\EntityRepository;

class PostRepository extends EntityRepository
{
    public function findAll()
    {
        return $this->findBy(array(), array('id' => 'DESC', 'createdAt' => 'DESC'));
    }  
   ....
}

Then in your controller:

$em = $this->getDoctrine()->getManager(); 
$entities = $em->getRepository("AppBundle:Post")->findAll();

Remove the annotations (them belongs to the entity). Also pay attention to what @james_bond told you. Try that!

Sign up to request clarification or add additional context in comments.

Comments

2

As stated in the documentation, you access custom repository classes through doctrine entity manager.

$em = $this->getDoctrine()->getManager();  
$posts = $em->getRepository('YourBundle:Post')->findAll();

Also you're mixing your entity definition with your repository definition, which is not a good idea.

Please refer to the doctrine documentation in symfony for proper usage.

1 Comment

Added ` $em = $this->getDoctrine()->getManager(); $entities = $em->getRepository("AppBundle:Post")->findAll();` And later printed inside $json_encode. The result was [{}{}{}] (for a 3 row table, adding, made more {} inside)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.