0
votes

Between 2 entities exists OneToMany relationship. First is User Entity second is Domains entities. One user can have multiple domains.

This is Users Entity (removed other fields because unrelated with subject):

class Users extends BaseUser
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="\UsersBundle\Entity\UserDomains" , mappedBy="user" , cascade={"all"})
     */
    protected $domains;
    /**
     * Add domain
     *
     * @param \UsersBundle\Entity\UserDomains $domain
     *
     * @return Users
     */
    public function addDomain(\UsersBundle\Entity\UserDomains $domain)
    {
        $this->domains[] = $domain;

        return $this;
    }

    /**
     * Remove domain
     *
     * @param \UsersBundle\Entity\UserDomains $domain
     */
    public function removeDomain(\UsersBundle\Entity\UserDomains $domain)
    {
        $this->domains->removeElement($domain);
    }
}

This is UserDomains Entity (some fields has been removed):

class UserDomains
{
    public function __construct() {
        $this->setCreatedAt(new \DateTime());
    }

    public function __toString()
    {
        return $this->name;
    }

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="AffiliateBundle\Entity\Users", inversedBy="domains")
     * @ORM\JoinColumn(name="user", referencedColumnName="id", onDelete="CASCADE")
     */
    private $user;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set user
     *
     * @param integer $user
     *
     * @return UserDomains
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return integer
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return UserDomains
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}

Must be add a domain when register user to system. I have got RegisterType for registration form. This type form class has got DataTransformer for adding domain which register user.

RegisterType class is here:

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('fullName', 'text', array('attr' => array('class' => 'form-control'), 'label' => 'user_register.fullname', 'translation_domain' => 'UsersBundle'))
            ->add('domains', 'text', array('attr' => array('class' => 'form-control select2', 'id' => 'select2_sample1')))
        ;

        $builder->get('domains')->addModelTransformer(new CallbackTransformer(
            function(){
                return '';
            },
            function($data){

                $arrCollection = new ArrayCollection();

                if (strpos($data, ",") !== false) {
                    $expData = explode(',', $data);
                    foreach ($expData as $domain) {
                        $domainObj = new UserDomains();
                        $domainObj->setName($domain);
                        $domainObj->setEnabled(true);
                        $arrCollection->add($domainObj);
                    }
                } else if (!empty($data)) {
                    $domain = new UserDomains();
                    $domain->setName($data);
                    $domain->setEnabled(true);
                    $arrCollection->add($domain);
                }

                return $arrCollection;
            }
        ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyBundle\Entity\Users',
            'intention'  => 'registration',
        ));
    }

    public function getParent()
    {
        return 'fos_user_registration';

        // Or for Symfony < 2.8
        // return 'fos_user_registration';
    }

    public function getName()
    {
        return 'user_registration';
    }
}

So anyone fill the register form and submit. User created and adding the domains to user_domains table but doesn't update the user field which must be new user's id. How to do this with automatically? Or have you got any idea to update this user column of user_domains table with the new user's id?

Thanks for helps to all StackoverFlow :)

1
In User::addDomain add $domain->setUser($this); I found your question to be a bit confusing but I think that is what you are asking for.Cerad
This didn't resolved my problem. Problem is when User entity save it must be save to UserDomains entity with new user's id.Mesuti
I think your basic approach is flawed. Take a look at form collections: symfony.com/doc/current/cookbook/form/form_collections.htmlCerad

1 Answers

2
votes

Add __construct like this to RegistrationType:

class RegistrationType extends AbstractType
{
    private $user;

    public  function  __construct(Users $user) {
        $this->user = $user;
    }

add setUser function to your DataTransform like this:

$domainObj->setUser($this->user);
//and
$domain->setUser($this->user);

finally update your controller like this:

$form = $this->createForm(new RegistrationType($userEntity), $userEntity);