我正在开发一个新项目,该项目几乎是使用Symfony框架完成的,但是我习惯于CodeIgnitor Framework的问题,基本上作为Java开发人员/ Android的很多东西我在Web上工作时感到困惑发展所以情况如下: 该网站有一个用户端和一个管理员端(我正在管理端),所以在数据库中有这些表,我真的不明白他们为什么这样构建,但这不是问题
我想知道的是如何使用表单或任何其他方式在service_category_translation中添加带有相应翻译的service_category字段
这是ServiceCategory实体
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use JMS\Serializer\Annotation as Serializer;
/**
* class ServiceCategory
*
* @ORM\Table(name="service_category")
* @ORM\Entity
*
* @Serializer\ExclusionPolicy("all")
*/
class ServiceCategory
{
use ORMBehaviors\Timestampable\Timestampable;
use ORMBehaviors\SoftDeletable\SoftDeletable;
use ORMBehaviors\Translatable\Translatable;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Serializer\Expose
* @Serializer\Groups({"general-information", "service-offer"})
*/
private $id;
/**
* @var ServiceGroup
*
* @ORM\OneToMany(targetEntity="ServiceGroup", mappedBy="serviceCategory")
*
* @Serializer\Expose
* @Serializer\Groups({"service-offer"})
*/
private $serviceGroup;
/**
* Constructor
*/
public function __construct()
{
$this->serviceGroup = new ArrayCollection();
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return $this->getName() ? $this->getName() : '';
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Get translated name
*
* @return string
*
* @Serializer\VirtualProperty
* @Serializer\SerializedName("name")
* @Serializer\Groups({"invoice-list", "service-offer"})
*/
public function getName()
{
if($this->getTranslations()->get($this->getCurrentLocale()) == null){
return 'sorry';
}
return $this->getTranslations()->get($this->getCurrentLocale())->getName();
}
/**
* Add serviceGroup
*
* @param ServiceGroup $serviceGroup
*
* @return ServiceCategory
*/
public function addServiceGroup(ServiceGroup $serviceGroup)
{
$this->serviceGroup[] = $serviceGroup;
return $this;
}
/**
* Remove serviceGroup
*
* @param ServiceGroup $serviceGroup
*/
public function removeServiceGroup(ServiceGroup $serviceGroup)
{
$this->serviceGroup->removeElement($serviceGroup);
}
/**
* Get serviceGroup
*
* @return Collection
*/
public function getServiceGroup()
{
return $this->serviceGroup;
}
}
这是ServiceCategoryTranslation Entity
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* Class ServiceCategoryTranslation
*
* @package CoreBundle\Entity
*
* @ORM\Entity
* @ORM\Table(name="service_category_translation")
*/
class ServiceCategoryTranslation
{
use ORMBehaviors\Translatable\Translation;
use ORMBehaviors\Timestampable\Timestampable;
use ORMBehaviors\SoftDeletable\SoftDeletable;
/**
* @ORM\Column(type="string", length=255)
*/
protected $name;
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string
* @return null
*/
public function setName($name)
{
$this->name = $name;
}
public function __toString() {
return $this->name;
}
}
我怎样才能做到这一点? 请不要引导我参考symfony或doctrine文档我已经在那里丢失了两天而且我在时间表上跑得很晚
提前致谢
答案 0 :(得分:1)
你有一个从ServiceCategory(1)到ServiceCategoryTranslations(很多)的一对多关联,因为我认为你 将管理该类别的转换。这必须是双向关联,看看 here 您必须添加属性来管理实体并描述关联。我会用注释来做。
use Doctrine\Common\Collections\ArrayCollection;
class ServiceCategory
{
/**
* @OneToMany(targetEntity="ServiceCategoryTranslation", mappedBy="serviceCategory")
**/
private $translations;
public function __construct()
{
$this->translations = new ArrayCollection();
}
/**
* @return ServiceCategoryTranslation[]
*/
public function getStandort(){
return $this->translations;
}
/**
* @param ArrayCollection $translations
* @return ServiceCategory
*/
public function setTranslations(ArrayCollection $translations)
{
$this->translations->clear();
foreach ($translations as $translation){
$this->addTranslation($translation);
}
return $this;
}
/**
* @param ServiceCategoryTranslation $translation
* @return ServiceCategory
*/
public function addTranslation(ServiceCategoryTranslation $translation){
/* this is a way to keep the integerity */
$translation->setServiceCategory($this);
if(!$this->translation){
$this->translations = new ArrayCollection();
}
$this->translations->add($translation);
return $this;
}
/**
* @param ServiceCategoryTranslation $translation
* @return ServiceCategory
*/
public function removeStandort(ServiceCategoryTranslation $translation){
$this->translations->removeElement($translation);
return $this;
}
}
class ServiceCategoryTranslation
{
/**
* @ManyToOne(targetEntity="ServiceCategory", inversedBy="translations")
* @JoinColumn(name="translatable_id", referencedColumnName="id")
**/
private $serviceCategory;
/**
* @param ServiceCategoryTranslation $translation
* @return ServiceCategoryTranslation
*/
public function setServiceCategory(ServiceCategory $serviceCategory){
$this->serviceCategory = $serviceCategory;
return $this;
}
/* getter analog */
}