Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.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 Doctrine2具有多个字段的唯一验证_Php_Validation_Doctrine Orm_Zend Framework2 - Fatal编程技术网

Php Doctrine2具有多个字段的唯一验证

Php Doctrine2具有多个字段的唯一验证,php,validation,doctrine-orm,zend-framework2,Php,Validation,Doctrine Orm,Zend Framework2,我有一个实体预订,它与用户有多对一关系。 每个预订实体都有一个日期字段,该字段对于用户来说应该是唯一的 例如,条目可能如下所示: +----------+---------+ | date | user_id | +----------+---------+ | 12-04-88 | 1 | | 13-04-88 | 1 | | 12-04-88 | 2 | +----------+---------+ +----------+---------+

我有一个实体
预订
,它与
用户
有多对一关系。 每个
预订
实体都有一个
日期
字段,该字段对于用户来说应该是唯一的

例如,条目可能如下所示:

+----------+---------+
|   date   | user_id |
+----------+---------+
| 12-04-88 |       1 |
| 13-04-88 |       1 |
| 12-04-88 |       2 |
+----------+---------+
+----------+---------+
|   date   | user_id |
+----------+---------+
| 12-04-88 |       1 |
| 12-04-88 |       1 |
+----------+---------+
try
{
   $booking = new Booking(1, '12-19-2000');
   em->persist($booking);
   em->flush();
}
catch( \Doctrine\DBAL\DBALException $e )
{ 
    /* the error 23000 should print the unique constrains mysql error */
    if( $e->getPrevious()->getCode() === '23000' ) 
    { 
        echo "entity already exists";
    }
} 
但不是这样:

+----------+---------+
|   date   | user_id |
+----------+---------+
| 12-04-88 |       1 |
| 13-04-88 |       1 |
| 12-04-88 |       2 |
+----------+---------+
+----------+---------+
|   date   | user_id |
+----------+---------+
| 12-04-88 |       1 |
| 12-04-88 |       1 |
+----------+---------+
try
{
   $booking = new Booking(1, '12-19-2000');
   em->persist($booking);
   em->flush();
}
catch( \Doctrine\DBAL\DBALException $e )
{ 
    /* the error 23000 should print the unique constrains mysql error */
    if( $e->getPrevious()->getCode() === '23000' ) 
    { 
        echo "entity already exists";
    }
} 
使用ZF2 REST控制器,添加
预订
实体的代码如下:

输入滤波器 控制器动作 服务 问题: 如何使用默认的条令验证器或自定义条令/ZF2验证器实现这一点

我不喜欢我的方法,似乎我在复制验证器组件的工作,但是,我上次尝试时无法让它与Doctrine Unique Validator一起工作,因为有不止一个字段需要唯一

我想扩展Doctrine Unique validator,它依赖于
用户
,用户将被注入一个构造,然后扩展
验证
方法,使用
日期
用户
字段搜索存储库

我想用一个 依赖于将被注入构造的用户,然后 扩展validate方法以使用这两个日期搜索存储库 和用户字段

在模型中添加一些更改后,您可以同时设置
日期
用户id
主键:

/** @Id @Column(type="integer") */
private $id;
/** @Id @Column(type="date") */
private $date;
我不知道您的框架控制器如何持久化对象,但您可以这样做:

+----------+---------+
|   date   | user_id |
+----------+---------+
| 12-04-88 |       1 |
| 13-04-88 |       1 |
| 12-04-88 |       2 |
+----------+---------+
+----------+---------+
|   date   | user_id |
+----------+---------+
| 12-04-88 |       1 |
| 12-04-88 |       1 |
+----------+---------+
try
{
   $booking = new Booking(1, '12-19-2000');
   em->persist($booking);
   em->flush();
}
catch( \Doctrine\DBAL\DBALException $e )
{ 
    /* the error 23000 should print the unique constrains mysql error */
    if( $e->getPrevious()->getCode() === '23000' ) 
    { 
        echo "entity already exists";
    }
} 

谢谢,我明天会检查这个,我的框架是ZF2,我希望使用
\doctriemodule\Validator\UniqueObject
组件。刚刚尝试了这个,遇到了概述的问题。看起来Doctrine2不支持日期时间对象作为id。我将尝试该解决方案并接受它是否有效:)