0
votes

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?

2
Search in project folder with MediaRepository keyword. When you find it look namespace and type it.Mert Öksüz

2 Answers

1
votes

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.

-1
votes

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!