我正在使用symfony构建一个站点并使用Knp Translatable来处理Sonata Admin的多语言管理。但是,我发现自己在一个角落里可以这么说。我有一个函数来强化一个名字,但是symfony会抛出一个“Entity has not field slug”,但问题是,该字段在我的翻译表格中应该如此。
我已经看过这个:Knplabs translatable: How to find an entry by a translatable field?
但它没有解决任何问题。
这是我的实体:
<?php
namespace Spec\Bundle\SiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* Artiste
*
* @ORM\Table()
* @ORM\Entity()
*/
class Artiste
{
use ORMBehaviors\Translatable\Translatable;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="website_url", type="string", length=255, nullable=true)
*/
private $websiteUrl;
/**
getters and setters
**/
public function __call($method, $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
}
这是EntityTranslation
<?php
namespace Spec\Bundle\SiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class ArtisteTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* @ORM\Column(type="string", length=255)
*/
protected $name;
/**
* @ORM\Column(type="string", length=255)
*/
protected $description;
/**
* @var string
*
* @ORM\Column(name="slug", type="string", length=255)
*/
protected $slug;
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string
* @return null
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string
* @return null
*/
public function setDescription($description)
{
$this->description = $description;
}
public function __toString()
{
return $this->getName();
}
public function setSlug($slug){$this->slug = $slug;}
public function getSlug(){return $this->slug;}
}
这是回购函数:
<?php
namespace Spec\Bundle\SiteBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Knp\DoctrineBehaviors\ORM as ORMBehaviors;
class ArtisteRepository extends EntityRepository
{
public function findOneBySlug($slug){
$qb = $this->createQueryBuilder("a");
$qb
->select("a, t")
->join("a.translations", "t")
->where("t.slug = :slug")
->setParameter("slug", $slug);
return $qb->getQuery()->getOneOrNullResult();
}
}
最后,这就是我如何调用该函数:
public function prePersist($artiste) {
$helper = $this->container->get("spec.site.helper");
$slugFr = $helper->slugify($artiste->translate("fr")->getName());
$slugEn = $helper->slugify($artiste->translate("en")->getName());
/*
the helper function is :
public function slugify($slug){
$repo = $this->em->getRepository("SpecSiteBundle:Artiste");
$slugToReturn = $repo->findOneBySlug($slug);
return $slugToReturn;
}
*/
$artiste->translate("fr")->setSlug($slugFr);
$artiste->translate("en")->setSlug($slugEn);
$artiste->upload();
}
symfony给了我这个错误:
Entity 'Spec\Bundle\SiteBundle\Entity\Artiste' has no field 'slug'. You can therefore not call 'findOneBySlug' on the entities' repository
答案 0 :(得分:0)
我解决了它,只是因为我没有在我的实体中指定我的存储库类。这是一个菜鸟的错误。