1. 介绍
我想知道是否有一种方法可以将一组文本字段保存在数据库的新行中,并且在每个字段中,第一个框的值也将随之保存。
2. 说明
这是表格:
当用户点击保存时,它应该保存在数据库中:
我的表结构是
3. 我现在在做什么?!
3.1 - 在我addNew.php
的(来自类文件(我知道我没有遵循命名约定,我的错)中的构造函数中,我从我的控制器(addNewAction()
)中获得了一个语言列表。
我遍历语言数组并生成语言翻译的文本字段。
3.2-当用户单击保存时,我遍历文本字段并检查语言键文本字段并将其值保存在变量中,并在每次遍历翻译文本字段时使用该变量
4.我目前的代码
4.1 - 添加新动作
public function addNewAction()
{
// to add new key and translation associated with it
// an array of optnions which cotains list of languages
$options = $this->getAllLanguages();
$form = $this->createForm(new AddNew($options));
$form->bind($this->getRequest());
if ($form->isValid()) {
foreach($form->getData() as $key => $value){
$oTranslation = new Translations();
if($key == 'languageKey'){
$languageKey = $value;
continue;
}
$locale = $key;
$translation = $value;
$language = $this->getDoctrine()
->getRepository('CodeizSDBTranslatorBundle:Languages');
$query = $language->createQueryBuilder('l')
->select('l.id')
->where('l.locale = :locale')
->setParameter('locale' , $locale)
->getQuery();
$id = $query->getResult();
$oTranslation->setLanguageId($id[0]['id']);
$oTranslation->setLanguageKey($languageKey);
$oTranslation->setTranslation($translation);
$em = $this->getDoctrine()->getManager();
$em->persist($oTranslation);
$em->flush();
}
return $this->redirect($this->generateUrl('codeiz_sdb_translator_addlanguagekey'));
}
return $this->render('CodeizSDBTranslatorBundle:Default:addNewKey.html.twig', array(
'form' => $form->createView(),
));
}
4.2 - addNew.php(来自类)
<?php
namespace Codeiz\SDBTranslatorBundle\Form\Add;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class AddNew extends AbstractType
{
private $languages;
public function __construct($languages){
$this->languages = $languages;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('languageKey');
foreach ($this->languages as $key => $value) {
$builder->add($value['locale'] , 'text' , array('label'=>$value['description']) );
}
}
public function getName()
{
return 'addnew';
}
}
4.3 - 这是我传递给表单以生成存储在中的文本字段的数组 $this->languages
,看起来像
Array
(
[0] => Array
(
[id] => 1
[locale] => en_US
[description] => English - United States
)
[1] => Array
(
[id] => 2
[locale] => fr_FR
[description] => Frenish
)
)
5. 可能的解决方案
这些是我只是认为我可以做的事情:
5.1-使用我的代码 BLAH 。
5.2-将数据库重组为每种语言都是一列。
六,结论
我尝试了很多对我没有效果的事情,而我现在这样做的方式让它看起来不成熟。
Aside from all that, I'm still learning and I really want to solve this problem as-is without restructuring the database and such.
note:
please avoid discussing performance , keep your focus on the problem ... and please change the title as im sure this isnt the right way to describe the problem .. or even better suggest one .. and if you fiend some incorrect technical terms please edit them .. thanks :)