i have a Gift entity, this entity has a sender, and a receiver...both from the entity User
a user can be a sender of many gifts to other users. but also a receiver of many gifts from many users
the way i see it, the solution inside my Gift entity would be like this:
/**
* #ORM\ManyToMany(targetEntity="Tracker\UserBundle\Entity\User")
* #ORM\JoinTable(name="gift_user",
* joinColumns={#ORM\JoinColumn(name="sender_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="gift_id", referencedColumnName="id")}
* )
*/
protected $senders;
/**
* #ORM\ManyToMany(targetEntity="Tracker\UserBundle\Entity\User")
* #ORM\JoinTable(name="gift_user",
* joinColumns={#ORM\JoinColumn(name="receiver_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="gift_id", referencedColumnName="id")}
* )
*/
protected $receivers;
but when i run php app/console doctrine:schema:update --dump-sql i get:
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'finaldb.gift_user' already exists.
how do i have to change my syntax, configuration, so i end up with a table like this?
gift_id | sender_id | receiver_id
What you are trying to achieve is not possible without two join tables.
Each #ManyToMany association requires a different join table. That because the join table does not know anything else than two linked entities (without knowing the direction of the association). Also, join tables generated by Doctrine ORM don't have any auto-incremental identifier, since the two references already represent the table's primary key.
Here is the stuff :
You can use joinTable to specific the join table name
/**
* #ORM\ManyToMany(targetEntity="Tracker\UserBundle\Entity\User")
* #ORM\JoinTable(name="gift_user",joinTable="myprefix_mytable_senders",
* joinColumns={#ORM\JoinColumn(name="sender_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="gift_id", referencedColumnName="id")}
* )
*/
protected $senders;
/**
* #ORM\ManyToMany(targetEntity="Tracker\UserBundle\Entity\User")
* #ORM\JoinTable(name="gift_user",joinTable="myprefix_mytable_receivers"
* joinColumns={#ORM\JoinColumn(name="receiver_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="gift_id", referencedColumnName="id")}
* )
*/
protected $receivers;
Related
I have the following field:
/**
* Many Organization have Many PartnerCategory.
*
* #ORM\ManyToMany(targetEntity="PartnerCategory", inversedBy="organizations")
* #ORM\JoinTable(name="partnercategory_organization_map",
* joinColumns={#ORM\JoinColumn(name="organization_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="partnercategory_id", referencedColumnName="id")}
* )
*/
protected $partnerCategories;
how can i make Unique to false!?
You cannot just disable uniqueness since result table of ManyToMany association have both columns as primary key.
Create your own association class PartnerCategoryOrganization which will contain links between categories and organizations with ManyToOne relation to the both entities. Then replace ManyToMany with OneToMany relation on PartnerCategory and Organization entities.
I want to make a ManyToMany relation in Doctrine with Symfony 2 between a group and an user : many users can be in many groups and many groups can have many users.
Then in my entities, i do this :
Groupe.php
/**
* Many Groups have Many clients.
* #ORM\ManyToMany(targetEntity="Utilisateurs\UtilisateursBundle\Entity\Client", mappedBy="groupe")
* #ORM\JoinTable(name="client_groupe")
*/
private $clients;
/**
* Get clients
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getClients()
{
return $this->clients;
}
Client.php
/**
* #ORM\ManyToMany(targetEntity="Ecommerce\EcommerceBundle\Entity\Groupe", inversedBy="clients")
* #ORM\JoinTable(name="client_groupe",
* joinColumns={#ORM\JoinColumn(name="client_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="groupe_id", referencedColumnName="id")}
* )
*/
private $groupe;
but when I call the getClients() function on my entity $groupe, following error occured :
FROM client t0 WHERE client_groupe.groupe_id = ?' with params ["2"]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'client_groupe.groupe_id' in 'where clause'
It doesn't add the client_groupe table in the from clause.
Someone can help me ?
No need to add client_groupe table in from clause. Try after removing * #ORM\JoinTable(name="client_groupe") from Groupe.php. Have a look at following working example of ManyToMany relationship scenario.
Groupe.php
/**
* Many Groups have Many clients
* #ORM\ManyToMany(targetEntity="Utilisateurs\UtilisateursBundle\Entity\Client", mappedBy="groupe")
*/
private $clients;
Client.php
/**
* #ORM\ManyToMany(targetEntity="Ecommerce\EcommerceBundle\Entity\Groupe", inversedBy="clients")
* #ORM\JoinTable(name="client_groupe",
* joinColumns={#ORM\JoinColumn(name="client_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="groupe_id", referencedColumnName="id")}
* )
*/
private $groupe;
client_id and groupe_id is fields of client_groupe table. Generate Getter and setter using doctrine command and update database using bin/console doctrine:schema:update --force command.
I'm new with Symfony2 and Doctrine 2 ORM, then i got a problem with composite FK association.
While the association OneToMany-ManyToOne by a single auto-increment ID works good, i got many troubles with the same kind of association but through a composite PK.
What i would like to do is an entity-structure that mirrors the below shema.
http://imageshack.us/photo/my-images/9/z07m.jpg (Sorry but I cannot insert images into the post)
where
prd_product_data.product_id
prd_product_data.language_id
make up together the PK of 'prd_product_data'
and
prd_product_image.pdoduct_id
prd_product_image.language_id
make up the FK linked to the table 'prd_product_data'.
These below
prd_product_image.pdoduct_id
prd_product_image.language_id
prd_product_image.file_image_id
make up, all together, the PK of 'prd_product_image'.
I read that Doctrine 2.1 and up, supports natively the composed PK - FK association but there are very few examples. Moreover, surfing on the web, i found just scattered fragments of similar situations but nothing of useful.
The main issue now, is that i'm unable to create one unique object for the two ids, like such an unique id.
For example...
Snapshot for the Entity ProductData where "product and language should make up the same object:
/**
*
* #ORM\OneToMany(targetEntity="ProductImage", mappedBy="product")
*/
protected $products_Pimgs;
/**
*
* #ORM\OneToMany(targetEntity="ProductImage", mappedBy="language")
*/
protected $products_Limgs;
Snapshot for the Entity ProductImage where *product_id* and *language_id* made up the FK for the entity ProductData:
/**
*
* #ORM\Id
* #ORM\ManyToOne(targetEntity="ProductData", inversedBy="products_Pimgs")
* #ORM\JoinColumn(name="product_id", referencedColumnName="product_id")
*/
protected $product;
/**
*
* #ORM\Id
* #ORM\ManyToOne(targetEntity="ProductData", inversedBy="products_Limgs")
* #ORM\JoinColumn(name="language_id", referencedColumnName="language_id")
*/
protected $language;
/**
*
* #ORM\Id
* #ORM\ManyToOne(targetEntity="FileImage", inversedBy="files_images")
* #ORM\JoinColumn(name="file_image_id", referencedColumnName="id")
*/
protected $file_image;
From these snapshots it's evident that those keys are working separately.
In the end, this is my doctrine version, taken from the composer:
"doctrine/orm": ">=2.2.3,<2.4-dev",
How can i create an unique reference with two objects with ORM?
Read the documentation chapter Composite Primary Keys.
/**
* #ORM\Id
* #ORM\OneToMany(targetEntity="ProductImage", mappedBy="product")
*/
protected $products_Pimgs;
/**
* #ORM\Id
* #ORM\OneToMany(targetEntity="ProductImage", mappedBy="language")
*/
protected $products_Limgs;
I have an entity 'listing' with OneToMany to entity 'view', the key between these to is view.content_id which holds the ID of listing, however, it also relates to other entities, so by adding
/**
* #var Listing
*
* #ORM\ManyToOne(targetEntity="\Acme\Bundle\Entity\Listing", inversedBy="views")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="content_id", referencedColumnName="id")
* })
*/
private $listing;
To the view it brakes because when saving the view entity the content_id becomes null.
How can I fix it?
Relation on listing side:
/**
* #var views
*
* #ORM\OneToMany(targetEntity="\Acme\Bundle\Entity\View", mappedBy="listing")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id", referencedColumnName="content_id")
* })
*/
private $views;
I'm making queries by joining the Listing.views and adding WITH content_type = :ContentType which discriminates some 'view' results.
I'm coming pretty late to the party, but thought I would answer in case it helps someone else.
Because of the relation you've established between the two entities, you now have to assign an object to that property rather than the FK value itself. Like so:
$listing = $em->getRepository('Acme\Bundle\Entity\Listing')
->find($content_id);
$view->setListing($listing);
And not:
$view->setListing($content_id);
Of course, if it turned out to be something else, I'd be curious to know.
:^)
I have a project in which I have a OneToMany relationship on the same database.
Currently it is designed like this:
/**
* #ORM\OneToMany(targetEntity="MyEntity", mappedBy="myCopiedItem")
*/
protected $mySource;
/**
* #ORM\ManyToOne(targetEntity="MyEntity", inversedBy="mySource")
* #ORM\JoinColumn(name="selected_myentity_copy_id", referencedColumnName="id")
*/
protected $myCopiedItem;
But now I have to make this relationship ManyToMany. So I did this:
/**
* #ORM\ManyToMany(targetEntity="MyEntity", mappedBy="myCopiedItem")
*/
protected $mySource;
/**
* #ORM\ManyToMany(targetEntity="MyEntity", inversedBy="mySource")
* #ORM\JoinTable(name="entity_has_copy")
*/
protected $myCopiedItem;
but the "entity_has_copy" table that symfony created has only 1 item (myentity_id) and I want to have 2 fields "myentity_id" & "selected_myentity_copy_id" which are both actualy id's from my "myentity" table...
What do I have to modify in order to have both id's in my generated table?
I'm sure I've missed something, but I cannot figure out WHAT :(
Note: Entity / table names were renamed for privacy
Solved this!
I had to add the relationship inside the definition...
So this is the correct definition for the JoinTable part:
/**
* #ORM\ManyToMany(targetEntity="MyEntity", inversedBy="mySource")
* #ORM\JoinTable(name="entity_has_copy",
* joinColumns={#ORM\JoinColumn(name="entity_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="entity_copy_id", referencedColumnName="id")}
* )
*/
protected $myCopiedItem;
Hope this will help others that are having same issue...
If you want to read more about how associations between entities are mapped with Doctrine, here's a good URL!