Symfony2 Array Collection什么都不返回

时间:2012-12-28 17:44:50

标签: php arrays symfony

每当我尝试使用其ID获取特定教室内的所有用户时,它都不会返回任何内容。我有一个user_id和class_id

的连接表
Classroom.orm.yml
  id:
    classID:
      column: class_id
      type: integer
      generator: { strategy: AUTO }
  fields:
  manyToMany:  // <---- this is the map
    classUsers:
      targetEntity: acme\UserBundle\Entity\User
      mappedBy: userClassrooms
  oneToMany:
    classAnnouncement:
      targetEntity: acme\ClassroomBundle\Entity\Announcements
      mappedBy: announcementClass
  manyToMany:
    classLesson:
      targetEntity: acme\ClassroomBundle\Entity\Lessons
      inversedBy: lessonClass
      joinTable:
        name: classroom_lessons
        joinColumns:
          fk_class_id:
            referencedColumnName: class_id
        inverseJoinColumns:
          fk_lesson_id:
            referencedColumnName: lesson_id


User.orm.yml

acme\UserBundle\Entity\User:
  type:  entity
  table: reg_user
  id:
    userID:
      column: user_id
      type: integer
      generator: { strategy: AUTO }
  fields:
...
  manyToMany: //this is the map
    userClassrooms:
      targetEntity: acme\ClassroomBundle\Entity\Classroom
      inversedBy: classUsers
      joinTable:
        name: classroom_users
        joinColumns:
          fk_user_id:
            referencedColumnName: user_id
        inverseJoinColumns:
          fk_class_id:
            referencedColumnName: class_id

Classroom.php
...   
public function __construct()
    {
        $this->classUsers = new ArrayCollection();  
//other array collections
        $this->classAnnouncement = new ArrayCollection(); 
        $this->classLesson = new ArrayCollection();
    }
    public function addClassUser(\acme\UserBundle\Entity\User $classUsers)
    {
        $this->classUsers[] = $classUsers;
           return $this;
    }

    public function removeClassUser(\acme\UserBundle\Entity\User $classUsers)
    {
        $this->classUsers->removeElement($classUsers);
    }
    public function getClassUsers()
    {
        return $this->classUsers;
    }

Controller
public function StudentClassroomAction($classID)
    {

        $class_repository = $this->getDoctrine()->getRepository('acmeClassroomBundle:Classroom');
        $classroom = $class_repository->find($classID); //im sure this stores an object of classroom
        $userinfo = $classroom->getClassUsers(); //THIS IS THE THING!!!

 //first i tried rendering it in twig. still gives me nothing
  /*return $this->render('acmeClassroomBundle:Classrooms:Student.html.twig',array(
            'classroom' => $classroom, 
            'classuser' => $userinfo));
            */
//I change it to response to know if something is stored in $userinfo it returns an empty page rather than an error.Then $userinfo still has no object stored.
        return new response($userinfo);


    }


tbl: classroom_users
 ______________________________
|  fk_user_id   |  fk_class_id|
-------------------------------
|        1      |     1       |
|        2      |     1       |
-------------------------------

任何人都可以告诉我,有什么不对吗?我真的不能确定哪个是特别的,因为它没有向我返回任何错误。

1 个答案:

答案 0 :(得分:0)

这听起来很傻但是yaml映射格式不正确,这就是无法读取映射的原因。

将classroom.orm.yml修复为

Classroom.orm.yml
manyToOne:
classCreatedBy:
  targetEntity: acme\UserBundle\Entity\User
  joinColumn:
    name: fk_created_by
    nullable: false
    referencedColumnName: user_id
oneToMany:
classAnnouncement:
  targetEntity: acme\ClassroomBundle\Entity\Announcements
  mappedBy: announcementClass

manyToMany: //changed this one
classUsers:
  targetEntity: acme\UserBundle\Entity\User
  mappedBy: userClassrooms

classLesson:
  targetEntity: acme\ClassroomBundle\Entity\Lessons
  inversedBy: lessonClass
  joinTable:
    name: classroom_lessons
    joinColumns:
      fk_class_id:
        referencedColumnName: class_id
    inverseJoinColumns:
      fk_lesson_id:
        referencedColumnName: lesson_id