Nested Queries in React Admin - react-admin

How can we possibly use Nested Queries? It just seems like a big waste when my server fully supports nested queries.
I'm also unable to make a nested input form.
I have a book, that has an author, and the author has a username with a first name and last name.
When creating the book, I want to show a list of Authors, but the optionText needs to be the author's user's first and last name.
I've been trying to figure it out but figured I'd post here since I've had no luck.
Schema:
Book {
id,
author_id
}
Author {
id,
user_id
}
User {
id,
name
}
In this case, When I want to create a new book (SelectInput), I want to show the Author's first and last name (which comes from their user) in the input.

Related

How to query a table whose name is dependable on the id?

I am making a website like Facebook. Whenever the user signup, a table named his/her 'id' is created in every database like messages, friends etc. My question is how to query that table.
This is what I tried so far:-
$id=$_SESSION['id'];
$id1=(string)$id;
$result=mysqli_query($conn2,"SELECT * from '$id1'");
while($row=mysqli_fetch_assoc($result))
{
echo $row['frnd_id'];
}
But I am getting an error.

Few questions about Grails' createCriteria

I read about createCriteria, and kind of interested on how these works, and its usability in providing values for dropdown box.
So say, i have a table in the database, Resource table, where i have defined the table in the domain class called Resource.groovy. Resource table has a total of 10 columns, where 5 of it are
Material Id
Material description
Resource
Resource Id
Product Code
So using the createCriteria, and i can use just like a query to return the items that i want to
def resList = Resource.createCriteria().list {
and {
eq('resource', resourceInstance)
ne('materialId', '-')
}
}
Where in the above, i want to get the data that matches the resource = resourceInstance, and none of the materialId is equal to '-'.
I want to use the returned data from createCriteria above on my form, where i want to use some of the column on my select dropdown. Below is the code i used for my select dropdown.
<g:select id="resourceId" name="resourceId"
from="${resList}"
disabled="${actionName != 'show' ? false : true}" />
How do i make it so that in a dropdown, it only shows the values taken from column Product Code? I believe the list created using createCriteria returns all 10 columns based on the createCriteria's specification. But i only want to use the Product Column values on my dropdown.
How do i customize the data if in one of the select dropdown in my form, i wanted to show the values as "Resource Id - Resource Description"? The values are combination of more than 1 columns for one select dropdown but i don't know how to combine both in a single select dropdown.
I read that hql and GORM query are better ways of fetching data from table than using createCriteria. Is this true?
Thanks
First of all refer to the document for using select in Grails. To answer all questions:
Yes, the list to select from in the dropdown can be customized. In this case it should be something like from="${resList*.productCode}"
Yes, this can be customized as well with something like
from="${resList.collect { \"${it.resourceId} - ${it.resourceDesc}\" } }"
It depends. If there are associations involved in a domain then using Criteria will lead to eager fetches which might not be required. But with HQL one gets the flexibility of tailoring the query as needed. With latest version of Grails those boundries are minimized a lot. Usage of DetachedCriteria, where queries etc are recommended whereever possible. So it is kind of mixing and matching to the scenario under consideration.

Selecting specific joined record from findAll() with a hasMany() include

(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?

How to design the schema for an author/user model in mongodb

I have looked through most of the mongodb schema design articles on mongo's website and most of the questions here on SO. There is still one use case which I haven't figured out. When looking at these tutorials, they usually reference the article comments problem and the products/categories problem. I want to figure out how to model the one to many relationship (author to posts) when querying a list of posts. Here are the example schemas:
Users: {
_id: ObjectID
Name: String
Email: String
}
Posts: {
_id: ObjectID
user_id: ObjectID
body: String
comments: [
body: String
]
}
Now, lets say you want to run a query for the latest 10 posts. A pretty simple query, but now you have posts with the possibility of each one having a unique ObjectID pointing to the user. Now, how should you accomplish getting the name and email of each user for a post.
Should you create an array of the user ObjectID's from the posts query and then run the query db.users.find({ _id: {$in: PostsUserIDArray}}); After that would you use your application logic to match the right user information to the correct post?
Should you keep a copy of the data in posts. I.E. keep the user ID, name, and email in the posts table. Then just have a hook when a user updates this information to update all the information in posts.
An option which myself or my friend have not thought of.
I appreciate all help as I try to wrap my head around mongo data modeling.
For a few videos I have seen from MongoDB creators, they advocate the second solution. If your user have more data than just a name and email and if you display only name and email when displaying apost, then it's not really bad to store it in the post. Thus you don't have to perform others queries when querying for posts. And since a user doesn't normally change his name every day, it's more effective to run an update to all posts once he changes his name than perform other queries to retrieve informations when displaying posts.
Edit : link to a video http://lacantine.ubicast.eu/videos/3-mongodb-deployment-strategies/

What sort of database design would I need to use in case I wanted users to save tags, and be able to call already used tags?

I'm trying to implement a feature similar to StackOverflow's tag feature. That a user can create a new tag, or by typing pull up a list of similar tags already created.
This is such a wonderful feature on this site and I find it sad that most sites do not have something like this. It's both robust, and yet very very flexible and best of all: driven by the community.
So I have these two tables:
Company
id
email
name
companySize
countryOfOrigin
industryid
Industry
id
description
Every time a user writes a new tag, I want to create one with a unique ID, and also be able to search for existing tags.
Will this database design allow for an easy and efficient implementation of this feature?
If not, please give a little guidance. :)
Whilst there's not a tremendous amount of information to go on, what you've listed should be fine. (The 'tag' being the 'description' field in the industry table, etc.)
As you might imagine, all of the real work is done outside of SQL, where you'll need to...
(Potentially) add new tag(s) that don't yet exist.
Associate the industry with the supplied tag(s).
(Potentially) prune previously used tags that may no longer be in use.
...every time you edit an industry.
That said, the key limitation of your proposed setup is that each company can only belong to a single industry. (i.e.: It can only have a single industry tag associated with it.)
As such, you might want to consider a schema along the lines of...
Company
id
...
countryOfOrigin
Industries
id
description
CompanyIndustriesLookup
companyID
industryID
...which would let you associate multiple industries/tags with a given company.
Update...
For example, under this setup, to get all of the tags associated with company ID 1, you'd use...
SELECT Industries.description FROM (CompanyIndustriesLookup, Industries)
WHERE companyID=1 AND industryID=Industries.ID
ORDER BY Industries.description ASC;
On a similar basis, to get all companies tagged with an industry of "testing", you'd use...
SELECT Company.name FROM (Company, Industries, CompanyIndustriesLookup)
WHERE Company.id=CompanyIndustriesLookup.companyID
AND Industries.id=CompanyIndustriesLookup.industryID
AND Industries.description="testing"
ORDER BY Company.name ASC
A very easy (if somewhat suboptimal, but it often does not matter) solution to use tags is to not have tag ids at all. So, you have:
Items
ItemId
Name
Description
...
ItemTag
ItemId
Tag
Adding a tag to an item is just adding the tuple to the ItemTag table, whether the tag already exists or not. And you don't have to do any bookkeeping on removing tags either. Just keep an index on ItemTag.Tag, to be able to quickly display all unique tags.