gazzwi86
gazzwi86

Reputation: 1030

Doctrine 2: Joining two tables ManyToOne

I'm having some issues when trying to join two tables with Doctrine 2. When I attempt to add a new item to the table I get the error:

A new entity was found through a relationship that was not configured to cascade persist operations: @. Explicitly persist the new entity or configure cascading persist operations on the relationship.

When I try to update a row in the table I'm getting the error:

The given entity has no identity.

I'm trying to join the winner entities event id to the id of an event so i can gather data on the event for the view. I'm defiantly getting an id returned for from the form for the update but it doesn't seem to let me update it.

Update: So having played around with things a little, when I add the cascade="{persist}" option I get a class not found error, removing it and adding the full namespace results in it claiming the entity does not exist...

<?php

namespace ZC\Entity;

/**
 * ZC\Entity\Winner
 *
 * @Table(name="winner")
 * @Entity(repositoryClass="ZC\Entity\Repository\Winner")
 */

class Winner
{

    /**
     * @var integer $id
     *
     * @Column(name="id", type="integer", nullable=false, unique=false, precision=0, scale=0)
     * @Id
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string $copy
     *
     * @Column(name="copy", type="text", length="", unique=false, nullable=true, precision=0, scale=0)
     */
    protected $copy;

    /**
     * @var string $url
     *
     * @Column(name="url", type="string", length="255", unique=false, nullable=true, precision=0, scale=0)
     */
    protected $url;

    /**
     *
     * @ManyToOne(targetEntity="Events")
     * @JoinColumns=({
     *  @JoinColumn(name="event", referencedColumnName="id")
     * })
     *
     */
    protected $event;


    /**
     * __get function.
     * 
     * @access protected
     * @param mixed $property
     * @return void
     */
    public function __get($property) {
        return $this->$property;
    }

    /**
     * __set function.
     * 
     * @access protected
     * @param mixed $property
     * @param mixed $value
     * @return void
     */
    public function __set($property, $value) {
        $this->$property = $value;
    } 

}



<?php

namespace ZC\Entity;

/**
 * ZC\Entity\Events
 *
 * @Table(name="events")
 * @Entity(repositoryClass="ZC\Entity\Repository\Events")
 */

class Events
{

    /**
     * @var integer $id
     *
     * @Column(name="id", type="integer", nullable=false, unique=false, precision=0, scale=0)
     * @Id
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string $title
     *
     * @Column(name="title", type="string", length="255", unique=false, nullable=false, precision=0, scale=0)
     */
    protected $title;

    /**
     * @var string $description
     *
     * @Column(name="description", type="text", length="", unique=false, nullable=true, precision=0, scale=0)
     */
    protected $description;

    /**
     * @var string $status
     *
     * @Column(name="status", type="smallint", length="1", unique=false, nullable=false, precision=0, scale=0)
     */
    protected $status = 0;

    /**
     * @var string $date
     *
     * @Column(name="date", type="string", length="255", unique=false, nullable=false, precision=0, scale=0)
     */
    protected $date;

    /**
     * @var string $lang
     *
     * @Column(name="lang", type="string", length="255", unique=false, nullable=false, precision=0, scale=0)
     */
    protected $lang;


    /**
     * __get function.
     * 
     * @access protected
     * @param mixed $property
     * @return void
     */
    public function __get($property) {
        return $this->$property;
    }

    /**
     * __set function.
     * 
     * @access protected
     * @param mixed $property
     * @param mixed $value
     * @return void
     */
    public function __set($property, $value) {
        $this->$property = $value;
    } 

}

Upvotes: 1

Views: 222

Answers (1)

Thomas K
Thomas K

Reputation: 6216

Use cascade={"persist"}:

/**
 *
 * @ManyToOne(targetEntity="Acme\Bundle\AcmeBundle\Entity\Events", cascade={"persist"})
 * @JoinColumns=({
 *  @JoinColumn(name="event", referencedColumnName="id")
 * })
 *
 */
protected $event;

Upvotes: 1

Related Questions