0

I have the following entities structure for my tables: https://gist.github.com/melokki/e2e0d7c03ee71c37c1185602562da6af

$movie = $repository->findOneBy([
    'title' => 'movie title',
]);

I am trying to access a movie sources like this:

$movie->getSources()

and based on the documentation I can do it, but right now I am getting the following notice

Notice: Undefined index: movie

and I cannot understand why

Is it something wrong with my code?

melokki
  • 104
  • 11
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Nawin May 18 '18 at 13:14
  • The error message implies that somewhere you are trying to access an array with an index of 'movie'. Probably not related to getSources(). It could be that you forgot to add $movie to your twig data? Just a wild guess. Search for movie in your code and see what pops up. – Cerad May 18 '18 at 13:43
  • I am not trying to access the `getSources() ` method from a view. I am in a cli command. Based on the docs from Doctrine, my entities are correct, but somehow something is still wrong and I cannot see what exactly. I understand that the index `movie` is missing, but I I cannot understand how to set it. the notice comes from `BasicEntityPersister` – melokki May 18 '18 at 13:47
  • Nothing really jumps out on your gist mappings. Make sure you don't have any old yaml/xml mapping files lurking about under Resources. The gist does not show Movie::getSources so I can't tell if that is the problem. Nor can I tell if Movie::movieSource is an array. The spelling seems to indicate it is not. – Cerad May 18 '18 at 14:07

2 Answers2

2

I've run the doctrine:schema:validate command and now I think I got it.

My problem is that in MovieSchema for $movie property I have two annotations for the field: @ORM\Column(type="string", name="movie_id") and @ORM\JoinColumn(name="movie_id", referencedColumnName="id", nullable=false).

I've removed the first one and now I get the desired result when I call the getSources method.

Thank you very much all for your time.

melokki
  • 104
  • 11
0

Based on your github link for your entities:

First

You can't do $movie->getSources() since there is no field called $sources in your Movie class

Second

In your movie-entity there is a field called $movieSource which is an one-to-many relation. That should be renamed to $movieSources cause you'll get back a ArrayCollection of MovieSource-objects

I suspect, you want to model a ManyToMany relation with Movie <-> Source?

If yes, you can directly link these entities to each other without being worry about an intermediate table, doctrine will do that for you.

See here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#many-to-many-bidirectional

I would strongly recommend to discard the intermediate table if there is no real usage for it.

But if you want to keep this model you'll have to call (after changing the methods name)

$movie->getMovieSources() 

and iterate over this collection.

for ($movie->getMovieSources() as $movieSource) {
  $moviesource->getSource();
}

It's most likely, that your original asked error is gone with these changes.

Jim Panse
  • 2,220
  • 12
  • 34
  • You are correct, I want to model a many to many relation between Movie and Source, but in the same time I want to store some extra information on the pivot table (a description for movies) it might be a bad thing to do, but as of right now I don't have other ideas how to store the movie description since it's different for every source. My `getSources` method actually returns the `movieSource` property of the object that is why I am trying to call this method. I'll try to update the gist with the complete code. Also `movieSource` property is initialized as `ArrayCollection` on the constructor – melokki May 18 '18 at 15:40
  • 1
    No, its not a bad thing. If there is a need (like in your case) you are fine. But for now you cant go with $movie->getSources() until you implement such a method (in my eyes no proper design). Is the error gone now? What about the AbstractEntity class? – Jim Panse May 22 '18 at 07:08
  • I've run the `doctrine:schema:validate` command and now I think I got it. My problem is that in `MovieSchema` for `$movie` property I have two annotations for the field `@ORM\Column(type="string", name="movie_id")` and `@ORM\JoinColumn(name="movie_id", referencedColumnName="id", nullable=false)`. I've removed the first one and now I get the desired result when I call the `getSources` method. Thank you very much all for your time. – melokki May 22 '18 at 07:53