我需要Symfony2(2.8)的帮助
创建了两个实体Category和CategoryLang。这将是一个类别。
<?php
namespace CreLabs\Bundle\SettingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;
/**
* @ORM\Entity(repositoryClass="CreLabs\Bundle\SettingBundle\Entity\CategoryRepository")
* @ORM\Table(name="category")
*/
class Category
{
const STATUS_ENABLED = 1;
const STATUS_DISABLED = 0;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": 1})
*/
protected $status;
/**
* @ORM\OneToMany(targetEntity="CategoryLang", mappedBy="category", cascade={"persist", "remove"})
*/
protected $locales;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
protected $created_at;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $updated_at;
CategoryLang。它将是我的类别的翻译。
<?php
namespace CreLabs\Bundle\SettingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;
/**
* @Entity
* @ORM\Table(name="category_lang")
*/
class CategoryLang
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $description;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="locales", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* @ORM\Column(type="string", length=2)
*/
protected $locale;
现在我尝试添加createFormBuilder类别实体+ CategoryLang实体字段,但无法正常查看表单。只能使用数组(&#39;映射&#39; =&gt; false并转发来自字段的数据。
$form = $this->createFormBuilder($category);
$form = $form
->add('locale', HiddenType::class, array('mapped' => false, 'data' => $locale))
->add('name', TextType::class, array('mapped' => false, 'data' => $name))
->add('description', TextareaType::class, array('mapped' => false, 'required' => false, 'data' => $description))
->add($label, SubmitType::class, array('label' => $label));
$form = $form->getForm();
顺便说一下,ClassRepository有自定义查询
public function getCategory($category_id, $locale)
{
$repository = $this->getEntityManager()->getRepository('SettingBundle:Category');
$fields = array('p.id', 'category_info.name', 'category_info.description', 'p.status, category_info.locale');
$query = $repository->createQueryBuilder('p')
->select($fields)
->innerJoin('SettingBundle:CategoryLang', 'category_info', 'WITH', 'category_info.category = p.id')
->where('p.id = :category_id AND category_info.locale = :locale')
->setParameter('category_id', $category_id)
->setParameter('locale', $locale)
->getQuery();
$result = $query->getOneOrNullResult() != NULL ? new ArrayCollection($query->getSingleResult()) : false;
return $result;
}
问题: 1. getCategory() - &gt; return not object,但是array或ArrayCollection。无法重用然后需要更新数据库中的字段。
/* @var $category Category */
$category = $this->getCategory($category_id, $form['locale']);
if ($category) {
// $category->setUpdatedAt(new ArrayCollection());
$locale = new CategoryLang();
$locale->setName($form['name']);
$locale->setDescription($form['description']);
$category->addLocale($locale);
$entityManager = $this->getEntityManager();
$entityManager->persist($category);
$entityManager->flush($category);
}
2。无法从子表中获取创建表单构建器信息。
我的问题在哪里?任何人都可以帮我理解我的错误吗?