【问题标题】:Argument 1 passed to?参数 1 传递给?
【发布时间】:2012-09-24 14:33:58
【问题描述】:

我尝试在 symfony2.1 中提交表单,但出现以下错误,我创建表单学生注册并尝试提交,我为此查看了可能的论坛,但没有得到任何适当的解决方案。

Error:Catchable Fatal Error: Argument 1 passed to
Frontend\EntityBundle\Entity\StudentRegistration::setIdCountry()
must be an instance of Frontend\EntityBundle\Entity\MasterCountry, string given,
called in C:\wamp\www\careerguide\src\Frontend\HomeBundle\Controller\RegistrationController.php
on line 41 and defined in C:\wamp\www\careerguide\src\Frontend\EntityBundle\Entity\StudentRegistration.php line 1253 

在控制器中我有:

$student_account = new \Frontend\EntityBundle\Entity\StudentRegistration();
$params = $request->get('student_registration');
$student_account->setIdCountry($params['idCountry']);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($student_account);
$em->flush();

实体类:

/**
 * @var MasterCountry
 *
 * @ORM\ManyToOne(targetEntity="MasterCountry")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_country", referencedColumnName="id_country")
 * })
 */
private $idCountry;

请建议我如何解决此错误?

【问题讨论】:

    标签: php symfony symfony-2.1


    【解决方案1】:

    当您使用原则设置多对一关系时,持有此关系的属性是相关实体的对象,而不是 id。它作为 id 保存在数据库中,但 Doctrine 会在您获取它时创建完整的对象,并在您持久保存对象时将其转换为 id。因此,为了反映这一点,该属性不应称为 $idCountry,而应改为 $country(这不是强制性的,您可以随意调用它,但这会使一切更清楚)。然后 setter 应该是 setCountry() 并且它应该接受 MasterCountry 对象。

    因此,当您从表单中收到国家/地区 ID 时,您应该将其转换为 MasterCountry 对象(通过从数据库中获取),将此对象设置在 studentRegistration 中,然后将其持久化。比如:

    $student_account = new \Frontend\EntityBundle\Entity\StudentRegistration();
    $params = $request->get('student_registration');
    $country = $this->getDoctrine()->getRepository('AcmeStoreBundle:MasterCountry')
            ->find($params['idCountry']);
    $student_account->setCountry($country);
    $em = $this->getDoctrine()->getEntityManager();
    $em->persist($student_account);
    $em->flush();
    

    虽然这应该可行,但这不是 Symfony 处理表单的方式。您应该创建一个 Form 对象,然后绑定并验证它。然后您不必处理请求参数等...我建议您仔细阅读 Symfony 文档的这一章:

    http://symfony.com/doc/current/book/forms.html

    【讨论】:

      【解决方案2】:

      我认为问题在于 $params 不是请求参数:

      $params = $request->get('student_registration'); // looks like a String http param value
      $student_account->setIdCountry($params['idCountry']); //what could be $params['idCountry'] 
      

      你应该想要

      $studentRegistrationId = $request->get('student_registration');
      $studentRegistration = getStudentFromId( $studentRegistrationId); // I don't know how you retrieve the $studentRegistration object
      $idCountry = $request->get('idCountry');
      $student_account->setIdCountry($idCountry);
      

      我敢肯定这不完全是这样,但对我来说,它更有意义。

      【讨论】:

        【解决方案3】:

        问题在于,通过使用学说设置关系,您声明“$idCountry”是 Country 对象。

        如果您设置 idCountry 本身,它将作为快捷方式工作(Doctrine 允许您设置 id 而不是对象),但按照惯例,属性应该命名为 $country,而不是 $idCountry,因为这个想法是在您编码时仅通过引用对象来抽象出 id 的存在。

        显示此错误是因为可能有一个类型提示将其强制为对象,因此请在 StudentRegistration 类中查找类似内容:

        public function setIdCountry(MasterCountry $idCountry)
        

        或者类似的东西,如果你想设置一个 id,你想删除类型提示($idCountry 之前的 MasterCountry)。如果您不想触摸它,那么您可能需要检索 country 对象并使用它而不仅仅是 id。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-03-24
          • 1970-01-01
          • 2023-03-10
          • 2021-12-10
          • 1970-01-01
          • 2015-02-26
          • 2014-05-28
          • 2017-12-23
          相关资源
          最近更新 更多