4

I hope you can help me with this problem because I really cannot see what is wrong here.

I have 2 entities: RokZaPrijavuProjekta AND Predmet.

RokZaPrijavuProjekta:

/**
* @ORM\Table(name="rok_prijava_projekta")
* @ORM\Entity(repositoryClass="JP\AdminBundle\Repository\RokZaPrijavuProjektaRepository")
*/
class RokZaPrijavuProjekta
{
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var integer $id_predmet
 * @ORM\ManyToOne(targetEntity="Predmet")
 * @ORM\Column(name="id_predmet", type="integer")
 */
private $predmet;

/**
 * @var date $od
 * @ORM\Column(name="od", type="date")
 */
private $od;

/**
 * @var date $do
 * @ORM\Column(name="do", type="date")
 */
private $do;

/**
 * @var string $info
 * @ORM\Column(name="info", type="string", length=120)
 */
private $info;
}

Predmet entity code:

/**
 * @ORM\Table(name="predmeti")
 * @ORM\Entity(repositoryClass="JP\AdminBundle\Repository\PredmetRepository")
 */
class Predmet
{
/**
 * @var integer $id
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string $sifra
 * @ORM\Column(name="sifra", type="string", length=64)
 */
private $sifra;

/**
 * @var boolean $vidljiv
 * @ORM\Column(name="vidljiv", type="boolean")
 */
private $vidljiv;
}

Repository method:

$q = $this->createQueryBuilder('r')
->select('rzpp')
->where('rzpp.predmet = :predmet')
->from('JPAdminBundle:RokZaPrijavuProjekta', 'rzpp')
->leftJoin("rzpp.predmet", "p")
->setParameter('predmet', $predmet)
->getQuery();

Both getters and setters for all class members are defined properly.

Now, "RokZaPrijavuProjekta" has a foreign-key reference to "Predmet", so many of these "RokZaPrijavuProjekta" can have the same "Predmet".

I want to create unidirectional ManyToOne relation for this purpose but keep getting exception thrown:

Class JP\AdminBundle\Entity\RokZaPrijavuProjekta has no association named predmet

I went all over Doctrine documentation, but found that this is the preferred way to define unidirectional many-to-one relation.

Do you have any idea what might be a problem here?


UPDATE

  • Added Predmet entity code...
  • Added Repository method

Thanks a lot!

Regards, Jovan

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85

3 Answers3

30

Well, last night I encountered the same problem again. This time I took some time to figure out why it started working so suddenly last time (at the time of my question above).

So, the bottom line and solution to the problem:

In my entity class I had both @Column and @ManyToOne annotation where I used @Column to define name of column in database and @ManyToOne to define relation. The point is that you need to remove @Column annotation and replace it with @JoinColumn thus defining both name and referencedColumnName attributes. Check these snippets out:

This will not work:

(either with or without @JoinColumn)

/**
 * @var integer $file
 * @ORM\Column("name="id_file", type="integer")
 * @ORM\ManyToOne(targetEntity="File")
 */
private $file

But this will:

/**
 * @var integer $file
 * @ORM\ManyToOne(targetEntity="File")
 * @ORM\JoinColumn(name="id_file", referencedColumnName="id")
 */
private $file

I hope this will be helpful to someone...

Cheers!

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
  • 4
    hey jperovic, just wanted to say thanks for putting the solution up. You probably just saved me an hours worth of scratching my head :D – Sacred Socks Nov 06 '12 at 11:22
  • Thank you jperovic, two years later and the solution is still helping people out. Been going round in circles until I found this. – AMP May 22 '13 at 13:42
  • @AMP Glad I could have in any way ;) – Jovan Perovic May 28 '13 at 14:19
  • don't know what to do with same error until i find your explaination, you deserve even more credit Mr Perovic! – Dung Aug 30 '16 at 15:01
2

Additional Info, the format of the comments does matter

I wasted a few hours just to find out that i missed a asterisk sign in the comments

This won't work

/*
 * @ORM\OneToMany(targetEntity="\Custom\Models\ProductText", mappedBy="product", orphanRemoval=true, cascade={"persist","remove"})
 */
protected $texts;

however this works

/**
 * @ORM\OneToMany(targetEntity="\Custom\Models\ProductText", mappedBy="product", orphanRemoval=true, cascade={"persist","remove"})
 */
protected $texts;

notice the missing asterisk (*) at the first line of the first example

Ello
  • 907
  • 1
  • 15
  • 33
  • I know that I shouldn't leave comments just for "thanks". But damn this cost me hours upon hours to find. Thank you very much. – PeterT May 25 '18 at 13:29
2

Can you show Predmet entity code?

Or just try out this code:

// RokZaPrijavuProjekta
/**
 * @ORM\ManyToOne(targetEntity="Predmet", inversedBy="rokzaprojects")
 */
protected $predmet;


//Predmet
/**
 * @ORM\OneToMany(targetEntity="RokZaPrijavuProjekta", mappedBy="predmet")
 */
protected $rokzaprojects;
Inoryy
  • 8,365
  • 2
  • 39
  • 40
  • Sorry, I totally forgot to include "Predmet" entity code before, so I edited my post. By specifying "inversedBy" and "mappedBy" wouldn't that be bidirectional relation? – Jovan Perovic Aug 01 '11 at 15:55
  • Hey Inori, I have tried putting bidirectional relation like you suggested but it still does not work. :( Maybe it has something to do with my repository method code? I have edited my post again to include it as well... Thanks! – Jovan Perovic Aug 01 '11 at 16:45
  • Inori, I can't really say what was the problem but now it's gone. I literally copy/pasted your code and this time it worked. Site-note: while running `app\console doctrine:schema:create --dump-sql` on a separate entity manager, I noticed that foreign key was not being created between these 2 tables. Probably was some typo (but not sure which one).... :-/ Anyhow... thanks a lot! :) – Jovan Perovic Aug 03 '11 at 09:32
  • Haha, glad I could help(?) I guess :D – Inoryy Aug 03 '11 at 17:58