Symfony 2 FOSUserBundle与产品表的关系 [英] Symfony 2 FOSUserBundle Relation to Products Table
问题描述
我已成功设置FOSUserBundle。
我正在尝试设置 http://symfony.com/doc/current/book/doctrine .html 该链接的产品示例文档。
我想做的是设置它,因此特定用户向用户添加与该产品相关联的FOSUser的用户标识的产品。
我在Acme文件夹中有两个软件包
Acme / UserBundle - > FOSUser设置
和
Acme / MainBundle - >我正在设置产品。
我已经过了关系文件了几次,我在Symfony2中并不太清楚。
我认为这是基于产品表中user_id的用户实体中的OneToMany关系,但我不是100%肯定如何做。
<有没有人知道一个教程,甚至一个基本的想法,如果我正确的方向这个?非常感谢,我会显然发布成功的结果一旦我到达那里。
以下在Symfony 2.3中为我完美的工作,我从阅读文档中获得了相关信息,并在Symfony2 I上进行了knpuniversities第3集希望它能帮助某人。
实体
使用Doctrine\ORM\\ \\映射为ORM;
使用Acme\UserBundle\Entity\User;
/ **
* @ ORM\Entity
* @ ORM\Table(name =products)
* /
class产品
{
/ **
* @ 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;
/ **
*获取ID
*
* @return integer
* /
public function getId()
{
return $ this-> id;
}
/ **
*设定名称
*
* @param string $ name
* @return产品
* /
public function setName($ name)
{
$ this-> name = $ name;
return $ this;
}
/ **
*获取名称
*
* @return string
* /
public function getName )
{
return $ this-> name;
}
/ **
*设定价格
*
* @param float $ price
* @return产品
* /
public function setPrice($ price)
{
$ this-> price = $ price;
return $ this;
}
/ **
*获取价格
*
* @return float
* /
public function getPrice )
{
return $ this-> price;
}
/ **
*设置描述
*
* @param string $ description
* @return产品
* /
public function setDescription($ description)
{
$ this-> description = $ description;
return $ this;
}
/ **
*获取描述
*
* @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;
}
}
在控制器中
/ **
*创建一个新的产品实体。
*
* /
public function createAction(Request $ request)
{
$ entity = new Products();
$ form = $ this-> createCreateForm($ entity);
$ form-> handleRequest($ request);
if($ form-> isValid()){
$ em = $ this-> getDoctrine() - > getManager();
//将用户实体关联到产品
$ 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(),
));
}
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.
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(),
));
}
这篇关于Symfony 2 FOSUserBundle与产品表的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!