Doctrine2 bidirectional manyToMany - 未定义的索引

时间:2014-06-24 21:28:32

标签: php doctrine-orm

向用户添加新喜欢的菜(这可行):

$user = $em->getRepository('user')->find($userId);
$dish = $em->getRepository('dish')->find($dishId);
$user->addFavouriteDish($dish);
$em->persist($user);
$em->flush();

让所有喜欢这道菜的用户(这不起作用)

$dish = $em->getRepository('dish')->find($dishId);
echo 'There are '.count($dish->getFavouriteUsers()).' users that has this dish as a favourite';

上述错误:

string(23) "Undefined index: dish"
string(88) "/srv/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php"

加入表:

dinner.user_dish
id, user_id, dish_id

学说生成的实体:

/entities/user.php

use Doctrine\ORM\Mapping as ORM;

/**
 * user
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class user
{

  ...

  /**
   * @var \Doctrine\Common\Collections\Collection
   *
   * @ORM\ManyToMany(targetEntity="dish", inversedBy="user")
   * @ORM\JoinTable(name="user_dish",
   *   joinColumns={
   *     @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
   *   },
   *   inverseJoinColumns={
   *     @ORM\JoinColumn(name="dish_id", referencedColumnName="id", onDelete="CASCADE")
   *   }
   * )
   */
  private $favouriteDishes;

  /**
   * Constructor
   */
  public function __construct()
  {
      $this->session = new \Doctrine\Common\Collections\ArrayCollection();
      $this->favouriteDishes = new \Doctrine\Common\Collections\ArrayCollection();
  }

  ...

  /**
   * Add favouriteDishes
   *
   * @param \dish $favouriteDishes
   * @return user
   */
  public function addFavouriteDish(\dish $favouriteDishes)
  {
      $this->favouriteDishes[] = $favouriteDishes;

      return $this;
  }


/entities/dish.php

use Doctrine\ORM\Mapping as ORM;

/**
 * dish
 *
 * @ORM\Table(name="dish")
 * @ORM\Entity
 */
class dish
{

  ...

  /**
   * @var \Doctrine\Common\Collections\Collection
   *
   * @ORM\ManyToMany(targetEntity="user", mappedBy="dish")
   */
  private $favouriteUsers;

  /**
   * Constructor
   */
  public function __construct()
  {
      $this->favouriteUsers = new \Doctrine\Common\Collections\ArrayCollection();
  }

  ...

  /**
   * Get favouriteUsers
   *
   * @return \Doctrine\Common\Collections\Collection 
   */
  public function getFavouriteUsers()
  {
      return $this->favouriteUsers;
  }

yaml文件:

/config/yaml/user.dcm.yml

user:
  type: entity
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    displayname:
      type: string
      length: 60
      unique: true
    email:
      type: string
      length: 255
      unique: true
    hash:
      type: string
      length: 60
  manyToMany:
    favouriteDishes:
      targetEntity: dish
      inversedBy: user
      joinTable:
        name: user_dish
        joinColumns:
          user_id:
            referencedColumnName: id
        inverseJoinColumns:
          dish_id:
            referencedColumnName: id
  oneToMany:
    session:
      targetEntity: session
      mappedBy: user
  lifecycleCallbacks:
    prePersist: [ hashPassword ]


/config/yaml/dish.dcm.yml

dish:
  type: entity
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    title:
      type: string
      length: 255
    description:
      type: text
    image:
      type: string
      length: 255
  manyToMany:
    favouriteUsers:
      targetEntity: user
      mappedBy: dish

1 个答案:

答案 0 :(得分:4)

你的菜实体中的manyToMany表示它是mappedBy: dish。但是,它实际上是由favouriteDishes映射的。只需将其更改为favouriteDishes

dish:
  type: entity
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    title:
      type: string
      length: 255
    description:
      type: text
    image:
      type: string
      length: 255
  manyToMany:
    favouriteUsers:
      targetEntity: user
      mappedBy: favouriteDishes