简体   繁体   中英

Symfony2 Array Collection returns nothing

Everytime I try to get all the users inside a specific classroom using its id, it returns me nothing. I have a jointable with user_id and 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       |
-------------------------------

Can anybody tell me, whats wrong? I really cant pinpoint which is which specially now since it doesn't return any error to me.

This is gonna sound silly but the yaml mapping format is incorrect thats why the mapping cant be read.

Fix the classroom.orm.yml to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM