从控制器向symfony 3中的类型类发送数据

时间:2017-06-07 01:16:22

标签: php symfony-3.2

我试图从数据库中获取数据并将其传递到表单的下拉列表

这是我要从中获取数据的实体,特别是类型列

<?php

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\TypesRepository")
* @ORM\Table(name="types")
*/
class Types
{

 /**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(name="idtype",type="integer")
 */
 private $idtype;
 /**
 * @ORM\Column(type="string", length=100)
 */
 private $type;

并且这是保存表单的类型类,正如您所看到的,我创建了一个基于我已经完成的某个Internet cherche的构造,构造函数将传递的值添加到变量中,然后调用它EntityType

class sell_form extends AbstractType
{

protected $cat;

public function __construct (Types $cat)
{
$this->cat = $cat;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{


$builder


->add('categorie',EntityType::class,array(
                       "required"=>false,
                        'class' => 'AppBundle\Entity\Types',
                        'choices' => $cat
                        ))

                        ;
                ;
 }
 }

最后这是我使用存储库函数从数据库中对$ cat变量进行初始化的控制器

/**
  * @Route("/Old/buy", name="old_buy")
*/
public function Old_buyAction()
{

return $this->render('/user_module/Old Views/Old_buy.html.twig');
        }
/**
   * @Route("/Old/sell", name="old_sell")
   *@Method({"GET","POST"})
*/
public function Old_sellAction()
{
$cat = $this->getDoctrine()->getManager()->getRepository('AppBundle:Types')-
>tn();



 /*line 122 */



$form = $this->createForm(new sell_form($cat));
 //$form = $this->createForm(sell_form::class,$cat);
 return $this->render("user_module/Old Views/old_sell.html.twig",array(
                                          "myForm"=>  $form->createView(),


                                    ));

这是它显示的错误

Catchable Fatal Error: Argument 1 passed to 
AppBundle\Forms\sell_form::__construct() must be an instance of 
AppBundle\Entity\Types, array given, called in 
C...\OldController.php 
on line 122 and defined`

非常感谢提前

1 个答案:

答案 0 :(得分:0)

$catAppBundle\Entity\Types个实例的数组。您将它们传递给表单构造函数:

public function __construct (Types $cat)
{
    $this->cat = $cat;
}

此时您的代码期望$cat是一个Types个实例,因此崩溃了:

  

AppBundle \ Forms \ sell_form :: __ construct()必须是。的实例   AppBundle \ Entity \ Types,给定数组

你需要一个数组:

public function __construct (array $cat)
{
    $this->cat = $cat;
}