是否可以在 nestJs(类验证器 => 自定义验证器)中注入执行上下文或访问当前请求?
import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';
import { Injectable } from '@nestjs/common';
import { Connection } from 'typeorm';
import { InjectConnection } from '@nestjs/typeorm';
@ValidatorConstraint({ name: 'modelExistsConstraint', async: true })
@Injectable()
export class ModelExistsConstraint implements ValidatorConstraintInterface {
constructor(
@InjectConnection('default') private readonly connection: Connection,
) {
}
async validate(value: string, validationArguments: ValidationArguments) {
// here need request or execution context;
// const test = this.context.switchToHttp().getRequest();
const model = await this.connection
.getRepository(validationArguments.constraints[0])
.findOne({ where: { [validationArguments.property]: …Run Code Online (Sandbox Code Playgroud) 我在项目 Zend Framework 2 中使用 Doctrine 2 ORM。我正在努力保持多对多关系。我遵循了此处描述的文档(很多)。在尝试保留数据时:$em->persist($form->getData());我收到错误:
"The class 'Doctrine\ORM\PersistentCollection' was not found in the chain configured namespaces".
Run Code Online (Sandbox Code Playgroud)
有什么建议 ?
为了更清楚,我在下面添加了一些代码:
首先,我注释了像文档所说的实体,对于多对多关系:
/**
* @ORM\ManyToMany(targetEntity="\User\Entity\Client", mappedBy="reportSettings")
*/
private $client;
public function __construct() {
$this->client = new ArrayCollection();
}
Run Code Online (Sandbox Code Playgroud)
和
/**
* @ORM\ManyToMany(targetEntity="\Statistics\Entity\ReportSettings", inversedBy="client")
* @ORM\JoinTable(name="report_client_settings",
* joinColumns={@ORM\JoinColumn(name="client_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="report_setting_id", referencedColumnName="id")})
*/
private $reportSettings;
public function __construct() {
$this->reportSettings = new ArrayCollection();
}
Run Code Online (Sandbox Code Playgroud)
并在控制器中
$form = new UpdateReportSettingsForm();
$form->bind($reportSettings);
$request = new Request();
if ($request->isPost()) …Run Code Online (Sandbox Code Playgroud)