NorthmaN
NorthmaN

Reputation: 125

symfony doctrine one-two-many index problem

Having a the paradigm : - stations could have 0..N forecast models associated. - each forecast model could have 0..N stations related.

It means stations and forecasts tables get related by the in between table named station_forecast.

Following code fails shooting the error when twig file try to read the forecasts collection from a station object:

An exception has been thrown during the rendering of a template

Notice: Undefined index: station in /vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1280

Station:

/** 
 * @ORM\OneToMany(targetEntity="ForecastBundle\Entity\StationForecast", mappedBy="***station***")   <-- THIS 'STATION' THE ERROR REFERS.
 */
protected $forecasts`;

public function __construct()
{
    $this->forecasts = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * @return Doctrine\Common\Collections\Collection 
 */
function getForecasts() {
    return $this->forecasts;
}

/**
 * @param \ForecastBundle\Entity\StationForecast $station_forecast
 */
public function addForecasts(StationForecast $station_forecast)
{
    $this->forecasts[] = $station_forecast;
}

StationForecast

/**
 * @ORM\Id
 * @ORM\Column(name="station_id", type="integer", nullable=false)  
 * @ORM\ManyToOne(targetEntity="EstacionsBundle\Entity\Station", inversedBy="forecasts")
 */
protected $station;

/**
 * @ORM\Id
 * @ORM\Column(name="forecast_id", type="integer", nullable=false)  
 * @ORM\ManyToOne(targetEntity="ForecastBundle\Entity\Forecast", inversedBy="stations")
 */
protected $forecast;

Forecast

/**
 * @ORM\OneToMany(targetEntity="ForecastBundle\Entity\StationForecast", mappedBy="forecast")
 */
protected $stations;

public function addEstacions(\ForecastBundle\Entity\StationForecast $stations)
{
    $this->stations[] = $stations;
}

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

public function addStationForecast(\ForecastBundle\Entity\StationForecast $stations)
{
    $this->stations[] = $stations;
}

Do you know what could happen? I'm getting crazy...

Upvotes: 2

Views: 210

Answers (1)

Peyman Mohamadpour
Peyman Mohamadpour

Reputation: 17954

You don’t need the StationForcast class at all! Just keep Station & Forcast, and doctrine will still create and manage the joint table (station_forcast) for you.

Station

class Station
{
    /** 
     * @ORM\ManyToMany(targetEntity="Forcast", mappedBy="stations")
     */
    protected $forecasts;

    public function __construct()
    {
        $this->forecasts = new ArrayCollection();
    }

    /**
     * @return Collection 
     */
    function getForecasts() {
        return $this->forecasts;
    }

    /**
     * @param Forecast $forecast
     */
    public function addForecast(Forecast $forecast)
    {
        if (!$this->forcasts->contains($forcast)
        {
            $this->forecasts->add($forecast);
            $forcast->addStation($this);
        }
    }
}

Forcast

class Forcast
{
    /**
     * @ORM\ManyToMany(targetEntity="Station", inversedBy="forecasts")
     */
    protected $stations;

    public function __construct()
    {
        $this->stations = new ArrayCollection();
    }

    /**
     * @return Collection 
     */
    public function getStations()
    {
        return $this->stations;
    }

    /**
     * @param Station $station
     */
    public function addStation(Station $station)
    {
        if (!$this->stations->contains($station)
        {
            $this->stations->add($station);
            $station->addForcast($this);
        }
    }
}

Upvotes: 1

Related Questions