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(),
);
}
}