Reputation: 759
I'm trying Doctrine Associations with Symfony2 for the first time and it's giving me headache.
I have an Admin interface that can, among other things, upload images. I want to know which administrator uploaded what image so i've putted a foreign key administrator
to my images
table. To gather data, a simple JOIN is necessary to collect the data but with Doctrine, I'm stuck, altough it seems simple.
So, I have an Administrator
object that reflects the table. In that object, I have this statement...
@ORM\OneToMany(targetEntity="ImageBundleNamespace\ImageEntity", mappedBy="administrator")
It's simple. In my ImageEntity
object (that reflects Images
table) is a foreigh key column administrator
.
In the ImageEntity object, I use this statement...
@ORM\ManyToOne(targetEntity="AdministratorNamespace\Administrator", inversedBy="imageEntity")
@ORM\JoinColumn(name="administrator", referencedColumnName="id")
There is a administrator
field in ImageEntity
and a imageEntity
field in Administrator
that the formentioned statements are mapping.
It doesn't work.
I've run the SchemaValidator
on the EntityManger and it says the the administrator
field on the ImageEntity
object is not defined as an association. The second message says that the administrator
field does not exist.
If it helps, this is my DQL for all of it...
'SELECT i.id,
i.imeSlike,
i.velicina,
i.ekstenzija,
i.paths,
a.username,
a.ime,
a.prezime FROM ImageBundle:ImageEntity i
JOIN a.administrator a'
Thank you in advance for all the help.
EDIT
I had a mistake in DQL. Corrected it.
EDIT
I forgot to add the source code. Association part of the Administrator...
**
* @ORM\OneToMany(targetEntity="Icoo\Administracija\GalerijaBundle\Entity\ImageEntity", mappedBy="administrator")
*/
protected $imageEntity;
public function __construct() {
$this->imageEntity = new ArrayCollection();
}
Association part of the ImageEntity
/**
* @ORM\Column(type="smallint")
*
* @ORM\ManyToOne(targetEntity="Icoo\LoginBundle\Entity\Administrator", inversedBy="imageEntity")
* @ORM\JoinColumn(name="administrator", referencedColumnName="id")
*
*/
protected $administrator;
Upvotes: 1
Views: 240
Reputation: 693
You can separate field mapping from association mapping:
/**
* @ORM\Column(name="administrator", type="smallint", nullable=false, options={"unsigned"=true})
*/
protected $administratorId;
/**
* @ORM\ManyToOne(targetEntity="Icoo\LoginBundle\Entity\Administrator", inversedBy="imageEntity")
* @ORM\JoinColumn(name="administrator", referencedColumnName="id")
**/
protected $administrator;
If you go further you can put exception into $administratorId getter/setter to avoid usage of it.
As i have tested, doctrine ignores value into $administratorId property when you persist/flush your entity (For confirmation you can look at prepareUpdateData() in Doctrine\ORM\Persisters\BasicEntityPersister)
EDIT
I think, my previous variant is possible but wrong. Because, doctrine gets field mapping from definitions in referencedColumn, you can add some more definitions using
* @ORM\JoinColumn(name="administrator", referencedColumnName="id", unique, nullable, onDelete, columnDefinition, fieldName)
This means that your image_entity.administrator
field in db will be the same as your administrator.id
field in db (except of additional definitions)
Upvotes: 1
Reputation: 162
In the administrator class, you have:
/**
* @ORM\Column(type="smallint")
*
* @ORM\ManyToOne(targetEntity="Icoo\LoginBundle\Entity\Administrator", inversedBy="imageEntity")
* @ORM\JoinColumn(name="administrator", referencedColumnName="id")
*
*/
you need to remove the @ORM\Column(type="smallint")
This must be all. Let me know.
Upvotes: 1