Symfony2:警告:spl_object_hash()期望参数1为对象,给定整数 [英] Symfony2: Warning: spl_object_hash() expects parameter 1 to be object, integer given

查看:75
本文介绍了Symfony2:警告:spl_object_hash()期望参数1为对象,给定整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实体 Project Course 之间存在多对一的关系,因为每个课程可以有多个项目,所以很多项目可以与同一课程相关.

I have a many to one relationship between the entities Project and Course because each course can have many projects so many projects could be related to the same course.

这些是我的实体:

class Project{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    //... other fields ...


    //----------------------- DATABASE RELATIONSHIP ----------------//

    //PROJECT-COURSE - M:1 relationship
    /**
     * @ORM\ManyToOne(targetEntity="Course", inversedBy="project")
     * @ORM\JoinColumn(name="course_id", referencedColumnName="id")
     **/
    private  $course;

class Course{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */

    //... other fields ...

    //----------------------- DATABASE RELATIONSHIP----------------//

    //COURSE-PROJECT 1:M relationship
    /**
     * @ORM\OneToMany(targetEntity="Project", mappedBy="course")
     **/
    private $project;

当我尝试为课程插入一个新项目时出现错误,这是我的表单生成器:

The error appears when i try to insert a new project for my course, this is my form builder:

            $builder
                ->add('name', 'text', array(
                    'attr' => array('class' => 'form-control')))
                ->add('description', 'textarea', array(
                    'attr' => array('class' => 'form-control', 'rows' => '10')))
                ->add('submit', 'submit', array(
                    'attr' => array('class' => 'btn btn-primary')));

我尝试插入这些数据以创建一个Project对象,并用表单的结果填充它,如您所见:

I try to insert these data creating a Project object and filling it with the result of the form as you can see:

$project->setName($form->get('name')->getData());
                $project->setDescription($form->get('description')->getData());
                $project->setPhasesNumber($form->get('phases_number')->getData());
                $project->setPathNumber($form->get('path_number')->getData());
                $project->setYear(date('Y'));
                $project->setCourse(5); //number 5 is just a test

                $em = $this->getDoctrine()->getManager();
                $em->persist($project);
                $em->flush();

问题应该与命令$project->setCourse(5);有关,并且我已经看到,如果删除Project与Course之间的关系,则不会出现该错误.即使我注释了用于设置课程ID的那行,该bug也消失了,所以我认为我与该关系存在问题,但我不知道在哪里.

The problem should be related to the command $project->setCourse(5); and I've seen that if i remove the relationship between Project and Course the bug doesn't appear. The bug disappears even if I comment the line used to set the course id, so I think I have a problem with this relationship but I can't understand where.

我刚刚在stackoverflow上读过类似的其他问题,但这对我没有帮助.

I've just read other question like this on stackoverflow but it doesn't help me.

谢谢.

推荐答案

它正在寻找您使用带有Course实例的对象,只是传递课程ID无效.

Its looking for you to use an object with an instance of Course just passing the ID of the course does not work.

您可以这样做:

//...
$course = $this->getDoctrine()
               ->getManager()
               ->getRepository('Namespace:Course')
               ->findOneById(5);
$project->setCourse($course);
//...

正如Full所提到的,如果您知道该实体已经存在,则可以通过执行以下操作来设置它,而无需进行数据库查找:

As Full mentions if you know that the entity already exists you can just set it without a db lookup by doing:

$project->setCourse($this->getDoctrine()
                         ->getManager()
                         ->getReference('Namespace:Course', 5)
);

这篇关于Symfony2:警告:spl_object_hash()期望参数1为对象,给定整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆