doctrine2 slugable 和 datafixtures

发布于 2024-12-05 20:53:35 字数 2335 浏览 4 评论 0原文

我正在尝试使对象的字段可slugable。

模型看起来像:

namespace myBundle\Bundles\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * myBundle\Bundles\BlogBundle\Entity\Category
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Category
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $title
     *
     * @Gedmo\Sluggable
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @Gedmo\Slug(separator="-", updatable=false, unique=true)
     * @ORM\Column(name="slug", type="string", length=255, unique=true)
     */
    private $slug;

    // other properties and methods

装置:

namespace myBundle\Bundles\BlogBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use tooMuch\Bundles\BlogBundle\Entity\Category;

class LoadCategoryData extends AbstractFixture implements OrderedFixtureInterface
{
    public function load($manager)
    {
        $this->generateCategory($manager);
    }

    public function generateCategory($manager)
    {
        for ($i=0; $i < 10; $i++) { 
            $category = new Category();
            $category->setTitle('Category '.$i);
            $manager->persist($category);
            $manager->flush();

            $this->addReference('category'.$i, $category);
            unset($category);
        }
    }

架构创建:

# sf doctrine:schema:create
ATTENTION: This operation should not be executed in a production environment.

Creating database schema...
Database schema created successfully!

但是当我尝试添加装置时:

# sf doctrine:fixtures:load
  > purging database
  > loading myBundle\Bundles\BlogBundle\DataFixtures\ORM\LoadCategoryData



  [PDOException]                                                                      
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'slug' cannot be null  



doctrine:fixtures:load [--fixtures[="..."]] [--append] [--em="…"]

# 

有什么想法吗?

I'm trying to make a Field of a Object slugable.

Model looks like:

namespace myBundle\Bundles\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * myBundle\Bundles\BlogBundle\Entity\Category
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Category
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $title
     *
     * @Gedmo\Sluggable
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @Gedmo\Slug(separator="-", updatable=false, unique=true)
     * @ORM\Column(name="slug", type="string", length=255, unique=true)
     */
    private $slug;

    // other properties and methods

The Fixtures:

namespace myBundle\Bundles\BlogBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use tooMuch\Bundles\BlogBundle\Entity\Category;

class LoadCategoryData extends AbstractFixture implements OrderedFixtureInterface
{
    public function load($manager)
    {
        $this->generateCategory($manager);
    }

    public function generateCategory($manager)
    {
        for ($i=0; $i < 10; $i++) { 
            $category = new Category();
            $category->setTitle('Category '.$i);
            $manager->persist($category);
            $manager->flush();

            $this->addReference('category'.$i, $category);
            unset($category);
        }
    }

schema create:

# sf doctrine:schema:create
ATTENTION: This operation should not be executed in a production environment.

Creating database schema...
Database schema created successfully!

but then when I'm trying to add the fixtures:

# sf doctrine:fixtures:load
  > purging database
  > loading myBundle\Bundles\BlogBundle\DataFixtures\ORM\LoadCategoryData



  [PDOException]                                                                      
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'slug' cannot be null  



doctrine:fixtures:load [--fixtures[="..."]] [--append] [--em="…"]

# 

any ideas?

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

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

发布评论

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

评论(1

明媚如初 2024-12-12 20:53:35

问题不在于装置,而在于弹头生成本身。如果设置正确,您绝对不应将 $slug 设置为 null

您确定已附加 Sluggable 侦听器吗?

The problem isn't in the fixtures, it's in Slug generation itself. If it's properly set up, you should never have $slug as null

Are you sure you have Sluggable listener attached?

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