问题
I apologize in advance if this has been asked before.
I have successfully setup FOSUserBundle.
I am trying to setup "http://symfony.com/doc/current/book/doctrine.html" the product example from that link out of the documentation.
What I would like to do is set it up so a specific user adds a product the User Id of the FOSUser that user is associated with that product.
I have two bundles in the Acme folder
Acme/UserBundle -> the FOSUser Setup
and
Acme/MainBundle -> where I am setting up products.
I've gone over the relationship documents a few time and I am not quite clear on them in Symfony2.
I think it is a OneToMany relationship in the User Entity based on a "user_id" in the Products table but i'm not a 100% sure how to do it.
Does anyone know a tutorial or even a basic idea on if I am going in the right direction on this? Thanks very much and I will obviously publish the successful results Once I get there.
回答1:
You just have to add a relation between your user and your product. So your Product entity should be like this :
src/Acme/MainBundle/Entity/Product.php
namespace Acme\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\Column(type="decimal", scale=2)
*/
protected $price;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy="products")
* @ORM\JoinColumn(name="user_id", nullable=false)
*/
protected $user;
}
And your user entity
src/Acme/UserBundle/Entity/User.php
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Acme\MainBundle\Entity\Product;
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User
{
/**-
* User properties...
*/
/**
* @ORM\OneToMany(targetEntity="Acme\MainBundle\Entity\Product", mappedBy="user", cascade={"remove"})
*/
protected $products;
/**
* Constructor
*/
public function __construct()
{
$this->products = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add product
*
* @param Product $product
* @return Branch
*/
public function addProduct(Product $product)
{
$this->products[] = $product;
return $this;
}
/**
* Remove product
*
* @param Product $products
*/
public function removeProduct(Product $product)
{
$this->products->removeElement($product);
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
}
Everything is explain in the doctrine documentation
回答2:
The following works perfectly for me in Symfony 2.3 I got it from Reading the docs and going the knpuniversities episode 3 on Symfony2 I hope it helps someone.
The Entity
use Doctrine\ORM\Mapping as ORM;
use Acme\UserBundle\Entity\User;
/**
* @ORM\Entity
* @ORM\Table(name="products")
*/
class Products
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\Column(type="decimal", scale=2)
*/
protected $price;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Products
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set price
*
* @param float $price
* @return Products
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* Set description
*
* @param string $description
* @return Products
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User", cascade={"remove"})
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $owner;
public function getOwner()
{
return $this->owner;
}
public function setOwner(User $owner)
{
$this->owner = $owner;
}
}
and in the Controller
/**
* Creates a new Products entity.
*
*/
public function createAction(Request $request)
{
$entity = new Products();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
// Associate User Entity To Product
$user = $this->container->get('security.context')->getToken()->getUser();
$entity->setOwner($user);
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('products_show', array('id' => $entity->getId())));
}
return $this->render('AcmeMainBundle:Products:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
来源:https://stackoverflow.com/questions/20109270/symfony-2-fosuserbundle-relation-to-products-table