many-to-many

How do I work with many-to-many relations in Yii2

北战南征 提交于 2019-11-26 19:12:31
问题 For example in one-to-many due to documentation (http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data) you can link two models in this way (one-many = company-zone) : $defaultZone = new Zone; $defaultZone->name = Zone::DEFAULT_ZONE; $company->link('zones', $defaultZone); But how it works for many-to-many relations when you have transit table like tbl_user_market(user_id, market_id) ? 回答1: When using a junction table for many-to-many relations, you have to Define the

How to define Many-to-Many relationship through Fluent API Entity Framework?

南楼画角 提交于 2019-11-26 18:58:29
问题 Below is my model: public class TMUrl { //many other properties //only property with type Keyword public List<Keyword> Keywords{get;set;} } public class Keyword { //many other properties //only property with type TMUrl public List<TMUrl> Urls{get;set;} } So clearly, both the entities have many-to-many relationship. I chose fluent api to tell the entity-framework about this relationship i.e. modelBuilder.Entity<TMUrl> .HasMany(s => s.Keywords) .WithMany(s => s.URLs).Map(s => { s.MapLeftKey(

Generic many-to-many relationships

走远了吗. 提交于 2019-11-26 18:51:46
问题 I'm trying to create a messaging system where a message's sender and recipients can be generic entities. This seems fine for the sender, where there is only object to reference (GenericForeignKey) but I can't figure out how to go about this for the recipients (GenericManyToManyKey ??) Below is a simplified example. PersonClient and CompanyClient inherit attributes from Client but have their own specific details. The last line is the sticking point. How do you allow message recipients to be a

Entity Framework Code First Many to Many Setup For Existing Tables

假装没事ソ 提交于 2019-11-26 18:26:44
问题 I have the following tables Essence , EssenseSet , and Essense2EssenceSet Essense2EssenceSet is the linking table that creates the M:M relationship. I've been unable to get the M:M relationship working though in EF code first though. Here's my code: [Table("Essence", Schema = "Com")] public class Essence { public int EssenceID { get; set; } public string Name { get; set; } public int EssenceTypeID { get; set; } public string DescLong { get; set; } public string DescShort { get; set; } public

MVC 4 - Many-to-Many relation and checkboxes

匆匆过客 提交于 2019-11-26 18:25:17
问题 I'm working with ASP.NET MVC 4 and Entity Framework. In my database, I have a table Subscription which represents a subscription to public transports. This subscription can provide access to several public transport companies (so a subscription could have 1, 2, 3, ... companies) then it is a Many-to-Many relation between these tables (I have an intermediate table between them). I want to allow the creation of a subscription throught a page which will contain a field Amount of the subscription

SQL - many-to-many table primary key

点点圈 提交于 2019-11-26 17:01:47
This question comes up after reading a comment in this question: Database Design When you create a many-to-many table, should you create a composite primary key on the two foreign key columns, or create a auto-increment surrogate "ID" primary key, and just put indexes on your two FK columns (and maybe a unique constraint)? What are the implications on performance for inserting new records/re-indexing in each case? Basically, this: PartDevice ---------- PartID (PK/FK) DeviceID (PK/FK) vs. this: PartDevice ---------- ID (PK/auto-increment) PartID (FK) DeviceID (FK) The commenter says: making the

Query on a many-to-many relationship using Doctrine with Symfony2

两盒软妹~` 提交于 2019-11-26 16:54:11
问题 I'm triyng to understand how the many to many relationship works with Doctrine and Symfony2. I've recreated the example shown in the official documentation (goo.gl/GYcVE0) and i have two Entity Classes: User and Group as you can see below. <?php /** @Entity **/ class User { // ... /** * @ManyToMany(targetEntity="Group", inversedBy="users") * @JoinTable(name="users_groups") **/ private $groups; public function __construct() { $this->groups = new \Doctrine\Common\Collections\ArrayCollection();

Django - Cascade deletion in ManyToManyRelation

为君一笑 提交于 2019-11-26 16:50:20
问题 Using the following related models (one blog entry can have multiple revisions): class BlogEntryRevision(models.Model): revisionNumber = models.IntegerField() title = models.CharField(max_length = 120) text = models.TextField() [...] class BlogEntry(models.Model): revisions = models.ManyToManyField(BlogEntryRevision) [...] How can I tell Django to delete all related BlogEntryRevision s when the corresponding BlogEntry is deleted? The default seems to be to keep objects in a many-to-many

Symfony2-Doctrine: ManyToMany relation is not saved to database

心已入冬 提交于 2019-11-26 16:03:33
I have two PHP model classes named Category and Item. A Category may have many Items and an Item may belong to many Categories. I have created a ManyToMany relation to both classes: class Category { /** * @ORM\ManyToMany(targetEntity="Item", mappedBy="categories", cascade={"persist"}) */ private $items; /** * Add items * * @param Ako\StoreBundle\Entity\Item $items */ public function addItems(\Ako\StoreBundle\Entity\Item $items) { $this->items[] = $items; } /** * Get items * * @return Doctrine\Common\Collections\Collection */ public function getItems() { return $this->items; } } And: class Item

MongoDB Many-to-Many Association

£可爱£侵袭症+ 提交于 2019-11-26 15:42:38
How would you do a many-to-many association with MongoDB? For example; let's say you have a Users table and a Roles table. Users have many roles, and roles have many users. In SQL land you would create a UserRoles table. Users: Id Name Roles: Id Name UserRoles: UserId RoleId How is same sort of relationship handled in MongoDB? diederikh Depending on your query needs you can put everything in the user document: {name:"Joe" ,roles:["Admin","User","Engineer"] } To get all the Engineers, use: db.things.find( { roles : "Engineer" } ); If you want to maintain the roles in separate documents then you