0

In a database, I have a nullable column. When I render my twig model, a null value throw an exception. Normal.

But, when I am testing if the value is null, it also throw an exception… because the value is null.

Problematic part in my twig file :

{% if musicien.photo is null %}
    <td><img class="img-vignette" src="data:image/jpeg;base64,{{ musicien.photo }}"/></td>
{% else %}
    <td><img class="img-vignette" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wAARCABaAFcDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD0CiiigDF12xtpp7KSSIM8lwkbHJ5XB4pb3S7ays5bqxBt5oVMgZWODjnBHfNSa8rulisT+W5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u0VHBL50KyGN4938Egww+tVJdWgWV4oYp7l0OGEEe4KfQnpRdAX6KqWmowXUjRASRzKMmKVdrY9cUU07gUbqK4l8REW1yIGFoCWMYfI3nirMGnSi5S4vLtrmSPPljYEVc98DvXDX0jjULkh2z5jDr7mu80ck6RaEnJ8pf5VlF3YyF1VvE0ZZQStoSMjod4pda+7Zf9fkX8642R2/ttvmP/AB8Ede26r/i5mGrqAxA8tT19zS59GB0N6yRa1Zy3OBDsZUZuiyEj8uK0ZZI44meVlWMDkseMVnaciTeHYVlVXBiyQwzXOaXGkms+U6K0YYYVhkD8Kpyt8wOl0IY0/IBWJpHaIEdEJOKTw8R/ZMa5+ZHcMPQ7zWkOBgVx/iJETWVCKqh8FgBjd9fWhvlswOraVZoplt5VaRQR8rZ2nHFU9CeE6ZFHFhXjG2VehD98/jVqxhihtYxFGkYKgkKoGTXP+LY0jKyoirIRy4GCefWm3ZcwGjqpEt/aRWzA3almyOqptOc+nOKKd4dijTT96oqsx+ZgME/WihK+oj//2Q=="/></td>
{% endif %}

And the exception  :

An exception has been thrown during the rendering of a template ("Warning: stream_get_contents() expects parameter 1 to be resource, null given").

My index funcion :

public function index(): Response
{
    if(isset($_POST['search'])){
        $search = $_POST['search'].'%';
    }else{
        $search = '%';
    }

    $musiciens = $this->getDoctrine()
    ->getRepository(Musicien::class)
    ->createQueryBuilder('m')
    ->where('m.nomMusicien LIKE :search')
    ->setParameter('search', $search)
    ->getQuery()
    ->getResult();

    return $this->render('musicien/index.html.twig', array(
        'musiciens' => $musiciens,
    ));
}

It is the {% if musicien.photo is null %} line who throw the exception. I just want to show a default value if this value is null.

2 Answers 2

1

Instead of:

{% if musicien.photo is null %}
  <td>
    <img class="img-vignette" src="data:image/jpeg;base64,{{ musicien.photo }}"/>
  </td>
{% else %}
...

Try:

{% if musicien.photo is null %}
  <td>
    <img class="img-vignette" src="some_default_photo_because_musicien_deosnt_have_one"/>
  </td>
{% else %}
...

Now, you are basically checking if photo is null, and then try to print in anyway.

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

1 Comment

It is right. I was a mistakes, but it was not this line who thrown the exception. I changed the lines, now it is supposed to show the default photo. But, it is really the line "if musicien.phot is null" who throw the exception, the exception is still be thrown.
0
  1. Please don't use isset($_POST['....']) in Symfony, the Symfony\Component\HttpFoundation\Request class is there for that.
  2. Use a separated QueryBuilder, place it inside your App\Repository\MusicienRepository

I think this can help you

The index() action for your controller

public function index(MusicienRepository $musicienRepository, Request $request): Response 
{
    // you can use $search = ($request->request->get('search')) ? $request->request->get('search') . '%' : '%';*/

    // But let's write something cool
    $search = $request->request->get('search');

    return $this->render('musicien/index.html.twig', [
        'musiciens ' => $musicienRepository->findLike($search),
    ]);
}

Your App\Repository\MusicienRepository

public function findLike($search)
{
    return $this->createQueryBuilder('m')
        ->andWhere('m.nomMusicien LIKE :search')
        ->setParameter('search', '%'.$search.'%')
        ->getQuery()
        ->getResult()
    ;
}
  • {% if variable is null %} is not required to test if the given variable is null or not, just use {% if variable %}.

Then your index.html.twig must be

{% set photo = (musicien.photo) ? musicien.photo : 'your_default_photo' %}
<td><img class="img-vignette" src="data:image/jpeg;base64,{{ photo }}"/></td>

3 Comments

Thank you for your help. I didn't know Request object existed, and now I undersdand better the Repository class. I edited my code, but the exception is still be thrown… The simple call to musicien.photo, even if it is for a test, throw the exception. "An exception has been thrown during the rendering of a template ("Warning: stream_get_contents() expects parameter 1 to be resource, null given")." I don't understand why if I want to test if something is null, and the value is null, it throw an exception…
Can you show the full controller code's? And the the musicien.html.twig
you could use an accesor like in laravel, when getting the model object, so instead of null you set it as empty string

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.