У меня много-много отношений с пользователем и аккаунтом. Когда я сохраняю форму (новый пользователь), у меня нет отношения в БД. Я что-то не так?
Я читаю http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html
@entity User(fosuser)
/**
* @ORM\ManyToMany(targetEntity="Account", mappedBy="users")
*/
private $accounts;/**
* Add accounts
*
* @param \CM\Bundle\iNewsBundle\Entity\Account $accounts
* @return User
*/
public function addAccount(\CM\Bundle\iNewsBundle\Entity\Account $accounts)
{
$this->accounts[] = $accounts;
return $this;
}@entity Account
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="accounts")
* @ORM\JoinTable(name="users_accounts")
*/
private $users;/**
* Add users
*
* @param \CM\Bundle\iNewsBundle\Entity\User $users
* @return Account
*/
public function addUser(\CM\Bundle\iNewsBundle\Entity\User $users)
{
$users->addAccount($this);
$this->users[] = $users;
return $this;
}
В БД таблица users_accounts пуста. Я не вижу новую запись.
Что я делаю неправильно ?
Полный userType
<?php
namespace CM\Bundle\iNewsBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class UserType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
private $roles;
private $nbRoles;
public function __construct($roles, $nbRoles)
{
$this->roles = $roles;
$this->nbRoles = $nbRoles;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$roles = $this->roles;
$nbRoles = $this->nbRoles;
$builder
->add('name')
->add('firstname')
->add('userName')
->add('email')
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
)
)
->add('roles', 'choice', array(
'required' => true,
'multiple' => true,
'choices' => $this->refactorRoles($roles, $nbRoles)//array("ROLE_ADMIN"=>"ADMIN", "ROLE_PUBLISHER"=>"PUBLISHER")
)
)
->add('accounts')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CM\Bundle\iNewsBundle\Entity\User'
));
}
/**
* @return string
*/
public function getName()
{
return 'cm_bundle_inewsbundle_user';
}private function refactorRoles($originRoles, $nbRoles) {
// $roles = array();
foreach($originRoles as $key=>$item)
{
$roles[$key] = substr($key, 5);
}
// echo'<pre>';
// var_dump($originRoles);
// die;
$roles = array_slice($roles, -$nbRoles, $nbRoles-1, true);
return $roles;
}
}
UserController
public function addAction($id=null, Request $request)
{
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->createUser();
$roles = $this->container->getParameter('security.role_hierarchy.roles');
//$user->addAccount($this->get('accountManager')->getActiveAccount());$userForm = $this->createForm(new UserType($roles, 4), $user);
$userForm->handleRequest($request);
if($request->getMethod() == 'POST')
{
if($userForm->isValid())
{
// echo'<pre>';
// var_dump($user);
// die;
$userManager->updateUser($user);
return $this->redirect($this->generateUrl('user_list'));
}
}
return $this->render('CMiNewsBundle:User:add.html.twig', array('userForm' => $userForm->createView()));
}
Задача ещё не решена.
Других решений пока нет …