这就是你需要实现的(这就是设计)
为 ChoiceType 元素设置选项时
'expanded' => true,
'multiple' => true,
分组消失(二维数组传递给选项)。
我真的很期待一些好主意。
我在orm学说中创建实体,ID配置为
#[ORM\Id]
#[ORM\Column(type: Types::INTEGER)]
#[ORM\GeneratedValue]
private int $id;
, 实体有唯一键
#[ORM\Column(type: Types::STRING, unique: true)]
private string $username;
#[ORM\Column(type: Types::STRING, unique: true)]
private string $email;
问题是,如果您尝试使用相同的电子邮件或用户名在事务中添加第二条记录,尽管 INSERT 不会被执行并且异常将被捕获
"An exception occurred while executing a query: Duplicate entry 'username' for key 'users.username'"
并完成$conn->rollBack()
, AUTO_INCREMENT 会增加。在下一次添加时(如果在此之前成功的 INSERT 返回 ID = 1),它 (ID) 将不是 2,而是 3。
如何正确实现正确的缩放和/或 AI 整形?
UPD:+ 添加了 prePersist
public function checkExistingRecords(array $fields, LifecycleEventArgs $args) {
$obj = $args->getObject();
$repository = $args->getObjectManager()->getRepository($obj::class)->findOneBy($fields);
if($repository instanceof $obj) {
throw new EntityAlreadyExistsException($obj::class);
}
}
有一个Offer实体
* @ApiResource(
* normalizationContext={"groups"={"offer:read"}, "swagger_definition_name"="Read"},
* denormalizationContext={"groups"={"offer:write"}, "swagger_definition_name"="Write"},
* itemOperations={
* "get",
* "put"={"security"="is_granted('ROLE_USER')"},
* "delete": {
* "security"="is_granted('ROLE_USER')"
* }
* },
* collectionOperations={
* "get",
* "post"={
* "security"="is_granted('ROLE_USER')"
* }
* },
* attributes={
* "order"={"id": "DESC"}
* },
* )
class Offer
{
...
/**
* @var Collection страны оффера
*
* @ManyToMany(targetEntity=Country::class)
* @JoinTable(name="offers_countries",
* joinColumns={@JoinColumn(name="offer_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="country_id", referencedColumnName="id")}
* )
* @Assert\NotBlank
* @Assert\NotNull
* @Assert\Count(min=1)
* @Groups({"offer:read", "offer:write"})
* @ApiSubresource(maxDepth=1000)
*/
private Collection $countries;
/**
* @return Country[]|ArrayCollection<Country>
*/
public function getCountries()
{
return $this->countries;
}
/**
* @param Country $country
* @return $this
*/
public function addCountry(Country $country): self
{
if (!$this->countries->contains($country)) {
$this->countries->add($country);
}
return $this;
}
...
}
使用诸如https://example.com/api/offers/1 "countries": ["/api/countries/10"] 之类的 PUT 请求 ,将 ID 为 10 的国家/地区添加到现有国家/地区。告诉我出了什么问题。谢谢
大家好。我正在新的 Symfony 6.1 上做一个小项目。我连接了设备,因此有必要对密码进行哈希处理。翻遍码头,发现 PasswordHasherInterface
<?php
namespace App\DataFixtures;
use App\Model\User\Entity\User\Email;
use App\Model\User\Entity\User\Id;
use App\Model\User\Entity\User\Role;
use App\Model\User\Entity\User\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
class UserFixture extends Fixture
{
private PasswordHasherInterface $hasher;
public function __construct(PasswordHasherInterface $hasher)
{
$this->hasher = $hasher;
}
public function load(ObjectManager $manager): void
{
$hash = $this->hasher->hash("password");
$user = User::signUpByEmail(
Id::next(),
new \DateTimeImmutable(),
new Email("admin@app.test"),
$hash,
"token"
);
$user->confirmSignUp();
$user->changeRole(Role::admin());
$manager->persist($user);
$manager->flush();
}
}
但我收到一个错误:
In DefinitionErrorExceptionPass.php line 54:
!!
!! Cannot autowire service "App\DataFixtures\UserFixture": argument "$hasher"
!! of method "__construct()" references interface "Symfony\Component\PasswordH
!! asher\PasswordHasherInterface" but no such service exists. Did you create a
!! class that implements this interface?
!!
我的 services.yaml 文件:
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Model/User/Entity/'
- '../src/Kernel.php'
如何在 Symfony 6.1 中散列密码?