Let me explain how I am making things (maybe not the best way by the way).
I want to join my StoreSchedule entity (which contains a triple relation : store (which is where I stock all informations about adress, name, picture of my stores), days (the 7 days of the week) and Schedules (only strings like '09:00-22:00').
To combine thoses 3 entities, I made StoreSchedule, that has a triple relation where I cross the 3 informations. Maybe I am not explaining well, let me show you some screens.
What I already tried to do with my QueryBuilder in my repository:
https://imgur.com/a/bE52iBH
How I structured things in my StoreSchedule table :
https://imgur.com/a/6m7YkhI
My Schedule table :
https://imgur.com/a/75bRriS
Days table contains the 7 days of the week. Maybe that's obvious, maybe not.
So here's the thing, I can't figure out how to make the I need : a query that gets all content from Store and joins StoreSchedule where ID = Store.ID
I want to get days and Schedule with the ID from store.
Can I do that with query builder?
Do I have to modifiy my database? Are my relations good?
Best regards!
ps : hope I made myself clear enough..
You make it too complicated. You can purge the days table AND StoreSchedule. Just make a many-to-one relation between Store and (Store)Schedule. Add a column "day" in your schedule table. A short integer will do if you store the number of the day. See php's date function.
For your query (the first link) you won't need that WHERE clause. Doctrine will join the related data automatically for you.
public function getAllContentStoreAndSchedule()
{
return $this->createQueryBuilder('st')
->leftJoin('st.schedule', 'sc')
->addSelect('sc')
->orderBy('st.name', 'ASC')
->addOrderBy('sc.day', 'ASC')
->getQuery()
;
}
Related
I'm working on my first (kinda) big personal project and I am stuck. I have 4 tables, 3 of which have foreign keys linking into tbl_model_details. All tables are listed below.
tbl_model_details
tbl_model_type
tbl_model_name
tbl_model_scale
Ideally I want to show data through my controller with HTTP Get. I can get Postman to to return data from my controller using _context.tbl_model_details.ToList();
Currently Postman is showing the id's for the other tables, but want them to show data from other columns within those tables instead of the id.
Within SQL I was able to build this query which displays the information I would like from the other tables, Is there an equivalent that I could make to run inside my controller? Or is there a way I can use this query that I have already made?
SELECT model_scale, model_name, info, picture, model_type, part_number, amount_owned, modified, limited_addition, date_purchase, price_paid, upc
from tbl_model_details
join tbl_model_type
on tbl_model_details.type_id = tbl_model_type.type_id
join tbl_model_name
on tbl_model_details.name_id = tbl_model_name.name_id
join tbl_model_scale
on tbl_model_details.scale_id = tbl_model_scale.scale_id
Any help from you guys would be great.
Thanks
You can use Entity Frameworks LINQ Include. This will allow you to include the sub-models in the same query:
_context.tbl_model_details
.Include(details => details.tbl_model_type)
.Include(details => details.tbl_model_name)
.ToList();
Without knowing your relationships, DBSet and Model setups, I can say that the statement will look exactly like the one I mentioned, but this may help you get on the right track.
This will allow you to later retrieve data from the sub-models:
#Model.tbl_model_scale.model_scale;
I'm here again. I'm developing an example of tabular model on Analysis Services and I've an error that I'm not able to solve.
This is my tables diagram just now:
The relevant tables are MARCHAMOS, ARFABRI and DimFecha (the top ones). MARCHAMOS and ARFABRI are joined by 2 relationships (2 fields of MARCHAMOS could be paired with 1 field of ARFABRI (depends of the situation)).
Now I'm trying to get a measure with the following DAX query:
UnidadesFrescosCompradas:= countx(
calculatetable(
filter(
marchamos;
not isempty (relatedtable('ARFABRI'))
);
MID(MARCHAMOS[REGIS03]; 1; 1) = "1";
TIPOS_COCHINILLO[GRUPO]=0;
userelationship(MARCHAMOS[REGIS28];ARFABRI[ARFA01]);
userelationship(ARFABRI[ARFA06]; DimFecha[Fecha])
);
[regis01])
As you can see, I'm telling the system the relationships that it must use (one of them is from MARCHAMOS to ARFABRI and the other one is from ARFABRI to DimFecha). But I get this error:
If I remove the "userelationship(ARFABRI[ARFA06]; DimFecha[Fecha])" caluse of the filter, the error disappears, but the result is not correct.
I don't understand this, since both relationships are not over the same tables.
Any hint?
Thank's in advance
You can USERELATIONSHIP with both active and inactive relationships. It looks like you have an ambiguity in that you have two eligible relationships for MARCHAMOS and ARFABRI.
Since the ending point column for ARFABRI is the same in both relationships you get the error.
Can you delete the inactive relationship?
I'm trying to obtain a list of articles and order them by their popularity over time. For example, older articles should rank lower even if they have a higher number of views.
In order to do this each article has a view count and a posted date. I'm guessing the simplest way would be to divide the article view count by the date posted... something like:
(view_count+comment_count) / date_posted = trend_score
I'm trying to understand if this is possible with the Django ORM, even if it is raw SQL? Would appreciate any help.
I guess the simpliest and most effective way to do is to add a trend_score field to your model and update it when the model is saved (you neeed to save the model anyways if you have a view count/comment count on it). Then you can easily filter by this field. You can fore sure do it somehow with SQL, but if you have to update the values you need to update already within your model, calculate also the score upon saving.
(I tried posting this to the CFWheels Google Group (twice), but for some reason my message never appears. Is that list moderated?)
Here's my problem: I'm working on a social networking app in CF on Wheels, not too dissimilar from the one we're all familiar with in Chris Peters's awesome tutorials. In mine, though, I'm required to display the most recent status message in the user directory. I've got a User model with hasMany("statuses") and a Status model with belongsTo("user"). So here's the code I started with:
users = model("user").findAll(include="userprofile, statuses");
This of course returns one record for every status message in the statuses table. Massive overkill. So next I try:
users = model("user").findAll(include="userprofile, statuses", group="users.id");
Getting closer, but now we're getting the first status record for each user (the lowest status.id), when I want to select for the most recent status. I think in straight SQL I would use a subquery to reorder the statuses first, but that's not available to me in the Wheels ORM. So is there another clean way to achieve this, or will I have to drag a huge query result or object the statuses into my CFML and then filter them out while I loop?
You can grab the most recent status using a calculated property:
// models/User.cfc
function init() {
property(
name="mostRecentStatusMessage",
sql="SELECT message FROM statuses WHERE userid = users.id ORDER BY createdat DESC LIMIT 1,1"
);
}
Of course, the syntax of the SELECT statement will depend on your RDBMS, but that should get you started.
The downside is that you'll need to create a calculated property for each column that you need available in your query.
The other option is to create a method in your model and write custom SQL in <cfquery> tags. That way is perfectly valid as well.
I don't know your exact DB schema, but shouldn't your findAll() look more like something such as this:
statuses = model("status").findAll(include="userprofile(user)", where="userid = users.id");
That should get all statuses from a specific user...or is it that you need it for all users? I'm finding your question a little tricky to work out. What is it you're exactly trying to get returned?
So, another in my series of Kohana 3 ORM questions :)
I have, essentially, a pivot table, called connections. The connections table connects a song to a keyword. That's all great and working (thanks to my last two questions!)
I want to output the most connected songs by keyword. So, to somehow query my connections table and output an object (with an arbitrarily limited number of iterations $n) that ranks songs by the number of times they have been connected, ie. the number of times that particular song_id appears for that particular keyword_id.
I have literally no idea how to achieve this, without querying every single row (!!!) and then counting those individual results in an array.... There must be a more elegant way to achieve this?
I believe this is more of an SQL question. Using the DB query builder:
DB::select('songs.*')->select(array('COUNT("keywords.id")', 'nconnections'))
->from('songs')
->join('connections', 'LEFT')->on('connections.song_id', '=', 'songs.id')
->join('keywords', 'LEFT')->on('connections.keyword_id', '=', 'keywords.id')
->group_by('songs.id')
->order_by('nconnections')
->as_object('Model_Song')
->execute();
or in SQL
SELECT `songs`.*, COUNT(`keywords`.`id`) AS `nconnections` FROM songs
LEFT JOIN `connections` ON `connections`.`song_id` = `songs`.`id`
LEFT JOIN `keywords` ON `connections`.`keyword_id` = `keywords`.`id`
GROUP BY `songs`.`id` ORDER BY `nconnections`
should return the result you want.
You'll want to have an accessible property called nconnections in your song model. The simplest way to do that is to add a public member so you don't tamper with ORM's inner workings.
I'm assuming you're using a model called 'Song', linked to a 'songs' table, a 'Keyword' model linked to a 'keywords' table and in the 'connections' table foreign keys 'song_id' and 'keyword_id' for each model respectively.