Sign up ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.
    $query = db_select('node', 'n')
                ->join('field_data_field_fenlei', 'c', 'n.nid = c.entity_id')
                ->join('taxonomy_term_hierarchy', 'th', 'c.field_fenlei_tid = th.tid')
                ->join('node_type', 'nt', 'n.type = nt.type')
                ->condition('n.type', 'product')
                ->condition( db_or()
                    ->condition('th.tid', arg(2), '=')
                    ->condition('th.parent', arg(2), '=') )
                ->fields('n', array('nid'))     // select nid field only so that node_load() will be called later
                ->extend('PagerDefault')        // this adds the pager
                ->limit(5);

$result = $query->execute(); 

product is the content type. field_fenlei is the category machine name . which is a select list, contains all the term.

i put the above code in the term template page file, the page shows unnormal(the page doesn't load the header part ....). there is something wrong with the query code?

is there a way to test whether the query code right or not?

share|improve this question
1  
What does the page shows unorder mean? It's very hard to understand what you're actually asking here –  Clive Dec 7 '12 at 14:12
    
the page doesn't load the header part .... –  123 Dec 7 '12 at 14:15
    
if i want to test whether the query code is right.how do i do? thank you –  123 Dec 7 '12 at 14:15
    
Do you mean how do you get the SQL string? You can cast the $query object to a string, e.g. print (string) $query; –  Clive Dec 7 '12 at 14:16
    
when i echo (string) $query; the page is blank why? –  123 Dec 7 '12 at 14:19

1 Answer 1

up vote 1 down vote accepted

Ohhh I've just realised why you're getting an error...join() isn't chainable, it returns the created alias to the joined table.

Try this instead:

$query = db_select('node', 'n')
  ->condition('n.type', 'product')
  ->condition( db_or()
    ->condition('th.tid', arg(2), '=')
    ->condition('th.parent', arg(2), '=') )
  ->fields('n', array('nid'))     
  ->extend('PagerDefault')        // this adds the pager
  ->limit(5);

$query->join('field_data_field_fenlei', 'c', 'n.nid = c.entity_id');
$query->join('taxonomy_term_hierarchy', 'th', 'c.field_fenlei_tid = th.tid');
$query->join('node_type', 'nt', 'n.type = nt.type');

$result = $query->execute(); 
share|improve this answer
    
this is the [queryString] => SELECT n.nid AS nid FROM node n INNER JOIN field_data_field_fenlei c ON n.nid = c.entity_id INNER JOIN taxonomy_term_hierarchy th ON c.field_fenlei_tid = th.tid INNER JOIN node_type nt ON n.type = nt.type WHERE (n.type = :db_condition_placeholder_0) AND( (th.tid = :db_condition_placeholder_1) OR (th.parent = :db_condition_placeholder_2) ) LIMIT 5 OFFSET 0 ) –  123 Dec 7 '12 at 14:39
    
there is still no any output. –  123 Dec 7 '12 at 14:40
    
why the where condition can't get the value? –  123 Dec 7 '12 at 14:41
1  
Apache error logs docs. Like I said, I can't help any further. It sounds like you might want to hire a consultant, you don't seem to understand the technologies you're using here –  Clive Dec 7 '12 at 14:42
1  
@123 I would suggest accepting this answer, as it exactly says why the code you shown here doesn't work. Between you and your colleague, you asked 8 questions to solve one issue. As you are not showing all the code used for the template file, it is not possible to understand why you don't get the expected result, considering we don't have important information, such as which theme it is set for the user testing the code, and for which theme that user edited the template file. –  kiamlaluno Dec 8 '12 at 9:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.