doctrine dql custom join with on - join

i have tables witch have no foreign key and no relation in doctrine
but logically they are related by some fields.
how can i join them with DQL and Query over them.
can i use ON between their fields?

as I found here:
Custom left join with unrelated models:
This is unfortunately not possible with the Doctrine 1 ORM. I think you would be best served to work directly with the database in this case.

Related

What's the best practice for connecting a table to multiple other tables where each has a many-to-many relationship?

I have a rails blog, with a PostgreSQL database, where I have a Post model and PodcastNote model. I want to add a 'Tag' model, where tags can be for both posts and podcast_notes. In both cases, it will be a many-to-many relationship. My question is what is the best practice for handling that?
I can think of 2 ways:
Have 1 join model that connects tags to both posts and podcast_notes.
Have 2 join models - one that connects tags to posts and another that connects tags to podcast_notes
Which would be considered the best practice in this case?
If best practice is option 1 above, how would I go about doing that?
One join model with a polymorphic association is the way most tagging gems solve this.
https://github.com/mbleigh/acts-as-taggable-on
https://github.com/pat/gutentag

How to join two tables in HTSQL specific to any columns

HTSQL works when two tables are properly linked with foreign key.
For example:
school can be easily joined with department if we have properly defined a foreign key say school_code of department table referencing code of school.
So far I'm not able to find any way to join two tables on some other columns.
Can any one help me how to do this in HTSQL?
The tweak.override extension allows you to define foreign-keys in a config file even if they're not present in the database itself. http://htsql.org/doc/admin/usage.html#extension-reference
tweak.override:
foreign-keys:
- program(school_code) -> school(code)
- program(school_code, part_of_code) -> program

Join queries in Doctrine on tables without specified relations

I have two tables that do not have a relation defined with each other in the schema.yml.
However, table 1 has a foreign key reference to the primary key of table 2. Clearly, I goofed up by not designing the database well, but now it's mitigation time.
I must do a left join between the two tables coupled with a where clause that will retrieve the select rows I want. And to do this, I do:
Doctrine_Query::create()->select('t.*, l.lid')->from('Taxonomy t')->leftJoin('t.Cid c') ->leftJoin('c.Lesson l')->where('t.section = ?','Critical reading');
This should typically do it, but it does not because what it returns is all the rows from taxonomy table irrespective of the where condition. I am thinking, is this because of the relation not being specified in the column? That would be ridiculous cause the query works, only in a doctrine context it does not.
Thanks
In doctrine you can only join using the relations you defined on your schema, this is a know limitation. You may use the Native SQL feature as a workaround.

How can I get this complicated query to work with ActiveRecord?

I have a complicated SQL query involving 3 models, creating virtual attributes based off of time operations... Ideally I would want to use ActiveRecord arel methods to generate this so that it's nicer, but I have no idea how.
find_by_sql <<--sql
SELECT username, count(*) as records, AVG(time_taken) as time_taken
FROM (
SELECT TIMESTAMPDIFF(HOUR, posts.created_at, reports.created_at) as time_taken,
users.username as username
FROM reports, users, posts
WHERE reports.user_id = users.id AND
reports.post_id = posts.id
) AS dashboard_data # this name is unused, but apparently required
GROUP BY username
sql
Is there any way to do something like this within ActiveRecord? Or is raw sql the only way to do complicated stuff like this?
Unless it's from a table you're going to be stuck doing things like this. You're fetching from a subquery which is way outside the scope of what ActiveRecord is intended to do. An ORM is designed to map objects to specific rows in well-defined tables.
You can sometimes fake it so that whatever you're doing appears to be sufficiently table-like that it works by using a SQL view. ActiveRecord will gladly use a view if it can be queried like a table. For instance if it has an id column defined that serves as some sort of primary key it should work. In your case you could GROUP BY users.id and have users.id AS id exposed as well as users.username.
I think this is equivalent to what you're doing:
User.joins({:reports=>:posts}).
group("users.username").
select("users.username,
count(*) as records,
avg(timestampdiff(hour, posts.created_at, reports.created_at)) as time_taken")

Hibernate many-to-many mapping

I am one of hibernate user in Australia. Recently, I have been dealing with hibernate many-to-many mapping and not going well though.
I got in trouble with "join/associate table with extra columns mapping".
Let`s say there are three tables called Product Order and OrderProduct (includes extra column quantity). Product holds many-to-many relationship with Order.
My confusion is that do we have to consider both ends of associate table when we are writing mapping files? or just write either side?
Also, is it necessary to produce mapping file for the associate table as well?
Any suggestions will be appreciated!!
create a view to join two tables. this view and rest table is many to one relationship
Hope it's userful
You should have an association mapping and entity because you need to create them just like an other entity.

Resources