Closed (fixed)
Project:
Domain
Version:
5.x-1.8
Component:
Code
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
17 Sep 2008 at 21:28 UTC
Updated:
28 Oct 2011 at 11:41 UTC
Hi,
I have the following SQL query which shows latest links from all sites.
I would very much only see the latest links from the current site.
Is this possible to achieve with just changing this Query? or may be setting an if something like, if $current_domain?
$result = db_query(db_rewrite_sql("SELECT n.nid, n.title
FROM {node} n
INNER JOIN {links_node} wn ON n.nid = wn.nid
INNER JOIN {links} w ON wn.lid = w.lid
WHERE n.type = 'weblink'
AND n.status = 1
AND n.moderate = 0
ORDER BY n.created DESC
LIMIT $nlimit"));
Thanks a lot in advance for your suggestion!
greetings,
Martijn
Comments
Comment #1
nonsieSomething like this should work for nodes on current domain (published to current or all domains):
Comment #2
agentrickardYou should remember to escape db_query() and use db_rewrite_sql() for security reasons.
Comment #3
summit commentedThanks a lot for your quick reply!
How to escape db_query() please, is this correct?
This is the whole snippet then, right?
greetings,
Martijn
Comment #4
agentrickardSee http://api.drupal.org/api/function/db_query/6 and http://api.drupal.org/api/function/db_rewrite_sql/6
Looking at the query, I do not understand why you have the extra INNER JOINs there -- since you are not selecting any data from those tables, it seems like wasted effort.
The query you have written will (in effect) be created automatically if you pass:
db_rewrite_sql() will magically add the node_access logic; pager_query() adds database-safe LIMIT clauses. Here's a sample query from the blog module:
Here's how that query is actually run after the query alter functions run:
If you need to filter out 'all affiliates,' you may need to tweak the query a bit.
You may, in this case, need to specify your primary table to db_rewrite_sql() -- it's hard to say. If you use db_rewrite_sql(), the node access system will filter your query for you, but it will return items that match either the active domain or 'all affiliates.'
Comment #5
summit commentedHi Ken,
I will alter the query, thanks for your suggestions!
Am I possible to show the items that match ONLY the active domain and NOT all affiliates?
greetings,
Martijn
Comment #6
agentrickardI think for that you have to add a JOIN to {domain_access}, try:
This should be ok.
Comment #7
summit commentedHi Ken,
It still doesn't work.
As example a substract of the domain_access table
For example I only want to show the nodes with the gid 7 (current subdomain I am looking at). How to achieve this?
I tried the following, but it doesn't work yet :(. I want only the nodes which are given access by the current (sub)domain.
Thanks a lot in advance again for going into this!
I think the key is AND da.gid = %d how to get the (subdomain) value for this, right?
Comment #8
agentrickardUse the Devel module to debug the query that is actually being written to the db.
The
global $_domainloads the currently active domain, which is used in the query argument as$_domain['domain_id']It is also possible that you are returning an empty result set for other reasons, such as no nodes that match the {node} table part of the query, or the use of Domain Strict.
And Domain Views build these for you, if you are using Views 1.
Comment #9
agentrickardComment #10
summit commentedHi,
Didn't have time to test this yet. Sorry for not responding quicker, perhaps monday time for this.
greetings,
Martijn
Comment #11
agentrickardComment #12
summit commentedHi, I finally got to it, this worked for me:
Greetings, Martijn