2016-11-26 13 views
1

に新しい提出1を追加し、私の実体である:FOSUserbundle Symfony2のは、私がここでFosuserbundle</p> <p>を使用して、ユーザーごとに、フィールド、ウェブサイト(URL)を追加しようとミリアンペア多くの

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つのエントリは、私は例外を取得する2番目のエントリで...ことができます。

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails 

これを修正する方法はありますか?

答えて

1

エンティティはコードに保持されず、関係にも保持されないため、データベースに挿入されません。 DoctrineはUser関係を通じて見つかった新しいWebsiteエンティティを永続化することができるようになります、ということにより

/** 
* @ORM\OneToMany(targetEntity="Website", mappedBy="user", cascade={"persist", "remove"}) 
*/ 
protected $websites; 

:あなたはこのような持続カスケードを追加するために関係を変更することができます。

+0

感謝。あなたは私のアップデート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; 
    } 


} 

ウェブサイトに役立ちます。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; 
    } 


} 

コントローラ:@Isouzaはあなたのソリューションをしようとしました

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'); 
    } 
関連する問題