Php 带preUpdate、prePersist的条令命令行插入

Php 带preUpdate、prePersist的条令命令行插入,php,symfony,doctrine-orm,doctrine,Php,Symfony,Doctrine Orm,Doctrine,我可以直接使用SQL在条令控制的数据库中插入数据 php bin/console doctrine:query:sql \ "INSERT INTO meta_info(app_title,enabled) VALUES('mapapp',1)" 然而,我的实体有这样的preUpdate(),prePersist() 所以我想使用DQL插入,而不是直接使用SQL 有没有办法做到这一点 /** * @ORM\PreUpdate */ public function preUpdate()

我可以直接使用SQL在条令控制的数据库中插入数据

php bin/console doctrine:query:sql \
    "INSERT INTO meta_info(app_title,enabled) VALUES('mapapp',1)"
然而,我的实体有这样的
preUpdate()
prePersist()

所以我想使用DQL插入,而不是直接使用SQL

有没有办法做到这一点

/**
* @ORM\PreUpdate
*/
public function preUpdate()
{
    $this->updatedAt = new \DateTime;
}
/**
* @ORM\PrePersist
*/
public function prePersist(){

    $this->createdAt = new \DateTime;
    $this->updatedAt = new \DateTime;
}
所以我想直接插入DQL而不是SQL

虽然存在用于DQL查询的命令(
doctor:query:DQL
),但DQL不支持
INSERT
查询,如您所见

您最好的选择是,插入实体管理器,并在那里创建和持久化实体

一个非常简单的命令如下所示:

class FooCreate extends Command {

    protected static $defaultName = 'foo:entity:create';
    /**
     * @var EntityManagerInterface
     */
    private $manager;

    public function __construct( EntityManagerInterface $manager ) {
        $this->manager = $manager;
        parent::__construct();
    }

      protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $entity = new Entity('app_title', 'enabled');
        $this->manager->persist($entity);
        $this->manager->flush();

    }
}

您可以将其称为控制台foo:entity:create

,因此创建一个命令并通过entityManager插入,否?Whitebear,以下答案有任何反馈吗?请记住,使用DQL不会触发
@ORM\PreUpdate