Selecting from a table using different relationships - symfony1

I have three tables (objects). The Foo table has a 1:N relationship defined with the table Bar. The table FooBar is used to define a different N:M relationship between A and B.
I want to select all objects in Foo which have either a 1:N relationship with Bar and/or a N:M relationship using the same Criteria. So far, what I have is two different Criteria for each:
$c1n = new Criteria();
$c1n->addJoin(FooPeer::ID, BarPeer::FOO_ID);
$cnm = new Criteria();
$cnm->addJoin(FooPeer::ID, FooBarPeer::FOO_ID);
$objects1N = FooPeer::doSelect($c1n);
$objectsNM = FooPeer::doSelect($cnm);
Is it possible to include both in the same criteria? And if it is, how can I achieve that?

You can't do this directly, but see if this works for what you are looking for:
find query for many to many relation propel
You should be able to easily hydrate the 1:N relationship.
foreach($query as $result) {
$result->getBar()->getColumnName();
}

Related

Can't use my joining table from DbContext

I have database with different tables. But I can't get access to some tables, and I'm curious why? For example, there is Projects table and Users table in database, and I need to get list of all users that are in specific Project by Id. These tables have many to many relationship, so there is joining table with ProjectId and UserId. It's logic to use some Join to get users that I need from table Users with help of this joining table from db. But how can I make Join when my DbContext doesn't have joining table? How can I get access to joining table using DbContext, or maybe there are some different ways to get list of users by project id?
If someone have similar problem, here is a simple way to use all the data from collections in model
var project = _db.Projects.Include(x => x.Tickets).ThenInclude(y => y.Users).SingleOrDefault(p => p.Id == id);
so after that you gonna have your model and all the data from collections that you need
ProjectUser - is joining table, use for link two tables. This tables must have (usually) Collection for access to this many-to-many data.
So you can will have collection of users for this project. And doing filtering by users from your Ticket (example from image).
Yes, exists few different ways - setting with ef fluent, or attribute, or dbcontext. Better to find complete guide for many-to-many setting in your application.

What will be the DB Context code for executing this SQL?

I am new to DbContext and I am confused about how to join tables using DbContext - can someone tell me the code equivalent to this SQL?
Select *
From AssignedCourses
Join Students On StudentRollnumber = Students.StudentRollno
Join Courses On Courses.CourseId = AssignedCourses.AssignedCourseId
With EF you don't seldom need joins. Provided your relations are setup right in your backend, tools generate the models for you using navigational properties. Even without tools, it is easy to create models with navigatinal properties. That query could be written in different ways depending on your real needs. With navigational properties, generally you don't have the middle bridging table, but directly reference to Courses from Student (courses that a student is enrolled) or Students from Course (Students that are enrolled to that course). Assuming you want primarily the Students and the Courses they were enrolled to:
var result = ctx.Students;
// or preload them the Courses as well
var result = ctx.Students.Include(s => s.Courses);
you can use LINQ query :
from ac in db.AssignedCourses join s in Students on StudentRollnumber equals s.StudentRollno join c in Courses on c.CourseId equals ac.AssignedCourseId

HQL query joining two domains that know nothing about one another

My domain model includes several domain classes like so:
The Budget and Employee classes each know about a Position, but the Position class has no knowledge about the Budget or Employee classes. I would like to use GORM to do a query where I get back a list of Budgets and their associated Employees.
I have resorted to using HQL, since it doesn't look like there is a way to use a Criteria query across domains. My query looks like
from Budget as budget, Employee as employee where budget.position = employee.position
The problem with this query is that there is not a one-to-one mapping from Employees to Positions. That is, there can be Positions "floating" out there where no Employee is assigned to that Position. And a given floating position can be assigned to a Budget but not to an Employee.
I would like to use a left outer join in HQL, something like
from Budget as budget left outer join Employee as employee on employee.position = budget.position
But it looks like HQL only supports doing joins on properties that are directly on the domain classes. Neither Budget nor Employee knows about one another, so I'm not sure if this is possible. Is there a way to do the query I want in HQL?
Can you modify the domain class definition in grails for Position as such to let gorm know about the association?
Position {
static hasMany = [budgets:Budget, employees:Employee]
}
Then you can do and hql statment like such:
from Budget b left join b.position p where p.employees is not empty

How do I cascade a delete from the many side of a unidirectional one-to-many in GORM

If I have the following unidirectional one to many relationship between two domain classes:
class Single {
static hasMany = [ multiples: Multiple ]
}
class Multiple {
...
}
Is there a way to cascade the delete from the Multiple class to the many relationship on Single?
What I have found that works is to add to add an onDelete: Cascade property to the FK that exists on the multiple_id column of the join table via the database script - however what I would really like to do is to express this in the GORM mapping DSL (and without creating a separate domain class to explicitly represent the join table). But there does not appear to be a way to manage relationships on the implicit join table of a uni one-to-many - unless I've missed something.
Is there a reason not to use bidirectional? If not, take a look at http://grails.org/doc/2.4.x/ref/Domain%20Classes/belongsTo.html?

How to search on joining table in Rails 3 which doesn't have a model?

I have two models merchant and category with a HABM relationship so have a joining table called categories_merchants
How can I write the following query in ActiveRecords?
SELECT
categories_merchants.merchant_id
WHERE
categories_merchants.category_id IN (1,2,3,4)
NOTE: The joining table doesn't have a model, it works automatically in Rails 3 without need for one.
If you want to do such a query you should transform your HABTM in a has_many :trough and create a model for the joining table and query based on that.
Alternatively, but possibly with worse performance, you could do:
Merchant
.select(:id)
.joins("categories_merchants ON categories_merchants.merchant_id = merchant.id")
.where("categories_merchants.category_id IN (?)", [1,2,3,4])

Resources