通过 PHP 使用具有多个同名键字段的 AX Web 服务

Consuming AX webservice through PHP with multiple key fields with the same name

提问人:PhDJ 提问时间:11/12/2015 最后编辑:Jan B. KjeldsenPhDJ 更新时间:11/13/2015 访问量:288

问:

我正在尝试通过 PHP 在 AX 2009 中使用 Web 服务,之前已经这样做过,没有任何问题,这次我需要生成一个具有多个同名键的 XML,对于更新部分,我找到了一个有效的解决方案,但我需要在更新之前选择记录,但它失败了。

我收到此错误消息:“客户端 SOAP-ERROR:编码:对象没有”DataSourceName“属性”

这是请求的 XML:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <xsd:schema targetNamespace="http://schemas.microsoft.com/dynamics/2006/02/documents/QueryCriteria" xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/QueryCriteria" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xsd:element name="QueryCriteria" type="QueryCriteria" /> 
- <xsd:complexType name="QueryCriteria">
- <xsd:sequence minOccurs="1" maxOccurs="unbounded">
  <xsd:element name="CriteriaElement" type="CriteriaElement" /> 
  </xsd:sequence>
  </xsd:complexType>
- <xsd:complexType name="CriteriaElement">
- <xsd:sequence>
  <xsd:element name="DataSourceName" type="xsd:string" /> 
  <xsd:element name="FieldName" type="xsd:string" /> 
  <xsd:element name="Operator" type="Operator" /> 
  <xsd:element name="Value1" type="xsd:string" /> 
  <xsd:element name="Value2" type="xsd:string" minOccurs="0" /> 
  </xsd:sequence>
  </xsd:complexType>
- <xsd:simpleType name="Operator">
- <xsd:restriction base="xsd:string">
  <xsd:enumeration value="Equal" /> 
  <xsd:enumeration value="NotEqual" /> 
  <xsd:enumeration value="Greater" /> 
  <xsd:enumeration value="GreaterOrEqual" /> 
  <xsd:enumeration value="Less" /> 
  <xsd:enumeration value="LessOrEqual" /> 
  <xsd:enumeration value="Range" /> 
  </xsd:restriction>
  </xsd:simpleType>
  </xsd:schema>

这是我尝试使用它的PHP代码:

  $CriteriaElement1 = new stdClass;
  $CriteriaElement1->DataSourceName = 'CustLoadListTrans';
  $CriteriaElement1->FieldName      = 'RefRecIdTrans';
  $CriteriaElement1->Operator       = 'Equal';
  $CriteriaElement1->Value1         = $refRecIdTrans;

  $CriteriaElement2 = new stdClass;
  $CriteriaElement2->DataSourceName = 'CustLoadListTrans';
  $CriteriaElement2->FieldName      = 'RefRecIdJour';
  $CriteriaElement2->Operator       = 'Equal';
  $CriteriaElement2->Value1         = $refRecIdJour;

  $CriteriaElement3 = new stdClass;
  $CriteriaElement3->DataSourceName = 'CustLoadListTrans';
  $CriteriaElement3->FieldName      = 'CustLoadListId';
  $CriteriaElement3->Operator       = 'Equal';
  $CriteriaElement3->Value1         = $custLoadListId;

  $QueryCriteria = new stdClass;

  $QueryCriteria->QueryCriteria->CriteriaElement[] = $CriteriaElement1;
  $QueryCriteria->QueryCriteria->CriteriaElement[] = $CriteriaElement2;
  $QueryCriteria->QueryCriteria->CriteriaElement[] = $CriteriaElement3;

如果我用这行替换最后三行:

  $QueryCriteria->QueryCriteria->CriteriaElement = $CriteriaElement1;

它有效,但记录仅由一个键字段选择。

我正在使用 PHP 版本 5.1.6

PHP axapta x++ 动态-AX-2009

评论

0赞 kingofzeal 11/12/2015
错误是由 PHP 脚本还是由 AX 引发的?
0赞 Jan B. Kjeldsen 11/13/2015
您能否分享生成的 XML 查询?
0赞 PhDJ 11/13/2015
你能帮我从哪里得到那个 Jan 吗?
0赞 Maxim Lazarev 11/13/2015
似乎您不能将数组设置为 CriteriaElement,它需要一个类。看看 nodkz 在这里的第一条评论,也许它会给你一个提示。
0赞 Maxim Lazarev 11/13/2015
另一个建议是将 CriteriaElement 对象括在 SoapVar 类中,如下所示: 然后将其设置为$CriteriaElement = new SoapVar(array($CriteriaElement1, $CriteriaElement2))CriteriaElement $QueryCriteria->QueryCriteria->CriteriaElement = $CriteriaElement;

答:

3赞 Maxim Lazarev 11/13/2015 #1

这将解决问题:

$nameSpace = 'http://schemas.microsoft.com/dynamics/2006/02/documents/QueryCriteria'; 

$CriteriaElement1 = array(); 
$CriteriaElement1[] = new SoapVar('CustLoadListTrans', XSD_STRING, null, $nameSpace, 'DataSourceName', $nameSpace); 
$CriteriaElement1[] = new SoapVar('RefRecIdTrans', XSD_STRING, null, $nameSpace, 'FieldName', $nameSpace); 
$CriteriaElement1[] = new SoapVar('Equal', XSD_STRING, null, $nameSpace, 'Operator', $nameSpace); 
$CriteriaElement1[] = new SoapVar($refRecIdTrans, XSD_STRING, null, $nameSpace, 'Value1', $nameSpace); 
$CriteriaElement1[] = new SoapVar('', XSD_STRING, null, $nameSpace, 'Value2', $nameSpace); 
$CriteriaElementOne = new SoapVar($CriteriaElement1, SOAP_ENC_OBJECT, null, $nameSpace, 'CriteriaElement', $nameSpace ); 

$CriteriaElement2 = array(); 
$CriteriaElement2[] = new SoapVar('CustLoadListTrans', XSD_STRING, null, $nameSpace, 'DataSourceName', $nameSpace); 
$CriteriaElement2[] = new SoapVar('RefRecIdJour', XSD_STRING, null, $nameSpace, 'FieldName', $nameSpace); 
$CriteriaElement2[] = new SoapVar('Equal', XSD_STRING, null, $nameSpace, 'Operator', $nameSpace); 
$CriteriaElement2[] = new SoapVar($refRecIdJour, XSD_STRING, null, $nameSpace, 'Value1', $nameSpace); 
$CriteriaElement2[] = new SoapVar('', XSD_STRING, null, $nameSpace, 'Value2', $nameSpace); 
$CriteriaElementTwo = new SoapVar($CriteriaElement2, SOAP_ENC_OBJECT, null, $nameSpace, 'CriteriaElement', $nameSpace ); 

$CriteriaElement3 = array(); 
$CriteriaElement3[] = new SoapVar('CustLoadListTrans', XSD_STRING, null, $nameSpace, 'DataSourceName', $nameSpace); 
$CriteriaElement3[] = new SoapVar('CustLoadListId', XSD_STRING, null, $nameSpace, 'FieldName', $nameSpace); 
$CriteriaElement3[] = new SoapVar('Equal', XSD_STRING, null, $nameSpace, 'Operator', $nameSpace); 
$CriteriaElement3[] = new SoapVar($custLoadListId, XSD_STRING, null, $nameSpace, 'Value1', $nameSpace); 
$CriteriaElement3[] = new SoapVar('', XSD_STRING, null, $nameSpace, 'Value2', $nameSpace); 
$CriteriaElementThree = new SoapVar($CriteriaElement3, SOAP_ENC_OBJECT, null, $nameSpace, 'CriteriaElement', $nameSpace ); 

$parm = array(); 
$parm[] = new SoapVar($CriteriaElementOne, SOAP_ENC_OBJECT, null, $nameSpace, 'CriteriaElement', $nameSpace); 
$parm[] = new SoapVar($CriteriaElementTwo, SOAP_ENC_OBJECT, null, $nameSpace, 'CriteriaElement', $nameSpace); 
$parm[] = new SoapVar($CriteriaElementThree, SOAP_ENC_OBJECT, null, $nameSpace, 'CriteriaElement', $nameSpace); 
$CriteriaElement = new SoapVar($parm, SOAP_ENC_OBJECT, null, $nameSpace, 'CriteriaElement', $nameSpace ); 

$QueryCriteria->QueryCriteria = $CriteriaElement; 


$result = $client->find($QueryCriteria);

评论

1赞 PhDJ 11/13/2015
非常感谢马克西姆的所有帮助!