Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony表单-具有2部手机的客户端唯一验证_Php_Symfony - Fatal编程技术网

Php Symfony表单-具有2部手机的客户端唯一验证

Php Symfony表单-具有2部手机的客户端唯一验证,php,symfony,Php,Symfony,我想同时验证一个表的两列是否唯一。我在两个单独的专栏中为每个客户提供了两部手机,如下所示: /** * @ORM\Entity(repositoryClass="App\Repository\ClientRepository") * @ORM\Table(name="client", * uniqueConstraints={ * @UniqueConstraint(name="phone_second_phone_constraint", *

我想同时验证一个表的两列是否唯一。我在两个单独的专栏中为每个客户提供了两部手机,如下所示:

/**
 * @ORM\Entity(repositoryClass="App\Repository\ClientRepository")
 * @ORM\Table(name="client", 
 *    uniqueConstraints={
 *        @UniqueConstraint(name="phone_second_phone_constraint", 
 *            columns={"phone", "second_phone"})
 *    })
 * @UniqueEntity("email")
 * @UniqueEntity("phone")
 * @UniqueEntity("second_phone")
 */
 class Client {

     /**
      * @ORM\Column(name="phone", type="phone_number", length=50, unique=true, nullable=true)
      * @AssertPhoneNumber(type="any")
      */
     protected $phone = null;

     /**
      * @ORM\Column(name="second_phone", type="phone_number", length=50, unique=true, nullable=true)
      * @AssertPhoneNumber(type="any")
      */
     protected $second_phone = null;
 }
我不想在任何专栏或任何客户机上重复使用手机。

请问您对如何实现这一目标有何想法

谢谢和亲切的问候,
David M.

因此,如果我理解正确,您希望在
电话
第二部电话
中使用电话号码时不能重复使用

因此,将这两个字段组合在一起的唯一键对您没有帮助,因为只有当相同的电话号码出现在相同的字段中时(例如phone=X和second_phone=Y),它才会违反唯一约束。您将能够保存phone=Y和second_phone=X

为了避免这种情况,您必须创建一个自定义约束来检查数据库中是否已经存在电话号码

是关于如何在symfony中创建和使用约束的文档

因此,您的代码将如下所示

您的自定义约束

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
class UniquePhoneNumber extends Constraint
{
    public $message = 'The phone number "{{ phoneNumber }}" is already in use.';
}
您的约束验证器

use Doctrine\ORM\EntityManager;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class UniquePhoneNumberValidator extends ConstraintValidator
{
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }        

    public function validate($phoneNumber, Constraint $constraint)
    {
        $client = $this->entityManager->getRepository(Client::class)->findByPhoneNumber($phoneNumber);

        if (!is_null($client)) {
            $this->context->buildViolation($constraint->message)
                ->setParameter('{{ phoneNumber }}',$phoneNumber)
                ->addViolation();
        }
    }
}
客户端存储库中的某个位置

public function findByPhoneNumber(string $phoneNumber): ?Client
{
    $qb = $this->getEntityManager()->createQueryBuilder('c');
    $qb->select('c')
        ->where('c.phone = :phone OR c.second_phone = :phone')
        ->setParameter('phone',$phoneNumber)
        ->setMaxResults(1);

    return $qb->getQuery()->getOneOrNullResult();
}
然后在手机上使用新的
验证器
第二部手机
,这是一个链接,它取决于您将约束类放置在何处,但通常您必须这样做

  • 导入约束,
    使用App\Validator\constraints作为CustomAssert
  • 如果您使用的是批注,请将批注添加到字段
    @CustomAssert\UniquePhoneNumber


注意:您必须为约束和验证器添加名称空间,并且如果您将约束放置在另一个文件夹App\validator\constraints中,您必须调整实体文件中的use语句

非常感谢!