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\RokZaPrijavuProj开发者_开发问答ekta 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
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!
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;
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
精彩评论