从PHP客户端库访问阵列以进行精确在线

时间:2019-04-20 12:22:56

标签: php exact-online picqer-exact-php-client

为了将客户提供的带有某些信息的订单从WooCommerce REST API通过REST API存储到Exact Online仪表板。我用过PHP Client library for Exact Online

有一个问题-将数据存储到Exact Online之后,无法访问数组中的特定字段。具体来说,当使用

进行REST API调用时
 /api/v1/{division}/bulk/CRM/Accounts?
 $filter=ID eq Email eq'nishanth@gmail.com'&$select=ID,Email

OR

$url = "https://start.exactonline.de/api/v1/" + mandant.number + "/crm/Accounts?
$filter=Email eq '" + orderitem.email + "'&$select=ID,Code";

在这里,您将获得以下代码

$customer = [
        'address'       => 'No.22/N, 91 Cross, XYZ Street, ABC Road',
        'address2'      => 'DEF',
        'city'          => 'GHI',
        'customerid'    => '999',
        'country'       => 'DE',
        'name'          => 'Nishanth',
        'zipcode'       => '123456'
];


// Create a new account

$account->AddressLine1 = $customer['address'];
$account->AddressLine2 = $customer['address2'];
$account->City = $customer['city'];
$account->Code = $customer['customerid'];
$account->Country = $customer['country'];
$account->IsSales = 'true';
$account->Name = $customer['name'];
$account->Postcode = $customer['zipcode'];
$account->Email = 'nishanth@gmail.com';
$account->Status = 'C';
$account->save();

这是使用电子邮件ID过滤后获得的结果

echo '<pre>'; print_r($account->filter("Email eq 'nishanth@gmail.com'"));


[attributes:protected] => Array
 (
    [Accountant] => 
    [AddressLine1] => No.22/N, 91 Cross, XYZ Street, ABC Road
    [AddressLine2] => DEF
    [City] => GHI
    [Code] => 1000
    [ConsolidationScenario] => 6
    [Country] => DE 
    [CountryName] => Germany
    [Created] => /Date(1555764341137)/
    [Creator] => 5bbfbd93-52f1-4f4b-b34e-fd213e479f8e
    [CreatorFullName] => Nishanth
    [Division] => 54810
    [Email] => nishanth@gmail.com
    [ID] => bb124287-647c-4267-bd60-004efa1302aa
    [LogoThumbnailUrl] => https://start.exactonline.de//docs/images/placeholder_account_myeol.png
    [LogoUrl] => https://start.exactonline.de//docs/images/placeholder_account_myeol.png
    [Modified] => /Date(1555764341137)/
    [Modifier] => 5bbfbd93-52f1-4f4b-b34e-fd213e479f8e
    [ModifierFullName] => Nishanth
    [Name] => Nishanth Jay
    [Postcode] => 123456
 )

使用以下内置函数,例如,

public function find()
{
    $result = $this->connection()->get($this->url);
    return new self($this->connection(), $result);
}
public function findWithSelect($select = '')
{
    $result = $this->connection()->get($this->url, [
        '$select' => $select
    ]);

    return new self($this->connection(), $result);
}

使用以下内置函数进行调用:

$Accounts->filter("Email eq 'nishanth@gmail.com'")[0]->findWithSelect('ID');

$checkAccount = $Accounts->filter("Email eq 'nishanth@gmail.com'");
$checkAccount[0]->find('ID');
$checkAccount[0]->findWithSelect('Code'); 

以上内置库产生了

[attributes:protected] => Array
 (
 )

我应该如何从数组访问特定属性?

同样,通过诸如CurrentDivision$getCurrentDivision->findWithSelect('CurrentDivision');之类的调用,可以轻松地从库中检索$getDivision->CurrentDivision;

为什么在检索数组后不能使用同一功能?

2 个答案:

答案 0 :(得分:1)

您可以使用closure bind方法来访问对象的私有或受保护的属性。这是静态函数示例。

class Helper {

    /**
    * Helper method to access private
    * or protected properties of an object
    * using the closure bind method
    */
    public static function accessPrivate($object, $property) {
        $bind = Closure::bind(
            function($prop) {
                return $this->$prop;
            },
            $object,
            $object
        );
        return $bind($property);
    }
}

用法:

$code = Helper::accessPrivate($checkAccount, "Code");

答案 1 :(得分:0)

为了从数组访问受保护的属性,您将在下面的代码片段中找到

$checkAccount = $Accounts->filter("Email eq 'nishanth@gmail.com'");
$getAccountId = $checkAccount[0]->ID;
$getAccountCode = $checkAccount[0]->Code;

自从我尝试使用findWithSelect()find()以来:

$Accounts->filter("Email eq 'nishanth@gmail.com'")[0]->findWithSelect('ID');

但是没有产生任何值会导致空白值