Dimitri
Dimitri

Reputation: 959

Error in doctrine model

I have a problem with my doctrine model. When I call him I have an error and I don't know why...

My entity:

namespace Dimi\YvmBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityRepository;

/**
 * Download
 *
 * @ORM\Table("t_download")
 * @ORM\Entity(repositoryClass="Dimi\YvmBundle\Entity\DownloadRepository")
 */
class Download extends EntityRepository
{


    public function getLastDownload()
    {

        $em = $this->getEntityManager();
        //$em = $this->getDoctrine()->getManager();
        $query = $em->createQueryBuilder();

        $query->select('d')
            ->from('DimiYvmBundle:Download', 'd')
            ->orderBy('d.id', 'DESC')
            ->groupBy('d.ytId');


        $query->setMaxResults(48);
        return $query->getQuery()->getResult();

    }

}

TopController.php :

 public function getLastDownload()
    {

        $query = $this->createQueryBuilder('q');

        $query->select('d')
            ->from('DimiYvmBundle:Download', 'd')
            ->orderBy('d.id', 'DESC')
            ->groupBy('d.ytId');


        $query->setMaxResults(48);
        return $query->getQuery()->getResult();

    }

Error:

ContextErrorException: Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::__construct(), called in /var/www/site/main.site/Symfony2/src/Dimi/YvmBundle/Controller/TopController.php on line 28 and defined in /var/www/site/main.site/Symfony2/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php line 67

Do you know how I can fix that's ?

Thanks you all for your helping. Best regards,

EDIT:

I have resolved my problem, with doctrine if you want to create your custom query you have to write them into the myentitiRepository.php, not directly in myentity.php.

Upvotes: 0

Views: 1736

Answers (2)

NHG
NHG

Reputation: 5877

So, first of all you have to define query alias for createQueryBuilder() method like -createQueryBuilder('q');. Next, you should rather use shorter notation: $this->createQueryBuilder(); instead $query = $this->getEntityManager()->createQueryBuilder();.

Compare: getEntityManager() and createQueryBuilder($alias)

Moreover, you should get your Download repository via $this->getDoctrine()->getRepository('YourBundleBundle:Download') in your controller. When you call new Download() construct method of extended Doctrine\ORM\EntityRepository is called too and it's makes error.

As @Cerad wrote - Entity and Repository are separate classes. In your controller after correctly got Repository class you can call each method simple:

$repository = $this->getDoctrine()->getRepository('YourBundleBundle:Download');
$result = $repository->myCustomMethod();

Upvotes: 1

Cerad
Cerad

Reputation: 48865

I am assuming this is Doctrine 2 and that your posted Download entity code is accurate?

You have: class Download extends EntityRepository which is just plain wrong.

Entities do not extend repositories. Two completely different objects.

You should have:

/ **
  * Download Entity
  *
  * @ORM\Table("t_download")
  * @ORM\Entity(repositoryClass="Dimi\YvmBundle\Entity\DownloadRepository")
 */
class Download
{
    /* @Id */
    protected $id;

    /* Other property mappings */

and

class DownLoadRepository extends EntityRepository
{
    // Your custom queries

And yes, your query building code needs some work. But get your entity and repository in separate classes first.

Upvotes: 4

Related Questions