简体   繁体   中英

Doctrine/Symfony - I have some errors. Something wrong with my relations

I always get an exception with my entities. I tried to play with them. I checked on google which relation is the most appropriate for my entities... But I couldn't find the best configuration.

I'm making a website where you can create some albums.

A user can have multiple albums.So I have an entity Album and I have inside a user property :

/**
* 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
}

However, When a user subscribe on the website, I create a default album called Upload that I want to keep in the user class.

This is what I tried to do :

/**
* 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
}

I have this error : Undefined index: album in /Users/Sandro/sites/moodress-website/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1608. This error appears directly when I serialize any entity that has a user object...

The error is caused by the association annotation of the $albumUpload property in the User class.

Doctrine assumes that you have a $album property in your Album entity when you use a @ORM\\OneToOne(targetEntity="Moodress\\Bundle\\AlbumBundle\\Entity\\Album") association in your User entity.

Try

/** 
 ** @ORM\ManyToOne(targetEntity="Moodress\Bundle\UserBundle\Entity\User", inversedBy="albums")
 ** @ORM\JoinColumn(nullable=false) 
 */
private $user;

in your Album entity and

/**
 * @ORM\OneToMany(targetEntity="Moodress\Bundle\AlbumBundle\Entity\Album", mappedBy="user", cascade={"persist"})
 */
 protected $albums;

in your User entity. In addition please add $this->albums = new ArrayCollection() to User::__construct().

Now if you want to add and keep a default album for users you should implement this somewhere in your business logic. You could figure out a way to identify the default album in your collection of albums and prevent deletion especially for this item.

Hope this helps.

My error:

'Undefined index: album'

results from this annotation in the Album class.

/**
 * @ORM\OneToMany(
 *      targetEntity="Moodress\Bundle\AlbumBundle\Entity\Picture",
 *      cascade={"persist"}, 
 *      mappedBy="album"
 *  )
 */

I have set mappedBy to album but there is no $album property in my Picture class.

I did add this to my Picture class and clear my cache.

/**
 * @ORM\ManyToOne(
 *      targetEntity="Moodress\Bundle\AlbumBundle\Entity\Album",
 *      inversedBy="pictures"
 *  )
 */
protected $album;

It solved my problem

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