我试图使用Fosuserbundle为每个用户添加一个字段,网站(网址)
以下是我的实体:
user.php的
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @ORM\Table(name="lcl_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @ORM\Column(name="facebook_id", type="string", length=255, nullable=true) */
protected $facebook_id;
/** @ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true) */
protected $facebook_access_token;
/** @ORM\Column(name="google_id", type="string", length=255, nullable=true) */
protected $google_id;
/** @ORM\Column(name="google_access_token", type="string", length=255, nullable=true) */
protected $google_access_token;
//YOU CAN ADD MORE CODE HERE !
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Website", mappedBy="user", cascade={"remove"})
*/
protected $websites;
/**
* Constructor
*/
public function __construct()
{
$this->websites = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add website
*
* @param Website $website
* @return Branch
*/
public function addWebsite(Website $website)
{
$this->websites[] = $website;
return $this;
}
/**
* Remove website
*
* @param Website $websites
*/
public function removeWebsite(Website $website)
{
$this->websites->removeElement($website);
}
/**
* Get websites
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getWebsites()
{
return $this->websites;
}
}
Website.php
<?
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="website")
*/
class Website
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $url;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="websites")
* @ORM\JoinColumn(name="user_id", nullable=false)
*/
protected $user;
/**
* Set url
*
* @return string
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
}
比在控制器中调用
.
.
.
if($found===1)
{
$entity = new User();
$em = $this->getDoctrine()->getManager();
$website = new Website();
$website->setUrl("http://www.google.com");
$entity->addWebsite($website);
$em->persist($entity);
$em->flush();
return $this->redirect('/scan');
}else{
return $this->redirect('/verifyurl');
}
.
.
.
但不知怎的,它并没有存储在数据库中。
我说得对吗?
Symfony2和FOSUserbundle的新手
更新1:
到达那里......必须稍微纠正它(添加构造函数等)
以下是我现在的代码:
user.php的
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @ORM\Table(name="lcl_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @ORM\Column(name="facebook_id", type="string", length=255, nullable=true) */
protected $facebook_id;
/** @ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true) */
protected $facebook_access_token;
/** @ORM\Column(name="google_id", type="string", length=255, nullable=true) */
protected $google_id;
/** @ORM\Column(name="google_access_token", type="string", length=255, nullable=true) */
protected $google_access_token;
//YOU CAN ADD MORE CODE HERE !
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Website", mappedBy="user", cascade={"persist", "remove"})
*/
protected $websites;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->websites = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add website
*
* @param Website $website
* @return Branch
*/
public function addWebsite(Website $website)
{
$this->websites[] = $website;
return $this;
}
/**
* Remove website
*
* @param Website $websites
*/
public function removeWebsite(Website $website)
{
$this->websites->removeElement($website);
}
/**
* Get websites
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getWebsites()
{
return $this->websites;
}
/**
* Set id
*
* @return string
*/
public function setId($id)
{
$this->id = $ir;
return $this;
}
}
Website.php
<?
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="website")
*/
class Website
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $url;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="websites")
* @ORM\JoinColumn(name="id", nullable=false)
*/
protected $user;
/**
* Set url
*
* @return string
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
}
并在控制器中:
if($found===1)
{
$logger = $this->get('logger');
$logger->info("found");
$userid = $this->getUser()->getId();
$logger = $this->get('logger');
$logger->info("user".$userid);
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('AppBundle:User')->find($userid);
$logger = $this->get('logger');
$logger->info("user".$user);
$website = new Website();
$website->setUrl($gaurl);
$user->addWebsite($website);
$user->setId($userid);
$em->persist($user);
$em->flush();
return $this->redirect('/scan');
}else{
return $this->redirect('/verifyurl');
}
它有效...但每个用户只允许1个条目...通过第二个条目我得到一个例外。
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
任何人和想法如何纠正这个?
答案 0 :(得分:1)
Website
实体没有持久存储在代码中,也没有存在于关系中,因此它没有被插入到数据库中。您可以更改关系以添加这样的持久级联:
/**
* @ORM\OneToMany(targetEntity="Website", mappedBy="user", cascade={"persist", "remove"})
*/
protected $websites;
通过这样做,Doctrine将能够保留通过Website
关系找到的任何新User
个实体。
答案 1 :(得分:0)
这就是我想要的。
必须存储网站对象,而不是关系存储在数据库中。
也许它对某人有帮助.....
user.php的
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @ORM\Table(name="lcl_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @ORM\Column(name="facebook_id", type="string", length=255, nullable=true) */
protected $facebook_id;
/** @ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true) */
protected $facebook_access_token;
/** @ORM\Column(name="google_id", type="string", length=255, nullable=true) */
protected $google_id;
/** @ORM\Column(name="google_access_token", type="string", length=255, nullable=true) */
protected $google_access_token;
//YOU CAN ADD MORE CODE HERE !
/**
* @ORM\OneToMany(targetEntity="Website", mappedBy="id",cascade={"persist", "remove"})
*/
protected $websites;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->websites = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add website
*
* @param Website $website
* @return Branch
*/
public function addWebsite(Website $website)
{
$this->websites[] = $website;
}
/**
* Remove website
*
* @param Website $websites
*/
public function removeWebsite(Website $website)
{
$this->websites->removeElement($website);
}
/**
* Get websites
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getWebsites()
{
return $this->websites;
}
}
Website.php
<?
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="website")
* @ORM\Entity(repositoryClass="AppBundle\Repository\WebsiteRepository")
*/
class Website
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $url;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="websites")
*/
protected $user;
public function setUser(User $user)
{
$this->user = $user;
}
public function getUser()
{
return $this->user;
}
/**
* Set url
*
* @return string
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
}
控制器:
if($found===1)
{
$logger = $this->get('logger');
$logger->info("found");
$userid = $this->getUser()->getId();
$logger = $this->get('logger');
$logger->info("user".$userid);
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('AppBundle:User')->find($userid);
$logger = $this->get('logger');
$logger->info("user".$user);
$website = new Website();
$website->setUrl($gaurl);
$website->setUser($user);
$em->persist($website);
$em->flush();
return $this->redirect('/scan');
}else{
return $this->redirect('/verifyurl');
}