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:
/**
* @ORMOneToMany(targetEntity="ForecastBundleEntityStationForecast", mappedBy="***station***") <-- THIS 'STATION' THE ERROR REFERS.
*/
protected $forecasts`;
public function __construct()
{
$this->forecasts = new DoctrineCommonCollectionsArrayCollection();
}
/**
* @return DoctrineCommonCollectionsCollection
*/
function getForecasts() {
return $this->forecasts;
}
/**
* @param ForecastBundleEntityStationForecast $station_forecast
*/
public function addForecasts(StationForecast $station_forecast)
{
$this->forecasts = $station_forecast;
}
StationForecast
/**
* @ORMId
* @ORMColumn(name="station_id", type="integer", nullable=false)
* @ORMManyToOne(targetEntity="EstacionsBundleEntityStation", inversedBy="forecasts")
*/
protected $station;
/**
* @ORMId
* @ORMColumn(name="forecast_id", type="integer", nullable=false)
* @ORMManyToOne(targetEntity="ForecastBundleEntityForecast", inversedBy="stations")
*/
protected $forecast;
Forecast
/**
* @ORMOneToMany(targetEntity="ForecastBundleEntityStationForecast", mappedBy="forecast")
*/
protected $stations;
public function addEstacions(ForecastBundleEntityStationForecast $stations)
{
$this->stations = $stations;
}
/**
* @return DoctrineCommonCollectionsCollection
*/
public function getStations()
{
return $this->stations;
}
public function addStationForecast(ForecastBundleEntityStationForecast $stations)
{
$this->stations = $stations;
}
Do you know what could happen? I'm getting crazy...
symfony doctrine2
add a comment |
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:
/**
* @ORMOneToMany(targetEntity="ForecastBundleEntityStationForecast", mappedBy="***station***") <-- THIS 'STATION' THE ERROR REFERS.
*/
protected $forecasts`;
public function __construct()
{
$this->forecasts = new DoctrineCommonCollectionsArrayCollection();
}
/**
* @return DoctrineCommonCollectionsCollection
*/
function getForecasts() {
return $this->forecasts;
}
/**
* @param ForecastBundleEntityStationForecast $station_forecast
*/
public function addForecasts(StationForecast $station_forecast)
{
$this->forecasts = $station_forecast;
}
StationForecast
/**
* @ORMId
* @ORMColumn(name="station_id", type="integer", nullable=false)
* @ORMManyToOne(targetEntity="EstacionsBundleEntityStation", inversedBy="forecasts")
*/
protected $station;
/**
* @ORMId
* @ORMColumn(name="forecast_id", type="integer", nullable=false)
* @ORMManyToOne(targetEntity="ForecastBundleEntityForecast", inversedBy="stations")
*/
protected $forecast;
Forecast
/**
* @ORMOneToMany(targetEntity="ForecastBundleEntityStationForecast", mappedBy="forecast")
*/
protected $stations;
public function addEstacions(ForecastBundleEntityStationForecast $stations)
{
$this->stations = $stations;
}
/**
* @return DoctrineCommonCollectionsCollection
*/
public function getStations()
{
return $this->stations;
}
public function addStationForecast(ForecastBundleEntityStationForecast $stations)
{
$this->stations = $stations;
}
Do you know what could happen? I'm getting crazy...
symfony doctrine2
1
Format your code in question to follow up
– Sunil Singh
Nov 12 at 19:27
add a comment |
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:
/**
* @ORMOneToMany(targetEntity="ForecastBundleEntityStationForecast", mappedBy="***station***") <-- THIS 'STATION' THE ERROR REFERS.
*/
protected $forecasts`;
public function __construct()
{
$this->forecasts = new DoctrineCommonCollectionsArrayCollection();
}
/**
* @return DoctrineCommonCollectionsCollection
*/
function getForecasts() {
return $this->forecasts;
}
/**
* @param ForecastBundleEntityStationForecast $station_forecast
*/
public function addForecasts(StationForecast $station_forecast)
{
$this->forecasts = $station_forecast;
}
StationForecast
/**
* @ORMId
* @ORMColumn(name="station_id", type="integer", nullable=false)
* @ORMManyToOne(targetEntity="EstacionsBundleEntityStation", inversedBy="forecasts")
*/
protected $station;
/**
* @ORMId
* @ORMColumn(name="forecast_id", type="integer", nullable=false)
* @ORMManyToOne(targetEntity="ForecastBundleEntityForecast", inversedBy="stations")
*/
protected $forecast;
Forecast
/**
* @ORMOneToMany(targetEntity="ForecastBundleEntityStationForecast", mappedBy="forecast")
*/
protected $stations;
public function addEstacions(ForecastBundleEntityStationForecast $stations)
{
$this->stations = $stations;
}
/**
* @return DoctrineCommonCollectionsCollection
*/
public function getStations()
{
return $this->stations;
}
public function addStationForecast(ForecastBundleEntityStationForecast $stations)
{
$this->stations = $stations;
}
Do you know what could happen? I'm getting crazy...
symfony doctrine2
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:
/**
* @ORMOneToMany(targetEntity="ForecastBundleEntityStationForecast", mappedBy="***station***") <-- THIS 'STATION' THE ERROR REFERS.
*/
protected $forecasts`;
public function __construct()
{
$this->forecasts = new DoctrineCommonCollectionsArrayCollection();
}
/**
* @return DoctrineCommonCollectionsCollection
*/
function getForecasts() {
return $this->forecasts;
}
/**
* @param ForecastBundleEntityStationForecast $station_forecast
*/
public function addForecasts(StationForecast $station_forecast)
{
$this->forecasts = $station_forecast;
}
StationForecast
/**
* @ORMId
* @ORMColumn(name="station_id", type="integer", nullable=false)
* @ORMManyToOne(targetEntity="EstacionsBundleEntityStation", inversedBy="forecasts")
*/
protected $station;
/**
* @ORMId
* @ORMColumn(name="forecast_id", type="integer", nullable=false)
* @ORMManyToOne(targetEntity="ForecastBundleEntityForecast", inversedBy="stations")
*/
protected $forecast;
Forecast
/**
* @ORMOneToMany(targetEntity="ForecastBundleEntityStationForecast", mappedBy="forecast")
*/
protected $stations;
public function addEstacions(ForecastBundleEntityStationForecast $stations)
{
$this->stations = $stations;
}
/**
* @return DoctrineCommonCollectionsCollection
*/
public function getStations()
{
return $this->stations;
}
public function addStationForecast(ForecastBundleEntityStationForecast $stations)
{
$this->stations = $stations;
}
Do you know what could happen? I'm getting crazy...
symfony doctrine2
symfony doctrine2
edited Nov 14 at 3:41
malarzm
2,04021016
2,04021016
asked Nov 12 at 16:54
Norman Morell Asensio
235
235
1
Format your code in question to follow up
– Sunil Singh
Nov 12 at 19:27
add a comment |
1
Format your code in question to follow up
– Sunil Singh
Nov 12 at 19:27
1
1
Format your code in question to follow up
– Sunil Singh
Nov 12 at 19:27
Format your code in question to follow up
– Sunil Singh
Nov 12 at 19:27
add a comment |
1 Answer
1
active
oldest
votes
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
{
/**
* @ORMManyToMany(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
{
/**
* @ORMManyToMany(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);
}
}
}
Nice! But how does Doctrine knows the name of the in the middle table? And its foreign keys fields names? Doing what you suggested, symfony returns '' ("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forecast_station' doesn't exist" '' because the right one is 'station_forecast'. Do I have to use any annotation to indicate it all (fk fields and table names)?
– Norman Morell Asensio
Nov 13 at 8:56
1
Doctrine combines the name of the two entities and creates the middle table. Gets the foreign key names from the association (ManyToMany) combined with_id
.
– Trix
Nov 13 at 9:00
Thank you very much for your help!! @Trix
– Norman Morell Asensio
Nov 13 at 11:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266741%2fsymfony-doctrine-one-two-many-index-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
{
/**
* @ORMManyToMany(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
{
/**
* @ORMManyToMany(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);
}
}
}
Nice! But how does Doctrine knows the name of the in the middle table? And its foreign keys fields names? Doing what you suggested, symfony returns '' ("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forecast_station' doesn't exist" '' because the right one is 'station_forecast'. Do I have to use any annotation to indicate it all (fk fields and table names)?
– Norman Morell Asensio
Nov 13 at 8:56
1
Doctrine combines the name of the two entities and creates the middle table. Gets the foreign key names from the association (ManyToMany) combined with_id
.
– Trix
Nov 13 at 9:00
Thank you very much for your help!! @Trix
– Norman Morell Asensio
Nov 13 at 11:25
add a comment |
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
{
/**
* @ORMManyToMany(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
{
/**
* @ORMManyToMany(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);
}
}
}
Nice! But how does Doctrine knows the name of the in the middle table? And its foreign keys fields names? Doing what you suggested, symfony returns '' ("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forecast_station' doesn't exist" '' because the right one is 'station_forecast'. Do I have to use any annotation to indicate it all (fk fields and table names)?
– Norman Morell Asensio
Nov 13 at 8:56
1
Doctrine combines the name of the two entities and creates the middle table. Gets the foreign key names from the association (ManyToMany) combined with_id
.
– Trix
Nov 13 at 9:00
Thank you very much for your help!! @Trix
– Norman Morell Asensio
Nov 13 at 11:25
add a comment |
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
{
/**
* @ORMManyToMany(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
{
/**
* @ORMManyToMany(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);
}
}
}
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
{
/**
* @ORMManyToMany(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
{
/**
* @ORMManyToMany(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);
}
}
}
edited Nov 12 at 20:02
answered Nov 12 at 19:31
Trix
9,70785073
9,70785073
Nice! But how does Doctrine knows the name of the in the middle table? And its foreign keys fields names? Doing what you suggested, symfony returns '' ("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forecast_station' doesn't exist" '' because the right one is 'station_forecast'. Do I have to use any annotation to indicate it all (fk fields and table names)?
– Norman Morell Asensio
Nov 13 at 8:56
1
Doctrine combines the name of the two entities and creates the middle table. Gets the foreign key names from the association (ManyToMany) combined with_id
.
– Trix
Nov 13 at 9:00
Thank you very much for your help!! @Trix
– Norman Morell Asensio
Nov 13 at 11:25
add a comment |
Nice! But how does Doctrine knows the name of the in the middle table? And its foreign keys fields names? Doing what you suggested, symfony returns '' ("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forecast_station' doesn't exist" '' because the right one is 'station_forecast'. Do I have to use any annotation to indicate it all (fk fields and table names)?
– Norman Morell Asensio
Nov 13 at 8:56
1
Doctrine combines the name of the two entities and creates the middle table. Gets the foreign key names from the association (ManyToMany) combined with_id
.
– Trix
Nov 13 at 9:00
Thank you very much for your help!! @Trix
– Norman Morell Asensio
Nov 13 at 11:25
Nice! But how does Doctrine knows the name of the in the middle table? And its foreign keys fields names? Doing what you suggested, symfony returns '' ("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forecast_station' doesn't exist" '' because the right one is 'station_forecast'. Do I have to use any annotation to indicate it all (fk fields and table names)?
– Norman Morell Asensio
Nov 13 at 8:56
Nice! But how does Doctrine knows the name of the in the middle table? And its foreign keys fields names? Doing what you suggested, symfony returns '' ("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forecast_station' doesn't exist" '' because the right one is 'station_forecast'. Do I have to use any annotation to indicate it all (fk fields and table names)?
– Norman Morell Asensio
Nov 13 at 8:56
1
1
Doctrine combines the name of the two entities and creates the middle table. Gets the foreign key names from the association (ManyToMany) combined with
_id
.– Trix
Nov 13 at 9:00
Doctrine combines the name of the two entities and creates the middle table. Gets the foreign key names from the association (ManyToMany) combined with
_id
.– Trix
Nov 13 at 9:00
Thank you very much for your help!! @Trix
– Norman Morell Asensio
Nov 13 at 11:25
Thank you very much for your help!! @Trix
– Norman Morell Asensio
Nov 13 at 11:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266741%2fsymfony-doctrine-one-two-many-index-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Format your code in question to follow up
– Sunil Singh
Nov 12 at 19:27