【问题标题】:Accessing the array from PHP Client library for Exact Online为 Exact Online 从 PHP 客户端库访问数组
【发布时间】:2019-04-20 12:22:55
【问题描述】:

为了将客户下的订单与来自 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

$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();

这是用Email 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
 (
 )

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

同样,通过$getCurrentDivision-&gt;findWithSelect('CurrentDivision');$getDivision-&gt;CurrentDivision; 等调用可以轻松地从库中检索CurrentDivision

为什么在检索数组后,同一个函数不能工作?

【问题讨论】:

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


    【解决方案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");
    

    【讨论】:

    • 是否可以编写我们自定义的内置函数。请参阅我对发布的问题所做的编辑。为什么上面的函数调用不起作用?从那以后,当我要调用这些内置函数时,我常常得到空数组
    • 抱歉,我对WooCommerce 或那个PHP 库Exact Online 一无所知。我只是建议您如何访问对象的私有和受保护属性。
    【解决方案2】:

    为了从数组中访问受保护的属性,您可以在下面的 sn-ps 中找到

    $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');
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 2019-05-19
      相关资源
      最近更新 更多