many-to-many

MVC4 save data using Many to Many relationship

£可爱£侵袭症+ 提交于 2019-12-02 08:27:56
Many to Many Relationship protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Company>() .HasMany(c => c.Tags) .WithMany(t => t.Companies) .Map(m => { m.MapLeftKey("Companyid"); m.MapRightKey("tagid"); m.ToTable("CompanyTags"); } } Add company var company = new Company() { Name = "FooBar Inc" }; Add Tag int tagId = _db.Tags.Where(x => x.Title == tag).Select(x => x.Id).SingleOrDefault(); if (tagId==0) company.Add(new Tag { Title = tag}); else ?????? //still create a relationship in CompanyTags (companyid,tagid) context.Companies.Add(company); context

Eloquent filter on pivot table created_at

我只是一个虾纸丫 提交于 2019-12-02 07:36:08
I want to filter the contents of two tables which have an Eloquent belongsToMany() to each other based on the created_at column in the pivot table that joins them. Based on this SO question I came up with the following: $data = ModelA::with(['ModelB' => function ($q) { $q->wherePivot('test', '=', 1); }])->get(); Here I'm using a simple test column to check if it's working, this should be 'created_at'. What happens though is that I get all the instances of ModelA with the ModelB information if it fits the criteria in the wherePivot() . This makes sense because it's exactly what I'm telling it

Many-to-Many relationships needing an associate table

时间秒杀一切 提交于 2019-12-02 07:14:08
In many database design tutorials/articles, they always bring up the fact that if two tables share a many-to-many relationship, then a third table should be created to act as an associate table to link the two. However , they never provide the reason WHY we should do it that way. I'm curious to know why and examples of problems that could occur if you just kept the two tables as it is without the associate table. You can't make many to many relations any other way using relational databases. Ie, if you have a table called "person", you can't create a column "friends" and expect to put many

Hibernate Many-to-Many, duplicates same record

落花浮王杯 提交于 2019-12-02 07:05:19
I tried Hibernate Mapping Many-to-Many using Annotations with the example given in vaannila. http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html Set<Course> courses = new HashSet<Course>(); courses.add(new Course("Maths")); courses.add(new Course("Computer Science")); Student student1 = new Student("Eswar", courses); Student student2 = new Student("Joe", courses); session.save(student1); session.save(student2); This thing works fine. But if I try to add another Course later, to a existing student like, Set<Course> courses = new HashSet

Many-to-Many relationships needing an associate table

﹥>﹥吖頭↗ 提交于 2019-12-02 07:04:46
问题 In many database design tutorials/articles, they always bring up the fact that if two tables share a many-to-many relationship, then a third table should be created to act as an associate table to link the two. However , they never provide the reason WHY we should do it that way. I'm curious to know why and examples of problems that could occur if you just kept the two tables as it is without the associate table. 回答1: You can't make many to many relations any other way using relational

Recursive CTE - Get descendants (many-to-many relationship)

為{幸葍}努か 提交于 2019-12-02 06:12:14
What I have: Given a tree (or more like a directed graph) that describes how a system is composed by its generic parts. For now let this system be e.g. the human body and the nodes its body parts. So for instance 3 could be the liver that has a left and a right lobe ( 6 and 9 ), in both of which there are veins ( 8 ) (that can also be found at any unspecified place of the liver, hence 8 -> 3 ) but also in the tongue ( 5 ). The lung ( 7 ) - which is in the chest ( 4 ) - also has a right lobe, and so on... (Well, of course there is no lung in the liver and also a 6 -> 7 would be reasonable so

Rails 3 many to many query condition

寵の児 提交于 2019-12-02 05:55:24
问题 I'm trying to do a simple Post/Tags relation in rails 3. Everything working fine except when I want to query the Posts which are related to several tags. Basically I'd like to do this kind of thing : Post.joins(:tags).where('tags.name ILIKE ?', var) But instead of having just one var, I'd like to use an array. I tried : Post.joins(:tags).where('tags.name IN (?)', names_array) But unfortunately it does a simple LIKE (not ILIKE) and works like a OR condition which sounds perfectly logical. I

Get data of all entities in a many-to-many relationship

帅比萌擦擦* 提交于 2019-12-02 03:20:53
picture of DB My database has 3 tables Customers, Films and CustomersFilms is the brige table between Films and Customers now, I want to diaplay a table of cutomers with the movies they rented. I wrote a function that does the job for spesific id ( in the code 8 and 9 ). My Question is how to show the data to all entitys ? public ActionResult GetData() { MyDatabaseEntities2 db = new MyDatabaseEntities2(); var q = (from c in db.Customers from Films in db.Films where Films.FilmId == 8 where c.CustomerId == 9 select new { c.CustomerName, c.Phone, c.FilmId, c.CustomerId, FilmName = Films.FilmName

JPA and many-to-many relations in google app engine

*爱你&永不变心* 提交于 2019-12-02 02:42:34
I have entities A and B, and A can have set of B. The same instance of B can belong to several A. So there is classical many-to-many relation here. In GAE there is no direct support of many-to-many relations, instead they're offering an ability to use sets of keys for related relations. So in A I will maintain set of keys of records in B. Now the problem is - how can I query for objects of type B belonging to given object of type A and matching certain criteria? In plain SQL I would do that like: select B.* from B inner join A on B.A_ID=A.ID where B.property0=criteria1 and B.property1

Many to many field django add the relationship both way

佐手、 提交于 2019-12-02 02:09:24
问题 I'm trying to do a function that allow a user to follow another one. the problem is when I'm adding a new user to the "followings" the user that follow another user is also added in the following list of the followed user. For example if user a follow user b I will have that: view.py def follow_test(request): name = request.POST.get('name', '') user_followed = Dater.objects.get(username=name) current_user = Dater.objects.get(id=request.user.id) print "Current", current_user.followings.all() #