symfony原则一二三索引问题

时间:2018-11-12 16:54:24

标签: symfony doctrine-orm

具有范例: -电台可以关联0..N个预测模型。 -每个预测模型可能有0..N个站点相关。

这意味着 stations precasts 表之间通过名为 station_forecast 的表之间的关联。

当树枝文件尝试从站点对象读取预报集合时,以下代码无法捕获错误:

在呈现模板期间引发了异常

  

通知:未定义的索引:/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php第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;

预测

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

您知道会发生什么吗?我快疯了...

1 个答案:

答案 0 :(得分:2)

您完全不需要 StationForcast 类!只需保留 Station Forcast ,该学说仍将为您创建和管理联合表(station_forcast)。

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);
        }
    }
}

预测

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);
        }
    }
}