Note that there are some explanatory texts on larger screens.

plurals
  1. POFOSUserBundle : I would like to add other items by edit of a profile
    text
    copied!<p>I would like to add other items by edit of a profile in FOSUserBundle.<br> I was trying to solve by using a form collection.</p> <p>Entity/Information.php</p> <pre><code>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; // .... } </code></pre> <p>Entity/User.php</p> <pre><code>// ... public function __construct() { parent::__construct(); $this-&gt;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-&gt;information[] = $information; return $this; } /** * Remove information * * @param My\UserBundle\Entity\Information $information */ public function removeInformation(\My\UserBundle\Entity\Information $information) { $this-&gt;information-&gt;removeElement($information); } /** * Get information * * @return Doctrine\Common\Collections\Collection */ public function getInformation() { return $this-&gt;information; } </code></pre> <p>Controller/ProfileController</p> <pre><code>public function editAction() { $em = $this-&gt;container-&gt;get('doctrine')-&gt;getManager(); $own = $this-&gt;container-&gt;get('security.context')-&gt;getToken()-&gt;getUser(); $user = $em-&gt;getRepository('MyUserBundle:User')-&gt;find_one_with_info($own); //=&gt; 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-&gt;getInformation()) == 0) $user-&gt;addInformation(new Information()); $form = $this-&gt;container-&gt;get('fos_user.profile.form'); $formHandler = $this-&gt;container-&gt;get('fos_user.profile.form.handler'); $process = $formHandler-&gt;process($user); // ... </code></pre> <p>Form/Type/InformationType</p> <pre><code>// ... public function buildForm(FormBuilderInterface $builder, array $options) { $builder -&gt;add('website', 'text', array('required' =&gt; false)) // ... and other information ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver-&gt;setDefaults(array( 'data_class' =&gt; 'My\UserBundle\Entity\Information', )); } // ... </code></pre> <p>Form/Type/ProfileFormType</p> <pre><code>// ... protected function buildUserForm(FormBuilderInterface $builder, array $options) { $builder -&gt;add('username', null, array('label' =&gt; 'form.username', 'translation_domain' =&gt; 'FOSUserBundle')) -&gt;add('information', 'collection', array( 'type' =&gt; new InformationType(), )) ; } // ... </code></pre> <p>View/Profile/edit_content.html.twig</p> <pre><code>// ... {% for info in form.information %} &lt;div class="_errors"&gt; {{ form_errors(info.website) }} &lt;/div&gt; &lt;div class="_form_bar"&gt; {{ form_widget(info.website) }} &lt;/div&gt; // ... and other information {% endfor %} &lt;div class="_errors"&gt; {{ form_errors(form.current_password) }} &lt;/div&gt; &lt;div class="_form_bar"&gt; {{ form_widget(form.current_password) }} {{ form_label(form.current_password) }} &lt;/div&gt; {{ form_widget(form) }} // ... </code></pre> <p><img src="https://i.stack.imgur.com/DmeRX.png" alt="enter image description here"></p> <p>Error occurs when I sent this. </p> <blockquote> <p>Warning: spl_object_hash() expects parameter 1 to be object, array given in /path/to/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1375</p> </blockquote> <p>error point: FOSUserBundle/Form/Handler/ProfileFormHandler.php</p> <pre><code>protected function onSuccess(UserInterface $user) { $this-&gt;userManager-&gt;updateUser($user); //=&gt; error point } </code></pre> <p>Is the right method to use data transfer like an invitation system?<br> Is it possible with a collection form? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload