【问题标题】:FOSUserBundle : I would like to add other items by edit of a profileFOSUserBundle :我想通过编辑配置文件来添加其他项目
【发布时间】:2013-07-24 19:01:27
【问题描述】:

我想通过编辑 FOSUserBundle 中的配置文件来添加其他项目。
我试图通过使用表单集合来解决。

实体/信息.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;

    // ....
}

实体/用户.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;
}

控制器/配置文件控制器

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) }}

// ...

发送此内容时发生错误。

警告:spl_object_hash() 期望参数 1 是对象、数组 给出 /path/to/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php 第 1375 行

错误点:FOSUserBundle/Form/Handler/ProfileFormHandler.php

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

是否像邀请系统一样使用数据传输的正确方法?
是否可以使用收集表格?

【问题讨论】:

    标签: symfony fosuserbundle formcollection


    【解决方案1】:

    已解决。

    我似乎不应该使用收集表格。
    而且我似乎应该使用“-&gt;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;
    }
    

    控制器/配置文件控制器

    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.twig

    // ...
    
    <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) }}
    
    // ...
    

    【讨论】:

      【解决方案2】:

      看起来您编辑的那个用户没有填写information,因此表单无法显示任何内容。阅读How to Embed a Collection of Forms 文章。可能你必须用一些值来初始化这个字段。

      【讨论】:

      • 1:用户实体的信息属性将到数组集合。 2:如果用户没有信息,则用户添加新的信息对象。这是通过编辑配置文件添加其他项目的正确方法吗?有一种奇怪的感觉,就是数组与 oneToOne 的关系。 (如果我使用收集表格会不会有帮助?)
      • 注意:$this-&gt;userManager-&gt;updateUser($user); 警告:spl_object_hash() 期望参数 1 是对象,给定数组
      • 在oneToOne关系上使用entity表单类型
      • 我似乎应该使用-&gt;add('information', new InformationType())。谢谢你的建议。
      猜你喜欢
      • 2015-12-16
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 2018-09-10
      • 2012-04-17
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多