根据文档Registration Form with MongoDB,其中一种解决方案是:
namespace Acme\AccountBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;
/**
* @MongoDB\Document(collection="users")
* @MongoDBUnique(fields="email", groups={"new"})
* @MongoDBUnique(fields="nick", groups={"new"})
*/
class User
{
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\Field(type="string")
* @Assert\NotBlank(groups={"new", "edit"})
* @Assert\Email(groups={"new", "edit"})
*/
protected $email;
/**
* @MongoDB\Field(type="string")
* @Assert\NotBlank(groups={"new", "edit"})
* @Assert\Length(min="4", groups={"new", "edit"})
*/
protected $nick;
/**
* @MongoDB\Field(type="string")
* @Assert\NotBlank(groups={"new", "edit"})
* @Assert\Length(min="4", groups={"new", "edit"})
*/
protected $name;
public function getId()
{
return $this->id;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getNick()
{
return $this->nick;
}
public function setNick($nick)
{
$this->nick = $nick;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
}
namespace Acme\AccountBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Acme\AccountBundle\Document\User;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', EmailType::class)
->add('nick', TextType::class)
->add('name', TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::class,
'validation_groups' => ['new', 'edit']
));
}
}
您使用验证组是因为,根据您的说法,您不想验证唯一字段以进行更新,而只想进行创建。
namespace Acme\AccountBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Acme\AccountBundle\Form\Type\UserType;
use Acme\AccountBundle\Document\User;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
class AccountController extends Controller
{
/**
* @Route("/users/create", name="app_users_create)
*/
public function createAction()
{
$dataContent = json_decode($request->getContent(), true);
if ($dataContent === null || !is_array($dataContent)) {
// Throw exception for invalid format request
}
$dm = $this->get('doctrine_mongodb')->getManager();
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->submit($dataContent);
if ($form->isSubmitted() && $form->isValid()) {
$dm->persist($user);
$dm->flush();
// Return something for success
}
// Return Form error
}
/**
* @Route("/users/edit", name="app_users_edit)
* @Security("is_granted('IS_AUTHENTICATED_REMEMBERED')")
*/
public function updateAction()
{
$dataContent = json_decode($request->getContent(), true);
if ($dataContent === null || !is_array($dataContent)) {
// Throw exception for invalid format request
}
$dm = $this->get('doctrine_mongodb')->getManager();
$user = $this->getUser();
$form = $this->createForm(UserType::class, $user);
$form->submit($dataContent);
if ($form->isSubmitted() && $form->isValid()) {
$dm->flush();
// Return something for success
}
// Return Form error
}
}
更新 :在没有实体的情况下进行,
根据此文档CustomContraint,您可以使用自定义约束名称,例如 UniqueCustom 并将其添加到您的约束数组中。在这个自定义约束(UniqueCustom)中,您可以检查对应的电子邮件(或昵称)是否已存在于您的 MongoDB 中。要忽略此更新检查,您可以使用上述验证组。所以你的约束数组就像(这只是一个想法):
$dataToCheck = [
'email' => [
new Assert\Required(['groups' => ['new', 'edit']]),
new Assert\NotBlank(['groups' => ['new', 'edit']]),
new Assert\Type('string'),
new Assert\Email(['groups' => ['new', 'edit']]),
new UniqCustom(['groups' => ['new']]) // Custom uniq constraint
],
'nick' => [
new Assert\Required(['groups' => ['new', 'edit']]),
new Assert\NotBlank(['groups' => ['new', 'edit']]),
new Assert\Type('string'),
new Assert\Length(['min' => 4, 'groups' => ['new', 'edit']]),
new UniqCustom(['groups' => ['new']])
],
'name' => [
new Assert\Required(['groups' => ['new', 'edit']]),
new Assert\NotBlank(['groups' => ['new', 'edit']]),
new Assert\Type('string'),
new Assert\Length(['min' => 4, 'groups' => ['new', 'edit']]),
],
];
ODM 的过程与用户 ORM 类似。
我希望这个解决方案的尝试可以帮助你。