doushi4956 2016-01-10 17:20
浏览 33
已采纳

仅显示登录用户创建的内容.Symfony2.8

I have two table in different bundle Post entity is link to the table User entity. The problem is every users that are logged can see all the products, would like to allow (show) only what logged in user has created.

In my PostRepository.php i added this code but it doesn't works:

<?php

namespace FLY\BookingsBundle\Entity;

use Doctrine\ORM\EntityRepository;

class PostRepository extends EntityRepository
{
    public function createAction()
    {
        $qb = $this->_em->createQueryBuilder();
        $qb->select('i')
            ->from('FLYBookingsBundle:Post', 'i')
            ->where('i.roles LIKE :roles')
            ->setParameter('roles', '%owner object%');

        return $qb->getQuery()->getResult();
    }
}

Post.php

<?php

namespace FLY\BookingsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Application\Sonata\UserBundle\Entity\User;

/**
 * Post
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="FLY\BookingsBundle\Entity\PostRepository")
 */
class Post
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     *
     *
     * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User")
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    private $owner;



    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


/**
     * @return User
     */
    public function getOwner()
    {
        return $this->owner;
    }

    /*
     * @param User $owner
     */
    public function setOwner(User $owner)
    {
        $this->owner = $owner;

        return $this;
    }
}

PostController.php

    public function createAction(Request $request)
    {
        $entity = new Post();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $user = $this->getUser();
            $entity->setOwner($user);
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('post_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }
}
  • 写回答

1条回答 默认 最新

  • doushenjia8514 2016-01-10 23:00
    关注

    To show only posts belonging to logged in user:

    1. Each post must be associated with a user. Create a ManyToOne relationship between the Post entity and the User entity. See this documentation since your code is too sparse to guess how best to present a solution.
    2. Your show action needs to be restricted to logged in users. Refer to this documentation.
    3. Once your show action is restricted, get the user in it: $user = $this->getUser();
    4. With $user, $this->getDoctrine()->getManager()->getRepository('FLYBookingsBundle:Post')->findBy(array('user' => $user ));
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算