Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从选择字段Symfony获取数据_Php_Symfony_Doctrine - Fatal编程技术网

Php 如何从选择字段Symfony获取数据

Php 如何从选择字段Symfony获取数据,php,symfony,doctrine,Php,Symfony,Doctrine,大家好,我有问题。我需要从实体类填充的选择字段中获取数据 这是我的实体:它是一本包含月份、第一天和最后一天的字典 实体 namespace accountant\ReportBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity */ class Calendar { /** * @var integer * * @ORM\Column(name="id", type="in

大家好,我有问题。我需要从实体类填充的选择字段中获取数据

这是我的实体:它是一本包含月份、第一天和最后一天的字典

实体

namespace accountant\ReportBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Calendar
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="month", type="string", length=255)
     */
    private $month;
}
我将此表的装置加载到db并创建formBuilder:

FormType

class ReportFormType extends AbstractType
{
     public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('month','entity', array(
                'class' => 'ReportBundle:Calendar',
                'property' => 'month',
                'expanded' => false,
                'multiple' => false
            ));
    }
}
现在我想从所选月份到获取数据,但
$data
null

控制器

 /**
 * @Route("/report", name="report")
 * @Template()
 */
public function indexAction(Request $request)
{
    $form = $this->createForm(new ReportFormType());
    $form->handleRequest($request);
    $data = $form->getData();

    var_dump($data);    // $data is null!       

    return array('form' => $form->createView());
}
小树枝

<form method="get" action="{{ path('report') }}">
    <div class="form-group">
        {{ form_row(form.month, {'label': 'Select Month:', 'attr': {'class': 'form-control'}}) }}
    </div>
    <div class="form-group">
        <input type="submit" value="Show Report" class="btn btn-warning btn-block"/>
    </div>
</form>
这是我的控制器

public function indexAction(Request $request)
{
    $form = $this->createForm(new ReportFormType());
    $form->handleRequest($request);
    $data = $form->get('month')->getData();
    // i check data
    var_dump($data);

    return array(
        'form' => $form->createView());
}
更新2

public function indexAction(Request $request)
{
    // ...

    $data = $form->get('month')->getData();

    // ...
}
我解决了我的问题,为此在模板中更改了我的表单:

<form method="post" action="{{ path('report') }}" novalidate="novalidate">
{{ form_errors(form) }}
<div class="form-group">
{{ form_row(form.month, {'label': 'Select month', 'attr': {'class': 'form-control'}}) }}
{{ form_rest(form) }}
</div>
<div class="form-group">
    <input type="submit" value="Show report" class="btn btn-warning btn-block"/>
</div>

{{form_errors(form)}}
{{form_行(form.month,{'label':'Select month',attr':{'class':'form control'}}}}}
{{form_rest(form)}


我将方法更改为
post
并添加
form\u rest

更改您的表单,如:

use accountant\ReportBundle\Entity\Calendar;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ReportFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('month','entity', array(
                'class' => 'ReportBundle:Calendar',
                'property' => 'month',
                'expanded' => false,
                'multiple' => false
            ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'=> Calendar::class,
        ));
    }
}
请记住,名称空间部分的第一个字母是大写的(
accountary
,而不是
accountary

更新 根据OP的评论,您可能会在控制器中获得
month
数据:

Symfony 2

public function indexAction(Request $request)
{
    // ...

    $data = $form->get('month')->getData();

    // ...
}
Symfony 3&4

public function indexAction(Request $request)
{
    // ...

    $data = $form['month']->getData();

    // ...
}

您需要在主窗体上配置数据类。您的Symfony版本?@Trix 2.7和7.2.7 phpI添加了“数据类”,但我仍然得到空值。请检查更新。我之前有此更新。我在documentaction:中找到了这个,但仍然为空。