Symfony 4.2.3 - 当用户注册电子邮件域时设置ManyToOne Id

问题描述 投票:0回答:1

My page

用户实体:

<?php
s
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Validator\Constraints as AssertPerso;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\Table(name="app_user")
 */
class User implements UserInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne (targetEntity="Formation")
     */
    private $customerName;

    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return CustomerName
     */
    public function getCustomerName()
    {
        return $this->customerName;
    }

    public function setCustomerName($customerName): self
    {
        $this->customerName = $customerName;
        return $this;
    }


}

形成实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\FormationRepository")
 */
class Formation
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     */
    private $customerName;

    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return CustomerName
     */
    public function getCustomerName()
    {
        return $this->customerName;
    }

    public function setCustomerName($customerName): self
    {
        $this->customerName = $customerName;

        return $this;
    }

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

}

和我的表格UserType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('customerName', null, array(
                'label'    => 'Client ',
                'required' => true)
        )
        ->add('submit', SubmitType::class, ['label_format' => "Register", 'attr'=>['class'=>'btn-primary btn-block']]);

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        array($this, 'preSetData')
    );
}

public function preSetData(FormEvent $event)
{

    $form = $event->getForm();
    $user = $event->getData();

         // $form->remove('customerName');


    $user->setEnabled(true);
}

在我的数据库“编队”中,我有customer_name“xxx”。

我想,当用户注册时,如果此电子邮件类似于“xxxx”,并且如果此电子邮件的域与customer_name不对应,则在我的表“user”中将customer_name_id设置为电子邮件的域名,显示错误

你能帮我吗 ?谢谢


使用Rebru响应进行编辑:

我成功地使自定义验证器检查域是否是“形成”中的现有域

public function validate($value, Constraint $constraint)
{
    $mailParts = explode('@', $value);
    $mailParts = explode('.', $mailParts[1]);
    $domain = $mailParts[0];

            if(!$this->entityManager->getRepository(Formation::class)->findBy(array('customerName' => $domain))){
                $this->context->buildViolation($constraint->messageWrongDomain)
                    ->setParameter('{{ value }}', $this->formatValue($value))
                    ->setCode(Email::INVALID_FORMAT_ERROR)
                    ->addViolation();
                return;
            }
}

但是现在我不知道如何在用户注册时自动设置customer_name_id?

谢谢

编辑2:我只是尝试在用户表中设置customer_name_id。

在我的UserController中:

$mailParts = explode('@', $user->getEmail());
            $mailParts = explode('.', $mailParts[1]);
            $domain = $mailParts[0];

            $customerNameId = $entityManager->getRepository(Formation::class)->findBy(array('customerName' => $domain));

            $customerNameId = $customerNameId[0]->getId();

我收到以下错误:

Expected value of type "App\Entity\Formation" for association field "App\Entity\User#$customerName", got "integer" instead.
symfony
1个回答
0
投票

好吧,那你的设计是错的 - 顺便说一句。我建议永远不要使用manytomany over strings。

实现这一目标的正确途径是使用自定义验证器......如下所述:https://symfony.com/doc/current/validation/custom_constraint.html

短:

  • 所有客户的表(id,name,formation_id)
  • 包含所有格式的表(id,name,domain)

在自定义验证器中,您可以拆分客户电子邮件(explode(“@”,$ email))并针对编队表中的域进行检查。如果匹配,则验证器应返回true,否则返回false。如果验证器为true,则可以保存客户。

使用此方法,表单永远无效,直到它与自定义验证器匹配。

© www.soinside.com 2019 - 2024. All rights reserved.