0

I have a table which has relationship with Application\Sonata\MediaBundle\Entity\Media (SonataMediaBundle Entity) as 'media'

Normally I can make the form for Media like this below,

    $form = $this->createFormBuilder($myMedia)
    ->add('name')
    ->add('media') // make the selectbox
    ->add('save', SubmitType::class, array('label' => 'Create Post'))
    ->getForm();

However I want to restrict to some medias from all medias, then I made this.

    $form = $this->createFormBuilder($myMedia)
    ->add('name')
    ->add('media','entity',array(
            'class' => "Application\Sonata\MediaBundle\Entity\Media",
            'query_builder' => function(EntityRepository $er) {
                    return $er->createQuery('SELECT r FROM ApplicationSonataMediaBundle:Media');
            }))
    ->add('save', SubmitType::class, array('label' => 'Create Post'))
    ->getForm();

However it shows the error like this.

Undefined method 'createQuery'. The method name must start with either findBy or findOneBy!

I have found some articles and understood it is related with Repository. But I am not sure which Repository should I point. THere is no Repository class under Sonata\MediaBundle\ either Application\Sonata\MediaBundle

namespace Application\Sonata\MediaBundle\Entity;    
use Sonata\MediaBundle\Entity\BaseMedia as BaseMedia;

@ORM\Entity(repositoryClass="Where is my repository???")

class Media extends BaseMedia
{
    /**
     * @var int $id
     */
    protected $id;

BTW, my first code shows only select box for pictures(medias)

It is not useful enough to select pictures, Is there a more suitable way for selecting pictures?

Community
  • 1
  • 1
whitebear
  • 11,200
  • 24
  • 114
  • 237

2 Answers2

1

Look at the error, the createQuery method does not exist.

If you take a look at the EntityRepository class you will see that the right method is createQueryBuilder().

If you look at the content of the method you'll see that it returns a QueryBuilder instance with already the right select from statement since you are supposed to get the right repository for your media entity from the Entity form type since you pass the class of your entity in the class option.

Mawcel
  • 1,967
  • 15
  • 22
  • Thank you very much. I have checked the Doctorine/ORM/EntityRepository class and understood what you meant. I am not sure yet how can I use createQuery() instead of createQueryBuilder() though, for now createQueryBuilder() fulfill my purpose. – whitebear Feb 01 '17 at 14:38
  • CreateQuery is a method from the `EntityManager` class. The `EntityRepository` class has a `getEntityManager()` method but it is protected so you can't call it from outside – Mawcel Feb 01 '17 at 16:46
-1

You have defined $er as $this->getDoctrine()->getRepository('Application\Sonata\MediaBundle\Entity:Media') which is the EntityRepository. What you need rather is the EntityManager which is $this->getDoctrine()->getManager() and then use the select statement that you have in the piece of code. Hope it helps!

Piyush Singh
  • 67
  • 1
  • 6
  • `$this->getDoctrine()->getManager()` can be called in a controller but this is a closure in a form type.He is receiving the entity repository as parameter of the closure as the callback from the `query_builder` option always does. – Mawcel Feb 01 '17 at 12:59