EasyAdmin在将新条目添加到实体时自动设置一个user_id

发布于 2025-01-21 15:16:05 字数 330 浏览 4 评论 0 原文

我在在线学习网站项目的学校项目中使用了EasyAdmin。 教师可以添加编队,附属于编队的部分以及属于部分的教训。我为每个设置了三个CRUD,并查询,以便登录的用户只能看到他们创建的内容。

问题是,在添加新条目时,EasyAdmin在数据库中未设置登录中的登录,我会收到以下错误: 执行查询时发生了一个例外:sqlstate [23000]:完整性约束违规:1048列'user_id'不能null

我得出的位置,如果没有USER_ID,您将无法在数据库中添加一个条目但是我似乎找不到可以设置easyAdmin的位置,因此在添加新条目时它总是会通过user_id。您可以在某个地方编辑添加操作吗?

谢谢!

I'm using easyadmin with my online learning website project for school.
Teachers can add formations, sections which are attached to formations, and lessons which belong to sections. I set up three cruds for each, and queries so the logged in user can only see content they created.

The issue is that when adding a new entry, easyadmin doesn't set up the logged in user_id in the database, and I get the following error:
An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null

I get where it's coming from, you can't add an entry to the database without the user_id since it's not nullable, but I can't seem to find where I can set up easyadmin so it always passes the user_id when adding a new entry. Can you edit the add action somewhere?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美胚控场 2025-01-28 15:16:05

$ this-> getUser())在您的 Abstract> AbstractCrudController :: CreateNentity )中。 >方法。

//    src: https://symfony.com/bundles/EasyAdminBundle/current/crud.html#creating-persisting-and-deleting-entities
// copied: 2022-04-13

namespace App\Controller\Admin;

use App\Entity\Product;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;

class ProductCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Product::class;
    }

    public function createEntity(string $entityFqcn)
    {
        $product = new Product();
        $product->createdBy($this->getUser());

        return $product;
    }

    // ...
}

As shown in the docs example code below, you need to manually set the user on your entity (currently logged-in user is returned by $this->getUser()) in your AbstractCrudController::createEntity method.

//    src: https://symfony.com/bundles/EasyAdminBundle/current/crud.html#creating-persisting-and-deleting-entities
// copied: 2022-04-13

namespace App\Controller\Admin;

use App\Entity\Product;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;

class ProductCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Product::class;
    }

    public function createEntity(string $entityFqcn)
    {
        $product = new Product();
        $product->createdBy($this->getUser());

        return $product;
    }

    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文