小编use*_*505的帖子

NestJs 在自定义验证器(类验证器)中获取请求实例或执行上下文

是否可以在 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)

typescript class-validator nestjs

8
推荐指数
1
解决办法
1635
查看次数

在链配置的命名空间中找不到类“Doctrine\ORM\PersistentCollection”

我在项目 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)

orm doctrine-orm zend-framework2

5
推荐指数
1
解决办法
5715
查看次数