I have a many-to-many relationship for objects in an SQL database in a Dancer server, and I need to be able to search objects based on a criteria on the other. In other words, I need to be able to do what is asked in this question but in Dancer.
The relationship is modeled as described in the Dancer DBIx::Class documentation here.
I see examples of how to search based on one-to-many relationships here but I have not been able to translate this to many-to-many.
If you read the DBIx::Class docs carefully, you'll see that many-to-many is not a relationship but a relationship bridge.
You can still filter on related columns by joining the relationships that form the many-to-many:
my $rs = $schema->resultset('Artist')->search({
'tracks.name' => 'Always',
},{
join => { cds => 'tracks' },
}
);
Related
There are 3 initial tables, Product, User, and Tag. (Not really a product table but it makes for a simpler example)
There's a many-to-many relationship between a User and Product, as well as between a User and Tag. So, I created 2 association tables for this relationship -> UserProduct and UserTag
Now, a user can create multiple tags, and add those tags to a product. From my knowledge, this can be achieved by creating another association table, UserProductTag, between the association tables UserProduct and UserTag.
I wasn't able to find many examples of this use case on the internet. That made me wonder, is this the correct approach? Or am I overdoing normalization?
This gets further complicated by the fact that I've to achieve this in SQLAlchemy as well and I've no idea (but I guess that's a different question).
I believe what you are looking for is what is often called "ternary relationship".
Please see these two SO questions and answers to give you implementation ideas:
How to three-way many-to-many relationship in flask-sqlalchemy
Inserting relationships into a table which connects 3 tables with many to many relationships with SQLALchemy - python
From the Grails documentation, by default one-to-many relationships are represented using a join table.
I don't understand why this is desirable. I had little SQL experience before starting to use Hibernate and/or Grails' GORM. It seems like using a foreign key in the 'many'-side table pointing at a row on the 'one'-side table is the way to implement a one-to-many relationship...
Can anyone explain this sort of design decision?
The reason for using an join table for a unidirectional one-to-many relationship is because the many side of the relationship may have many relationships and does not know of those relationships. Perhaps an example is best here:
class Book {
String title
}
class BookStore {
String name
static hasMany = [books: Book]
}
class Library {
String name
static hasMany = [books: Book]
}
In the above domain, a Book has no need to have both the BookStore and Library IDs on it's table. A Book is perfectly valid without either. By using join tables this keeps from polluting the book table with foreign keys.
Keep in mind because this is modeling uni-directional and not bi-directional relationships.
I have an initial migration that sets up two tables (users and projects), with a relationship (innoDB).
$table->foreign('user_id')->references('id')->on('users');
I have two Eloquent models set up, blank except for the relationship:
return $this->has_many('Project');
Do I definitely need to tell eloquent about the relationship in the models and the database? I'd assumed something as comprehensive as Laravel would infer it from the Schema? Is there something I'm missing?
You're right, you have to define the relationships in your Models.
Laravel doesn't analyze the schema, foreign key indexes are actually not mandatory to use.
This bootstraping may could be avoided, but it also allows you to use non-conventional table and column names or use the Fluent Query Builder to modify the relationship queries.
Less magic, more control for now.
I have Kohana ORM/Mysql query problem. I hope you can help.
To start of, here's the diagram of tables:
Here's my ORM definitions:
MEMBERS has many TOPICS through MEMBERS_TOPICS
ARTICLES has many TOPICS though ARTICLES_TOPICS
TOPICS has many MEMBERS though MEMBERS_TOPICS
TOPICS has many ARTICLES though ARTICLES_TOPICS
Think of it as a mailing list where you have members that has chosen topics of articles in which topics are also assigned to particular topics.
I couldn't figure out how to make a single query so I can return joined results and send an email to individual members with articles they only chose through the topics they've chosen.
I hope to receive wisdom with mysql/kohana ninjas around. :D
There is a with() method in ORM that will join your tables, provided you have relations setup correctly in your models. Below a small example:
Model:
public function find_all_orders()
{
return $this->with('customer')->with('product')->find_all();
}
View:
foreach ($orders as $order)
{
echo $order->product->name . ' ' . $order->customer->name;
}
The relations are:
order belongs to product, product has many orders
order belongs to customer, customer has many orders
ORM does have a join() method: http://kohanaframework.org/3.2/guide/api/ORM#join
There's possibly a more elegant KO3 way to do what you want, but I'm not entirely familiar with KO3 ORM, perhaps someone else can suggest a better way.
[edit]
Some further reading:
Kohana 3 ORM: How to perform query with 2 many to many relationships
I have a Subscriber object that contains a list of Provider objects. Providers can belong to many Subscribers, hence the many-to-many relationship. This is fine, except that the Provider needs to define a Status property but this cannot be stored in the Provider table, as the same provider could have a different Status for different Subscribers, so I am storing the Status in the many-to-many table. At the moment I have a basic many-to-many mapping:
HasManyToMany(s => s.Providers)
.Table("SubscriberProviders")
.ParentKeyColumn("SubscriberID")
.ChildKeyColumn("ProviderID");
How can I set the Status property, of the Provider, within the many-to-many mapping?
Many thanks
A many-to-many mapping can't have properties of its own, so you have to map the join table into an artificial ProviderSubscriber entity, which will be one-to-many from the Provider.
For a full example of a workaround, see Many-to-many relationships with properties
You'll have to map the cross-reference table (which NH currently generates for you), and change the mapping between Providers and Subscribers to instead be a HasMany() on either side referencing the cross-reference table.