我的实体总是遇到异常。我试着和他们一起玩。我检查了谷歌哪个关系最适合我的实体...但我找不到最好的配置。
我正在创建一个网站,您可以在其中创建一个简单的帖子,并在其中创建一些专辑。
3个实体:专辑/邮政/图片
在相册中我有一个图片数组,我在Poste也有一个图片数组。
用户可以拥有多个相册。 在Album Entity中,我确实让用户保留了一个参考。
/**
* Album
*
* @ORM\Table(name="album")
* @ORM\Entity(repositoryClass="Moodress\Bundle\AlbumBundle\Entity\AlbumRepository")
*/
class Album
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
** @ORM\ManyToOne(targetEntity="Moodress\Bundle\UserBundle\Entity\User")
** @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var \DateTime
*
* @ORM\Column(name="creationDate", type="datetime")
*/
private $creationDate;
/**
* @var \DateTime
*
* @ORM\Column(name="modificationDate", type="datetime")
*/
private $modificationDate;
/**
* @ORM\OneToMany(targetEntity="Moodress\Bundle\AlbumBundle\Entity\Picture", cascade={"persist"}, mappedBy="album")
*/
private $pictures;
/**
* Constructor
*/
public function __construct()
{
$this->creationDate = new \Datetime();
$this->modificationDate = new \Datetime();
}
// Get and set
}
当用户在网站上订阅时,我创建了一个名为Upload的默认相册,我希望将其保留在用户类中。
这就是我试图做的事情:
/**
* User
*
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToOne(targetEntity="Moodress\Bundle\AlbumBundle\Entity\Album", cascade={"persist"})
*/
protected $albumUpload;
// Get and set
}
我的Poste实体带有图片数组:
/**
* Poste
*
* @ORM\Table(name="poste")
* @ORM\Entity(repositoryClass="Moodress\Bundle\PosteBundle\Entity\PosteRepository")
*/
class Poste
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Moodress\Bundle\UserBundle\Entity\User")
* @ORM\JoinColumn(nullable=true)
*/
private $user;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @ORM\OneToMany(targetEntity="Moodress\Bundle\AlbumBundle\Entity\Picture", cascade={"persist"}, mappedBy="poste")
*/
private $pictures;
// Getters and setters
}
我的图片实体:
/**
* Picture
*
* @ORM\Table(name="picture")
* @ORM\Entity(repositoryClass="Moodress\Bundle\AlbumBundle\Entity\PictureRepository")
*/
class Picture
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
** @ORM\ManyToOne(targetEntity="Moodress\Bundle\UserBundle\Entity\User")
** @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255)
*/
private $path;
// Getters and setters
}
我有这个错误:Undefined index: album in /Users/Sandro/sites/moodress-website/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1608.
当我序列化任何具有用户对象的实体时,会立即出现此错误...
在日志中:
at ErrorHandler->handle('8', 'Undefined index: album', '/Users/Sandro/sites/moodress-website/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php',
'1608', array('assoc' => array('fieldName' => 'pictures', 'mappedBy' => 'album',
'targetEntity' => 'Moodress\Bundle\AlbumBundle\Entity\Picture',
'cascade' => array('persist'), 'orphanRemoval' => false, 'fetch' => '2',
'type' => '4', 'inversedBy' => null, 'isOwningSide' => false, 'sourceEntity' => 'Moodress\Bundle\AlbumBundle\Entity\Album', 'isCascadeRemove' => false,
'isCascadePersist' => true, 'isCascadeRefresh' => false, 'isCascadeMerge' => false,
'isCascadeDetach' => false), 'sourceEntity' => object(Album), 'offset' => null,
'limit' => null, 'criteria' => array())) in /Users/Sandro/sites/moodress-website/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1608
我不知道我的实体有什么问题。有帮助吗?
答案 0 :(得分:1)
错误:
'Undefined index: album'
来自Album
类中的此注释。
/**
* @ORM\OneToMany(
* targetEntity="Moodress\Bundle\AlbumBundle\Entity\Picture",
* cascade={"persist"},
* mappedBy="album"
* )
*/
您已将mappedBy设置为album
,但$album
类中没有Picture
属性。
将其添加到Picture
课程并清除缓存。
/**
* @ORM\ManyToOne(
* targetEntity="Moodress\Bundle\AlbumBundle\Entity\Album",
* inversedBy="pictures"
* )
*/
protected $album;