2013-07-25 23 views
1

我想通過編輯FOSUserBundle中的配置文件添加其他項目。
我試圖通過使用表單集合來解決。FOSUserBundle:我想通過編輯配置文件添加其他項目

實體/ information.php文件

namespace My\UserBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity(repositoryClass="My\UserBundle\Repository\UserRepository") 
*/ 
class Information 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column @ORM\Column(type="string", length=255, nullable=true) 
    */ 
    protected $website; 

    // .... and other information 

    /** 
    * @ORM\OneToOne(targetEntity="User", mappedBy="information", cascade={"persist", "merge"}) 
    */ 
    protected $user; 

    // .... 
} 

實體/ user.php的

// ... 

public function __construct() 
{ 
    parent::__construct(); 
    $this->information = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

// ... 

/** 
* @ORM\OneToOne(targetEntity="Information", inversedBy="user") 
* @ORM\JoinColumn(referencedColumnName="id") 
*/ 
protected $information; 

/** 
* Add information 
* 
* @param My\UserBundle\Entity\Information $information 
* @return User 
*/ 
public function addInformation(Information $information) 
{ 
    $this->information[] = $information; 

    return $this; 
} 

/** 
* Remove information 
* 
* @param My\UserBundle\Entity\Information $information 
*/ 
public function removeInformation(\My\UserBundle\Entity\Information $information) 
{ 
    $this->information->removeElement($information); 
} 

/** 
* Get information 
* 
* @return Doctrine\Common\Collections\Collection 
*/ 
public function getInformation() 
{ 
    return $this->information; 
} 

控制器/ ProfileController可

public function editAction() 
{ 
    $em = $this->container->get('doctrine')->getManager(); 
    $own = $this->container->get('security.context')->getToken()->getUser(); 
    $user = $em->getRepository('MyUserBundle:User')->find_one_with_info($own); //=> Left join information table 

    if (!is_object($user) || !$user instanceof UserInterface) { 
     throw new AccessDeniedException('This user does not have access to this section.'); 
    } 

    if (count($user->getInformation()) == 0) 
     $user->addInformation(new Information()); 

    $form = $this->container->get('fos_user.profile.form'); 
    $formHandler = $this->container->get('fos_user.profile.form.handler'); 

    $process = $formHandler->process($user); 

    // ... 

窗體/類型/信息類型

// ... 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('website', 'text', array('required' => false)) 
     // ... and other information 
    ; 
} 

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'My\UserBundle\Entity\Information', 
    )); 
} 

// ... 

表/類型/ ProfileFormType

// ... 

protected function buildUserForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle')) 
     ->add('information', 'collection', array(
      'type' => new InformationType(), 
     )) 
    ; 
} 

// ... 

查看/資料/ edit_content.html.twig

// ... 

{% for info in form.information %} 
    <div class="_errors"> 
     {{ form_errors(info.website) }} 
    </div> 
    <div class="_form_bar"> 
     {{ form_widget(info.website) }} 
    </div> 

    // ... and other information 

{% endfor %} 

<div class="_errors"> 
    {{ form_errors(form.current_password) }} 
</div> 
<div class="_form_bar"> 
    {{ form_widget(form.current_password) }} 
    {{ form_label(form.current_password) }} 
</div> 

{{ form_widget(form) }} 

// ... 

enter image description here

錯誤我送這個時候發生。

警告:spl_object_hash()預計參數1爲對象,數組 在 給出/path/to/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php 線1375

錯誤點:FOSUserBundle /表格/處理器/ ProfileFormHandler.php

protected function onSuccess(UserInterface $user) 
{ 
    $this->userManager->updateUser($user); //=> error point 
} 

是使用諸如邀請系統的數據傳輸正確的方法?
收集表單可以嗎?

回答

0

已解決。

我似乎不應該使用收集表格。
而我似乎應該使用「->add('profile', new ProfileType())」。

希望這有助於


實體/ Profile.php

namespace My\UserBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity(repositoryClass="My\UserBundle\Repository\UserRepository") 
*/ 
class Profile 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="string", length=255, nullable=true) 
    */ 
    protected $website; 

    // .... and other profile 

    /** 
    * @ORM\OneToOne(targetEntity="User", mappedBy="profile", cascade={"persist", "merge", "remove"}) 
    */ 
    protected $user; 

    // .... 
} 

實體/用戶。PHP

// ... 

/** 
* @ORM\OneToOne(targetEntity="Profile", inversedBy="user", cascade={"persist", "merge", "remove"}) 
* @ORM\JoinColumn(referencedColumnName="id") 
*/ 
protected $profile; 

public function setProfile(Profile $profile) 
{ 
    $this->profile = $profile; 
} 

public function getProfile() 
{ 
    return $this->profile; 
} 

控制器/ ProfileController可

public function editAction() 
{ 
    $em = $this->container->get('doctrine')->getManager(); 
    $own = $this->container->get('security.context')->getToken()->getUser(); 
    $user = $em->getRepository('MyUserBundle:User')->find_one_with_info($own); //=> Left join profile table 

    if (!is_object($user) || !$user instanceof UserInterface) { 
     throw new AccessDeniedException('This user does not have access to this section.'); 
    } 

    if (! $user->getProfile()) 
     $user->setProfile(new Profile()); 

    $form = $this->container->get('fos_user.profile.form'); 
    $formHandler = $this->container->get('fos_user.profile.form.handler'); 

    $process = $formHandler->process($user); 

    // ... 

表/類型/ ProfileType

// ... 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('website', 'text', array('required' => false)) 
     // ... and other profile 
    ; 
} 

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'My\UserBundle\Entity\Profile', 
    )); 
} 

// ... 

表/類型/ ProfileFormType

// ... 

protected function buildUserForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle')) 
     ->add('profile', new ProfileType()) 
    ; 
} 

// ... 

查看/資料/ edit_content.html。小枝

// ... 

<div> 
    {{ form_errors(form.profile.website) }} 
</div> 
<div> 
    <label>Website:</label> 
    {{ form_widget(form.profile.website) }} 
</div> 

// ... and other profile 

<div> 
    {{ form_errors(form.current_password) }} 
</div> 
<div> 
    {{ form_label(form.current_password) }} 
    {{ form_widget(form.current_password) }} 
</div> 

{{ form_widget(form) }} 

// ... 
0

看起來像你編輯的用戶沒有information填充所以形式不能顯示任何東西。閱讀How to Embed a Collection of Forms文章。可能您必須以某個值初始化此字段。

+0

1:用戶實體的信息屬性將數組集合。 2:如果用戶沒有信息,用戶添加新的信息對象。這是通過編輯配置文件添加其他項目的正確方法嗎?有一種奇怪的感覺,就是數組與oneToOne的關係。 (注意:'$ this-> userManager-> updateUser($ user);'警告:spl_object_hash()期望參數1是對象,在oneToOne上給出的數組爲 –

+0

關係使用'實體'表單類型 –

+0

(如果我使用集合表單,會不會有所幫助) –

相關問題