Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Class "..\.." has no association named "..."

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

like image 588
Jovan Perovic Avatar asked Nov 27 '22 04:11

Jovan Perovic


2 Answers

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!

like image 87
Jovan Perovic Avatar answered Dec 05 '22 14:12

Jovan Perovic


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;
like image 44
Inoryy Avatar answered Dec 05 '22 14:12

Inoryy