なぜ私はsymfonyの5上のDateTime制約を使用した場合、「この値は文字列型である必要があり、」受信できますか?

Dimitrie-トーマスFurdui:

私は(関連部分のみを添付)以下のエンティティを持っています:

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ApiResource(mercure=true)
 * @ORM\Entity(repositoryClass="App\Repository\EventRepository")
 */
class Event {
    /**
     * @ORM\Column(type="datetime")
     * @Assert\DateTime
     * @Assert\NotNull
     */
    private $createdAt;

    public function __construct() {
        $this->createdAt = new \DateTime();
    }

    public function getCreatedAt(): ?\DateTimeInterface {
        return $this->createdAt;
    }

    public function setCreatedAt(\DateTimeInterface $createdAt): self {
        $this->createdAt = $createdAt;
        return $this;
    }
}

そのリポジトリ:

class EventRepository extends ServiceEntityRepository {
    public function __construct(ManagerRegistry $registry) {
        parent::__construct($registry, Event::class);
    }
}

(ポストマンや闊歩UIを介した)イベントエンドポイントへのPOSTリクエストを作成する場合、それは次の例外で失敗します。

プロファイラ

Yivi:

私はあなたがアサーションの間違ったを使用していると信じています。

Date 文字列または文字列にキャストできるオブジェクトを期待そして、DateTimeInterfaceどちらもありません。

あなたは使うべきType 制約を

/**
 * @Assert\Type("\DateTimeInterface")
 */
 private $createdAt;

使用する能力Assert\Date検証するDateTimeオブジェクトはsymfony 4.2で廃止されました、そしてsymfonyの5.0にそれが完全に除去されました

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=10032&siteId=1