EntityMetadataWrapperException:字段的未知数据属性 [英] EntityMetadataWrapperException: unknown data property for field

查看:128
本文介绍了EntityMetadataWrapperException:字段的未知数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在尝试更新我的代码,使用实体包装器访问字段值。现在我有这个:

  $ wrapper = entity_metadata_wrapper(node,$ nid); 
print($ wrapper-> field_property_sample() - > value());

而不是这样:

 打印($节点 - > field_property_sample [LANGUAGE_NONE] [0] [ 值]); 

有时我遇到这个问题:



< blockquote>

EntityMetadataWrapperException:未知数据属性field_property_sample。


有没有办法解决这个问题?



我有大约10个这样的字段可以抛出这个异常,它真的变得丑陋了

  $ wrapper = entity_metadata_wrapper(node,$ nid); 

try {
print($ wrapper-> field_property_sample() - > value());
} catch(EntityMetadataWrapperException& e){
print();
}

/ **重复10次** /

有没有一些函数可以或多或少这样调用?

  $ wrapper = entity_metadata_wrapper(node,$ nid ); 
print($ wrapper-> field_property_sample-> exists()?$ wrapper-> field_property_sample-> value():);

/ **重复10次** /


解决方案

是的,你可以使用PHP语言的现有功能

  try {
print( $ wrapper-> field_property_sample->值());
}
catch(EntityMetadataWrapperException $ e){
//恢复
}

或者,由于 EntityMetadataWrapper 实现 __ isset()可以使用:

  print isset($ wrapper-> field_property_sample)? $ wrapper-> field_property_sample-> value():''; 


I have recently been trying to update my code to use entity wrappers to access field values. Now I have this:

$wrapper = entity_metadata_wrapper("node", $nid);
print($wrapper->field_property_sample()->value());

instead of this:

print($node->field_property_sample[LANGUAGE_NONE][0]["value"]);

The problem is sometimes I encounter this:

EntityMetadataWrapperException: unknown data property field_property_sample.

Is there a way for me to workaround this?

I have about 10 of these fields that can throw this exception and it is really getting ugly

$wrapper = entity_metadata_wrapper("node", $nid);

try {
  print($wrapper->field_property_sample()->value());
} catch (EntityMetadataWrapperException &e){
  print("");
}

/** repeat 10 times **/

Is there some function that I can more or less call like this?

$wrapper = entity_metadata_wrapper("node", $nid);
print($wrapper->field_property_sample->exists() ? $wrapper->field_property_sample->value()  : "" );

/** repeat 10 times **/

解决方案

Yep, you can just use existing features of the PHP language

try {
  print($wrapper->field_property_sample->value());
}
catch (EntityMetadataWrapperException $e) {
  // Recover
}

Or, since EntityMetadataWrapper implements __isset() you can use that:

print isset($wrapper->field_property_sample) ? $wrapper->field_property_sample->value() : '';

这篇关于EntityMetadataWrapperException:字段的未知数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆