我刚刚安装了理论扩展来使用Sluggable。
我要做的是:
composer.json
"stof/doctrine-extensions-bundle": "1.2.*@dev"AppKernel.php
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),app/config/config.yml
stof_doctrine_extensions:
orm:
default:
sluggable: trueDjoo\AppliBundle\Entity\Nomenclature.php
namespace Djoo\AppliBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\DBAL\Types\SmallIntType;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Nomenclature
*
*
* @ORM\Table(name="app_nomenclature")
* @ORM\Entity
*/
class Nomenclature
{
.....
/**
* @var string
*
* @ORM\Column(name="titre", type="string", length=200, nullable=false)
*/
private $titre;
/**
* @var string
*
* @ORM\Column(name="finess", type="string", length=10, nullable=true)
*/
private $finess;
/**
* @Gedmo\Slug(fields={"titre","finess"},suffix=".html")
* @ORM\Column(length=128, unique=true,nullable=true)
*/
private $slug;
public function getSlug() {
return $this->slug;
}
public function setSlug($slug){
$this->slug = $slug;
return $this;
}
}在我的控制器中,我使用它为datatable中的旧值生成段塞:
$filterBuilder = $this->get('doctrine.orm.entity_manager')>getRepository('DjooAppliBundle:Nomenclature')->createQueryBuilder('e')->orderBy('e.titre', 'asc');
$query = $filterBuilder->getQuery();
$nomenclatures = $query->getResult();
foreach($nomenclatures as $nomenclaturee){
$nomenclature->setSlug(null);
$this->get('doctrine.orm.entity_manager')->persist($nomenclature);
$this->get('doctrine.orm.entity_manager')->flush();
}我没有错误,但我的旧值是空段塞。我试图创造一个新的元素,我有一个很好的鼻涕虫。你有想法吗?
谢谢
发布于 2014-10-17 14:04:29
若要更改弹格,必须更改相关属性。您可以在$titre的末尾添加一个空格,保存它,更改它,然后再次保存它。会冲走鼻涕虫。
发布于 2016-08-05 14:14:21
$uow = $em->getUnitOfWork();
$uow->propertyChanged($entity, 'slug', NULL, NULL);
$uow->scheduleForUpdate($entity);
$em->flush();发布于 2017-03-30 17:31:33
为什么它不适用于OP,而适用于其他人(例如。@gregor):
在创建段塞时,您的第一本能是使用以下列配置创建弹状体属性:
..
@ORM\Column(unique=true, nullable=false)
private $slug;
..在运行app/console doctrine:schema:update时,将产生2个sql语句:
ALTER TABLE ... ADD slug ... NOT NULL
CREATE UNIQUE INDEX...`. 默认情况下,列slug将被值'' (空字符串)填充,这将导致第二条语句失败(Duplicate entry '')错误。现在你有两个选择:
选择A:忽略第二语句的失败
如果您忽略了错误,然后尝试使用文档化的方法$entity->setSlug(null)来手工生成段塞,那么一切都会正常工作。这是因为通过使用$entity->setSlug(null),您可以让Doctrine知道属性slug被更改(从''改为null),而这反过来又会触发内部$uow->propertyChanged()和$uow->scheduleForUpdate() (感谢@Sebastian作为他的例子)。Sluggable扩展也会注意到这一变化,并重新生成弹状体。现在,由于所有的段塞都是唯一的,下次运行app/console doc:schema:update时,它将成功地在slug上创建索引,并且您的模式将完全同步。
选择B:在注意到错误后,将slug nullable字段修改为字段,您的本能反应是将slug字段标记为nullable,以便索引创建成功:
..
@ORM\Column(unique=true, nullable=true)
private $slug;
..这将导致slug列将NULL作为其默认值。现在,当您尝试使用文档化的$entity->setSlug(null)方法时,它将无法工作(就像OP发布的那样)。这是因为当$entity->slug属性已经是NULL时。因此,当您使用$entity->setSlug(null)时,Doctrine不会检测到任何更改,因此不会触发Sluggable再生行为。为了触发这些变化,有两个答案:
$entity -> setTitre($titre." "); (但这将导致额外的空间,之后您将不得不修剪)。希望这能帮助您更好地理解理论和扩展的内部工作。
https://stackoverflow.com/questions/26426322
复制相似问题