【问题标题】:sending data from controller to a typeclass in symfony 3将数据从控制器发送到 symfony 3 中的类型类
【发布时间】:2017-06-07 01:16:22
【问题描述】:

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

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

<?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`

提前致谢

【问题讨论】:

  • 您的代码期望传递AppBundle\Entity\Types 实例,但您传递了它们的数组。更改 sell_form 构造函数中的类型提示。
  • 非常感谢@MateuszSip 的帮助 Bot 你能澄清更多吗?

标签: php symfony-3.2


【解决方案1】:

$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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多