简体   繁体   中英

Doctrine error Symfony 3

I've got fos_user entity that is in relation OneToOne with reservedArea entity.

class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="ReservedAreaBundle\Entity\ReservedArea", cascade={"persist"})
     * @ORM\JoinColumn(name="reserved", referencedColumnName="id", onDelete="SET NULL")
     */
    protected $reserved;

    public function __construct()
    {
        parent::__construct();
    }

    public function getReserved()
    {
        return $this->reserved;
    }

    public function setReserved($reserved)
    {
        $this->reserved = $reserved;
    }
}

In my reservedArea entity i've got only a OneToMany relation with File entity.

class ReservedArea
{
    /**
    * @ORM\Column(type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="ReservedAreaBundle\Entity\File", mappedBy="reserved_area", cascade={"persist"})
     */
    protected $file;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->file = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add file
     *
     * @param \ReservedAreaBundle\Entity\File $file
     *
     * @return ReservedArea
     */
    public function addFile(\ReservedAreaBundle\Entity\File $file)
    {
        $this->file[] = $file;

        return $this;
    }

    /**
     * Remove file
     *
     * @param \ReservedAreaBundle\Entity\File $file
     */
    public function removeFile(\ReservedAreaBundle\Entity\File $file)
    {
        $this->file->removeElement($file);
    }

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

And in my File entity i've got just a simple string.

class File
{
    /**
    * @ORM\Column(type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;

    /**
    * @ORM\Column(type="string")
    */
    protected $file;

    /**
     * @ORM\ManyToOne(targetEntity="ReservedAreaBundle\Entity\ReservedArea", inversedBy="file")
     * @ORM\JoinColumn(name="reserved_area", referencedColumnName="id")
     */
    protected $reservedArea;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set reservedArea
     *
     * @param \ReservedAreaBundle\Entity\ReservedArea $reservedArea
     *
     * @return File
     */
    public function setReservedArea(\ReservedAreaBundle\Entity\ReservedArea $reservedArea = null)
    {
        $this->reservedArea = $reservedArea;

        return $this;
    }

    /**
     * Get reservedArea
     *
     * @return \ReservedAreaBundle\Entity\ReservedArea
     */
    public function getReservedArea()
    {
        return $this->reservedArea;
    }

    /**
     * Set file
     *
     * @param string $file
     *
     * @return File
     */
    public function setFile($file)
    {
        $this->file = $file;

        return $this;
    }

    /**
     * Get file
     *
     * @return string
     */
    public function getFile()
    {
        return $this->file;
    }

Now if i try to access to user file doing something like that :

$files = $this->getUser()->getReserved()->getFile();
foreach($files as $file) {
    var_dump($file); //or $file->getFile();
}

I get an error :

Notice: Undefined index: reserved_area

In my database I add an user with a reserved area and two file and if i direct make :

SELECT u.username, f.file FROM fos_user u INNER JOIN reserved_area r ON u.reserved=r.id INNER JOIN reserved_area_file f ON f.reserved_area=r.id;

I get in output the right result. Where i'm going wrong?
Thanks in advice.

EDIT: I answered the original question: https://stackoverflow.com/revisions/35969467/1 which was then edited by the author.


The annotation mapping of $reservedArea in File class is incorrect. The mappedBy and inversedBy attributes must always contain name of the attribute from entity on the other side of the association. So in your case File::$reservedArea should have inversedBy="file" .

See http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional for more examples.

I found the solution to the problem and I post it beacause maybe will help someone.
The problem is here:

/**
* @ORM\OneToMany(targetEntity="ReservedAreaBundle\Entity\File", mappedBy="reserved_area", cascade={"persist"})
*/
protected $file;

I can't use in mappedBy the name of the table in @JoinColumn(name="reserved_are") but I have to use the name of the attribute.
So :

mappedBy="reservedArea"

solved the 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