嵌入PUGXMultiUserBundle和注册表单



我使用PUGXMultiUserBundle,因为我有两种类型的用户:CustomerUser和ShopUser。 商店用户与位置(地址,城市...(具有一对多关系。

我需要将所有数据保存到表格(商店和位置(中,仅从注册商店用户表单类型。如何使用 PUGX 执行此操作?

我尝试使用表单嵌入和表单集合,但我没有得到一个好的结果。 你有什么想法吗?

编辑

这里的代码:

形式/:

注册商店用户表单类型.php

<?php

namespace AppBundleForm;
use AppBundleEntityShopUser;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
use SymfonyComponentFormFormBuilderInterface;
use FOSUserBundleFormTypeRegistrationFormType as BaseRegistrationFormType;
use SymfonyComponentOptionsResolverOptionsResolver;
class RegistrationShopUserFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('lastName')
->add('shopName')
;
}
public function getParent()
{
return 'FOSUserBundleFormTypeRegistrationFormType';
}
public function getBlockPrefix()
{
return 'user_shop';
}
public function getName()
{
return $this->getBlockPrefix();
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => ShopUser::class,
));
}
}

位置窗体类型.php

<?php

namespace AppBundleForm;
use AppBundleEntityBranch;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
class LocationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('address')
->add('city')
->add('cap')
->add('lat')
->add('lng')
;
$builder->add('shop', CollectionType::class, array(
'entry_type' => RegistrationShopUserFormType::class
));
}
public function getBlockPrefix()
{
return 'location';
}
public function getName()
{
return $this->getBlockPrefix();
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Location::class,
));
}

}

实体/: ShopUser.php (来自 PUGMultiUserBundle( -没有 getter 和 setter-

<?php
namespace AppBundleEntity;

use DoctrineORMMapping as ORM;
use PUGXMultiUserBundleValidatorConstraintsUniqueEntity;

/**
* @ORMEntity
* @ORMTable(name="shop")
* @UniqueEntity(fields = "username", targetClass = "AppBundleEntityUser", message="fos_user.username.already_used")
* @UniqueEntity(fields = "email", targetClass = "AppBundleEntityUser", message="fos_user.email.already_used")
*/
class ShopUser extends User
{
/**
* @ORMId
*
* @ORMColumn(type="string")
*/
protected $id;
/**
* @ORMColumn(type="string",nullable=true)
*/
protected $vat;

/**
* @ORMColumn(type="string")
*/
protected $name;
/**
* @ORMColumn(type="string")
*/
protected $lastName;
/**
* @ORMColumn(type="string")
*/
protected $shopName;
/**
* @ORMColumn(type="string",nullable=true)
*/
protected $logo;
}

位置.php -没有一些吸气剂和二传手-

<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineCommonCollectionsArrayCollection;
/**
* Location
*
* @ORMTable(name="location", uniqueConstraints={@ORMUniqueConstraint(name="id_UNIQUE", columns={"id"})}, indexes={@ORMIndex(name="fk_location_shop1_idx", columns={"shop_id"})})
* @ORMEntity
*/
class Location
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=50, nullable=true)
*/
private $name;
/**
* @var integer
*
* @ORMColumn(name="cap", type="integer", nullable=false)
*/
private $cap;
/**
* @var float
*
* @ORMColumn(name="lat", type="float", precision=10, scale=6, nullable=false)
*/
private $lat;
/**
* @var float
*
* @ORMColumn(name="lng", type="float", precision=10, scale=6, nullable=false)
*/
private $lng;
/**
* @var string
*
* @ORMColumn(name="address", type="string", length=45, nullable=false)
*/
private $address;
/**
* @var string
*
* @ORMColumn(name="city", type="string", length=45, nullable=false)
*/
private $city;
/**
* @var AppBundleEntityShopUser
*
* @ORMManyToOne(targetEntity="AppBundleEntityShopUser")
* @ORMJoinColumns({
*   @ORMJoinColumn(name="shop_id", referencedColumnName="id")
* })
* @AssertType(type="AppBundleEntityShopUser")
* @AssertValid()
*
*
*/
private $shop;
/**
* @param ShopUser $shop
*/
public function setShop(ShopUser $shop)
{
$this->shop = $shop;
}

/**
* @return ShopUser
*/
public function getShop()
{
return $this->shop;
}

控制器/:

注册商店用户控制器.php

<?php

namespace AppBundleController;

use AppBundleEntityBranch;
use AppBundleEntityShopUser;
use AppBundleFormBranchFormType;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentBrowserKitRequest;
class RegistrationShopUserController extends Controller
{
public function registerAction(){

return $this->container
->get('pugx_multi_user.registration_manager')
->register(ShopUser::class);

}
}

在我看来,问题出在注册商店用户控制器中,但我不知道如何解决它。请帮忙。

问题是Location实体和ShopUser实体之间存在单向一对多关系。当你在控制器中创建shopUser对象时,就是"不知道"应该与Location存在某种关系。

解决方案是,您必须在ShopUser实体类中定义一个属性,该属性将包含与Location实体类的关系(恕我直言,多对一是您想要的(。 还要考虑在创建新的 shopUser 对象时如何处理该属性 - 如果要在每次新用户注册时创建新位置,则只需在ShopUser实体类构造函数中创建并分配一个新的位置对象即可。

(如果要创建新位置并在新用户注册期间从现有位置中进行选择,则需要更复杂的解决方案。

我解决了这个问题!

在注册商店用户表单类型中.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('lastName')
->add('shopName')
->add('mainLocation', LocationFormType::class);

;
}

位置窗体类型.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('address')
->add('city')
->add('cap')
;
}

我在商店用户中添加.php

/**
* @ORMOneToMany(targetEntity="AppBundleEntityLocation", mappedBy="shop", cascade={"persist"})
*/
protected $locations;
public function __construct()
{
$this->locationss = new ArrayCollection();
}
/**
* @return mixed
*/
public function getLocations()
{
return $this->locations;
}

public function getMainLocation()
{
return $this->locations->first() ? $this->locations->first() : null;
}
public function setMainLocation(Location $location)
{
if ($this->locations->contains($location)) {
return;
}
$this->locations[] = $location;
$branch->setShop($this);
}

它有效!我希望它有用。

相关内容

  • 没有找到相关文章

最新更新