1

I had a field using entity reference in my site. I'm trying to develop a module can read/use it.

$wrapper = entity_metadata_wrapper('node',$node);
dpm($wrapper->myRefField->getPropertyInfo());

This returns a error page, but dpm($wrapper->myRefField) shows that it's a standard EntityDrupalWrapper, though nothing in it.

How can I read $nid of the referenced node?

Errors below:

 Additional uncaught exception thrown while handling exception.

Original

PDOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "node" does not exist LINE 3: node base ^: SELECT revision.vid AS vid, base.uid AS uid, revision.title AS title, revision.log AS log, revision.status AS status, revision.comment AS comment, revision.promote AS promote, revision.sticky AS sticky, base.nid AS nid, base.type AS type, base.language AS language, base.created AS created, base.changed AS changed, base.tnid AS tnid, base.translate AS translate, revision.timestamp AS revision_timestamp, revision.uid AS revision_uid FROM {node} base INNER JOIN {node_revision} revision ON revision.vid = base.vid WHERE (base.nid IN (:db_condition_placeholder_0)) ; Array ( [:db_condition_placeholder_0] => 1 ) in DrupalDefaultEntityController->load() (line 191 of /var/www/site2/includes/entity.inc).

Additional

PDOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "filter_format" does not exist LINE 3: filter_format ff ^: SELECT ff.* FROM {filter_format} ff WHERE (status = :db_condition_placeholder_0) ORDER BY weight ASC; Array ( [:db_condition_placeholder_0] => 1 ) in filter_formats() (line 427 of /var/www/site2/modules/filter/filter.module).

Uncaught exception thrown in shutdown function.

PDOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "semaphore" does not exist LINE 1: DELETE FROM semaphore ^: DELETE FROM {semaphore} WHERE (value = :db_condition_placeholder_0) ; Array ( [:db_condition_placeholder_0] => 18701629495353e89467a483.34692095 ) in lock_release_all() (line 269 of /var/www/site2/includes/lock.inc).

Uncaught exception thrown in session handler.

PDOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "sessions" does not exist LINE 3: sessions sessions ^: SELECT 1 AS expression FROM {sessions} sessions WHERE ( (sid = :db_condition_placeholder_0) AND (ssid = :db_condition_placeholder_1) ); Array ( [:db_condition_placeholder_0] => sLS3fA1YAd1u1G6_xC1yxg0Ba6Kgrus6oXDBm4Wpuww [:db_condition_placeholder_1] => ) in _drupal_session_write() (line 209 of /var/www/site2/includes/session.inc).
4
  • Welcome to Drupal Answers! Are you sure $node is a valid node object?
    – apaderno
    Apr 20, 2014 at 14:16
  • Hi,yes,it's my node var in fact,write it like this so it's easier to read. The problem is that myRefField here is a entity reference, but i can't read anything from it via getPropertyInfo().
    – zhuchun
    Apr 20, 2014 at 15:30
  • If the field has multiple cardinality try $wrapper->myRefField[0]->getPropertyInfo()
    – Clive
    Apr 20, 2014 at 16:44
  • Thanks, but with no luck. Devel print it as an EntityDrupalWrapper, not an array
    – zhuchun
    Apr 21, 2014 at 6:51

1 Answer 1

3
<?php

/**
 * We define a new function in our MODULE and will simply use it where needed.
 *
 */

/**
 * @param (object) $node
 * @param (string) $field_name
 * @return false|null|integer
 *    The target id. FALSE on error. NULL when no reference found.
 */
function _MODULE_get_node_reference($node, $field_name) {
    $target_id = null;
    if (!isset($node->type)) {
        return false;
    }
    // In case of any exception we simply return false.
    try {
        $entity_wrapper = entity_metadata_wrapper('node', $node);
        if (isset($entity_wrapper->{$field_name})) {
            $target_id = $entity_wrapper->{$field_name}->value();
        }
        // maybe we are in the middle of a DB transaction. In that case entity_metadata_wrapper() would not work
        if (empty($target_id)) {
            // So, we try using Fields API. This always works.
            $items = field_get_items('node', $node, $field_name);
            if (isset($items[0])) {
                $target_id = $items[0]['target_id'];
            }
        }
    } catch (Exception $e) {
        return false;
    }
    return $target_id;
}

?>
1
  • 1
    Thanks guys! It's my bad, my incomplete codes led us to a wrong way. This answer remind me ->value() has some DB transaction inside, and I used db_set_active($OtherDB) before it! My apology, love you all :)
    – zhuchun
    Apr 21, 2014 at 7:28

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.