DEV Community

Discussion on: Serializer Component with Symfony

Collapse
 
_mertsimsek profile image
Mert Simsek

Merhaba,

Tabiki, nedir sorun?

Collapse
 
shnby780 profile image
Mehmet Şahin • Edited

Symfonyde ajax ile veritabanından veri getirmeye çalışıyorum ama bir türlü yapamadım. Yapmak istediğim, kullanıcı profil sayfasına kullanıcının paylaştığı postları ajax ile getirmek.

profile.html.twig

{% extends 'base.html.twig' %}

{% block title %}User{% endblock %}

{% block body %}

<div class="profile-lasts"></div>

<script>

        $(document).ready(function () {
            $.ajax({
                url: '{{ path('show_user_posts') }}',
                type: 'post',
                dataType: 'json',
                contentType: 'application/json',
                async: true,
                data: {
                    'userid': {{ app.user.id }}
                },
                success: function(data) {
                    $.each(data, function () {
                        $(".profile-lasts").prepend("<table><tr><td>Username</td><td>Content</td><td>Creadted at</td></tr><tr><td>"+this.username+"</td><td>"+this.content+"</td><td>"+this.createdAt+"</td></tr></table>");
                    });
                }
            });
        });
    </script>
{% endblock %}

PostController.php

<?php

namespace App\Controller;

use App\Entity\Post;
use App\Entity\User;
use App\Form\PostType;
use App\Repository\PostRepository;
use App\Repository\UserRepository;
use phpDocumentor\Reflection\Types\Array_;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
 * @Route("/post")
 */
class PostController extends AbstractController
{
    /**
     * @Route("/show", name="show_user_posts", methods={"GET","POST"})
     */
    public function show(Request $request, UserRepository $userRepository, PostRepository $postRepository, SerializerInterface $serializer): Response
    {
        if ($request->isXmlHttpRequest()){
            $userid = $_POST['userid'];
            if (!empty($userid)){
                $user = $userRepository->findBy(['id' => $userid]);
                $posts = $postRepository->findBy(['user' => $user[0]]);

                return new JsonResponse($posts);
            }
            return new JsonResponse('userid null');
        }

        return new JsonResponse('not xmlhttprequest');
    }
}

Post.php

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use phpDocumentor\Reflection\Types\Integer;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PostRepository")
 */
class Post
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $content;

    /**
     * @ORM\Column(type="string", length=75, nullable=true)
     */
    private $image;

    /**
     * @ORM\Column(type="string", length=10, nullable=true)
     */
    private $likecounter;

    /**
     * @ORM\Column(type="string", length=10, nullable=true)
     */
    private $dislikecounter;

    /**
     * @ORM\Column(type="string", length=10, nullable=true)
     */
    private $commentcounter;

    /**
     * @ORM\Column(type="string", length=10, nullable=true)
     */
    private $status;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $created_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $updated_at;

Bir haftadır bu sorun ile uğraşıyorum, yardımcı olabilirseniz çok sevinirim. Mezun olmadan önce projemi bitirmek istiyorum.

Thread Thread
 
_mertsimsek profile image
Mert Simsek

Sorun ne, hata mı alıyorsun?

Thread Thread
 
shnby780 profile image
Mehmet Şahin

Failed to load resource: the server responded with a status of 500 ()

bu hatayı veriyor

Thread Thread
 
_mertsimsek profile image
Mert Simsek

Bence github'a at, public repo olarak, öyle bakalım. Bu şekilde debug edemeyiz.

Thread Thread
 
shnby780 profile image
Mehmet Şahin

tamam atıyorum.

Thread Thread
 
shnby780 profile image
Mehmet Şahin

aslında github kullanmayı bilmiyorum, denedim ama yapamadım.

Thread Thread
 
shnby780 profile image
Mehmet Şahin

hallettim sorunumu. teşekkür ederim ilgilendiğiniz için.