Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3924

I am trying to get a list of users with their main picture in Symfony 4

$
0
0

I am having a hard time showing the main picture in a list of users. The query seems to do his job I can see in the log. These are my tables and code:

table user

id | username

table meta

id | user_id | plaats_id

table picture

id | user_id | naam| mainfoto

These are my Entities:

User.php

     /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;   

    /**
     * @var string
     *
     * @ORM\Column(type="string", unique=true)
     * @Assert\NotBlank()
     * @Assert\Length(min=2, max=50)
     */
    private $username;

     /**
     * @ORM\OneToMany(targetEntity="App\Entity\Pictures", mappedBy="user", orphanRemoval=true)
     */
    private $pictures;

Meta.php

     /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\User", cascade={"persist", "remove"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;


    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Plaats", inversedBy="metas")
     * @ORM\JoinColumn(nullable=true)
     */
    private $plaats;

Pictures.php

     /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="pictures")
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;

     /**
     * @ORM\Column(type="integer")
     */
    private $mainfoto;

MetaRepository.php

public function getSingles()
    {
        return $this->createQueryBuilder('meta')
            ->addselect('u')           
            ->addSelect('pics')
            ->Join('meta.user', 'u')            
            ->Join('u.pictures', 'pics')
            ->andWhere('pics.mainfoto = 1')
            ->getQuery()
            ->execute();
    }

SinglesController.php

public function index(Request $request, int $page, MetaRepository $metas): Response
    {

        $this->em = $this->getDoctrine()->getManager();       
        $singlesList = $metas->getSingles();

        return $this->render('singles/singles_list.html.twig', ['singles' => $singlesList]);
    }

singles_list.html.twig

  {% for single in singles %}

        <div class="row">
            <div class="col-sm">
                {{ single.user.pictures.getMainFoto() }}
            </div>
            <div class="col-sm">
                {{ single.user.username }}
            </div>
            <div class="col-sm">
                {{ single.user.geboortedatum.diff((date('NOW'))).format('%y jaar') }}
            </div>
            <div class="col-sm">
                {{ single.plaats }}
            </div>
        </div>



    {% else %}
        <div class="well">{{ 'database.no_message_found'|trans }}</div>
    {% endfor %}

{% endblock %}

Viewing all articles
Browse latest Browse all 3924

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>