2

I have a content type B with a node reference field (from url) pointing to content type A. When current user is at "node/add/typeB/nid_of_type A" page then how can i check the database if he has already created a B node for that A node?

I am trying the following code in my template php:

function theme_name_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'b_node_form') {
global $user;
if (arg(0) == 'node') {
 $nid = arg(3);
}
$query = db_select('node', 'n')
->condition('type', 'b', '=')
->condition('uid', $user->uid, '=');
$query->join('field_data_field_a_ref', 'ar', 'n.nid = ar.entity_id');
//field_a_ref is the node reference field from B to A type
$query->condition('field_a_ref_nid', $nid, '=');
$query->groupBy('ar.entity_id')->execute();
if(!empty($query)) {
drupal_goto('/');

    } else {

   }
  }
 }

But i get this error:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM node n INNER JOIN field_data_field_a_ref ar ON n.nid = ar.entity_id W' at line 1: SELECT FROM {node} n INNER JOIN {field_data_field_a_ref} ar ON n.nid = ar.entity_id WHERE (type = :db_condition_placeholder_0) AND (uid = :db_condition_placeholder_1) AND (field_a_ref_nid = :db_condition_placeholder_2) GROUP BY ar.entity_id; Array ( [:db_condition_placeholder_0] => vote [:db_condition_placeholder_1] => 1 [:db_condition_placeholder_2] => 7 ) in theme_name_form_alter() (line 109 of C:...\sites\all\themes\theme_name\template.php).

Any help?

3
  • Could you pop the exception you're getting in as well? Are you using the References modules or the Entity Reference module for your node reference?
    – Clive
    Feb 9, 2013 at 15:59
  • Hi Clive. I added the error in my question. I'm using Refrences and Node reference from url widget modules to achieve this.
    – Billy
    Feb 9, 2013 at 16:10
  • Check the answer I've put in, that should do the trick
    – Clive
    Feb 9, 2013 at 16:29

1 Answer 1

1

Took a while to spot this, but you're not selecting any fields in the query so the SQL syntax here SELECT FROM {node} n is invalid.

Just select a field and you should be fine:

$query = db_select('node', 'n')
  ->fields('n', array('nid'))
  ->condition('type', 'b', '=')
  ->condition('uid', $user->uid, '=');
3
  • Thanks Clive now it finally works nice with no errors! But i must have some other mistake at my code because the query never returns empty. It always returns as if current user has already created a B node. I'll look in to it. Please give it a check also. Thanks for your time.
    – Billy
    Feb 9, 2013 at 16:35
  • Probably because you're just testing if ($query); $query will be an object either way so that statement will always return true. Try assigning the result and actually pulling a field out to determine existence, e.g. $result = $query->groupBy('ar.entity_id')->execute()->fetchField(); if ($result) { ....
    – Clive
    Feb 9, 2013 at 16:37
  • Ok everything working perfect. You were right. Thanks!
    – Billy
    Feb 9, 2013 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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