无法在实体Gedmo Doctrine Extensions中找到作为映射属性的slug [slug]。

问题描述 投票:1回答:2

我正在实施 Gedmo Sluggable 在Symfony 2中的注解中,根本无法生成slug,而且每次我试图保存对象时都会出现500错误。

我试着创建一个新的对象,也试着更新一个已经定义了slug的现有对象。

// config.yml
stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            timestampable: true
            uploadable: true
            sluggable: true

对象类

// Journal.php
<?php
namespace Example\JournalBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Example\UserBundle\Entity\User;
use \DateTime;


/**
 * Class Journal
 * @package Example\JournalBundle\Entity
 *
 * @ORM\Entity(repositoryClass="Example\JournalBundle\Entity\JournalRepository")
 * @ORM\Table(name="journal", indexes={
 *     @ORM\Index(name="idx_created", columns={"created"}),
 *     @ORM\Index(name="idx_status", columns={"status"})
 * })
 */
class Journal
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer", options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=100)
     */
    protected $title;

    /**
     * @Gedmo\Slug(fields={"title", "id"})
     * @ORM\Column(length=128, unique=true)
     */
    protected $slug;

    /**
     * @return mixed
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * @param mixed $slug
     * @return $this
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;
        return $this;
    }


}

控制器

<?php
namespace Example\JournalBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Example\JournalBundle\Entity\Journal;

/**
 * Class JournalController
 * @package Example\JournalBundle\Controller
 *
 * @Route("/journals")
 */
class JournalController extends Controller
{
    /**
     * @Route("/slugify")
     * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')")
     * @Template("ExampleJournalBundle::test.html.twig")
     */
    public function slugifyAction()
    {
        $em = $this->getDoctrine()->getManager();
        $user = $this->getUser();

        $journal = new Journal();
        $journal->setBody('this is the body');
        $journal->setTitle('this is the title');
        $journal->setPrivacy(1);
        $journal->setStatus(1);
        $journal->setType(1);
        $journal->setUser($user);
        $em->persist($journal);
        $em->flush();
    }
}

它抛出了以下错误。Sluggable error有什么办法可以解决为什么Sluggable不能用?

php symfony doctrine-orm slug
2个回答
3
投票

我的电脑上的缓存出现了一些问题。

我将我们的repo克隆到一个新的文件夹中,并将相同的注释添加到该版本中,sluggable完美地工作。

在重启电脑后,sluggable开始工作了

所以有一些我没能清除的地方有一个不好的缓存。

¯\_(ツ)_¯

© www.soinside.com 2019 - 2024. All rights reserved.