I've been trying to load a node from the database which I have the nid for but I keep getting an EntityMalformedException
. This is the error I get:
EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7947 of C:\Apache24\htdocs\devrevolution\includes\common.inc).
I'm using Drupal 7.67, MySQL 5.7, and Apache 2 on Windows 10. node_load()
is used hundreds of times in this project with no problems, but somehow in this function I can't get it to properly return a node without throwing this exception. I'm aware that EntityFieldQuery
is the preferred way to retrieve a node from the database in Drupal 7, but when I tried the following, it retrieved nothing from the database.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'study')
->fieldCondition('field_sname', 'value', $study_name, '=')
->addMetaData('account', user_load(1))
->execute();
The field exists and is populated in the field_sname
table with the nid attached but it doesn't retrieve the node for some reason.
I have checked to make sure the node actually exists in the node table, and then used the following to retrieve the nid from the database. I've made sure the node has a revision id as well, as that was causing problems for others. This code works to get the correct nid, but when it gets to node_load($nid)
it throws the exception. I know that node_load
is causing the error because without the line, there are no errors.
function get_study_information($study_name)
{
// Retrieve the node nid with the study name from the database
$query = db_query(
'SELECT n.nid AS nid '.
'FROM {node} n '.
'WHERE title = :studyname AND type = :study',
array(':studyname' => $study_name,
':study' => "study"));
$nid = $query->fetchAssoc()['nid'];
$node = node_load($nid);
return $node;
}