示例过滤器挂钩可使用相关字段更新项目 [英] Example filter hook to update item with related field

查看:101
本文介绍了示例过滤器挂钩可使用相关字段更新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个收藏集:客户",经销商"和订单". 客户与转销商之间存在多对一的关系. 经销商通过一对多关系与客户建立关系. 订单通过多对一关系与客户相关.

I have 3 collections: 'customers', 'resellers', and 'orders'. Customers are related to resellers by a many to one relationship. Resellers are related to customers by a one-to-many relationship. Orders relate to a customer by a many-to-one relationship.

我想使用先前配置的多对一关系,根据所选客户自动设置orders.reseller字段.

I want to automatically set the orders.reseller field based on the chosen customer using the previously configured many-to-one relationship.

$ payload对象仅包含订单"列,因此我看不到"customer.reseller",只是"customer".

The $payload object only contains 'orders' columns, so I cannot see 'customer.reseller', just 'customer'.

https://docs.directus.io/extensions/hooks.html #filter-hooks

点表示法 get()和has()方法可以使用点符号来访问子元素.例如:get('data.email').

Dot Notation get() and has() method can use dot-notation to access child elements. eg: get('data.email').

有效载荷对象 有效载荷对象是可数组的,这意味着您可以将数据作为数组$ payload ['data'] ['email]进行交互,但不能执行\ Directus \ Util \ ArrayUtils :: get($ payload,'data.email ').

Payload Object Payload object is Arrayable which means you can interact with the data as an array $payload['data']['email], but you can't do \Directus\Util\ArrayUtils::get($payload, 'data.email').

当尝试使用\Directus\Util\ArrayUtils::get()时,我没有得到任何结果,但是$payload->get()可以正常工作.在下面的示例中,我只是将返回的数组设置为订单项中的文本字段("out")以进行测试.

When trying to use \Directus\Util\ArrayUtils::get() I don't get any results returned but $payload->get() works as expected. In my example below, I am just setting the returned array to a text field ('out') in the order item for testing.

'filters' => [
                'item.create.orders:before' => function (\Directus\Hook\Payload $payload) {
                    $data = $payload->getData();
                    //$data = $payload->get('customer');
                    //$data = \Directus\Util\ArrayUtils::get($payload,'customer');
                    $out = json_encode($data);
                    $payload->set('out',$out);
                    return $payload;
                }

我希望ArrayUtils中的get函数能够使我作为数组与之交互,并根据选定的客户"从客户"表中检索经销商"列.

I expect the get function in ArrayUtils to allow me to interact with it as an array and retrieve the 'reseller' column in from the 'customers' table based on the selected 'customer'.

有效负载对象仅返回类似以下的数组:{"order_number":"1234","customer":"15"}

The Payload Object is only returning an array like: {"order_number":"1234","customer":"15"}

客户"字段应与客户"表中的"id"(行)匹配,我希望查看该字段的经销商"列值.

'customer' field should match an 'id' (row) from 'customers' table which I want to see the 'reseller' column value for.

推荐答案

为此,我的解决方案是使用ZendDB查询而不是get函数.

My solution for this was to use a ZendDB query instead of the get function.

https://docs.directus.io/api/data .html#zend-db-tablegateway

'item.create.orders:before' => function (\Directus\Hook\Payload $payload) {
                    $customer_id = $payload->get('customer'); //get customer_id from 'order' item
                    $container = \Directus\Application\Application::getInstance()->getContainer();
                    $dbConnection = $container->get('database'); // connect to database
                    $table = new \Zend\Db\TableGateway\TableGateway('customers', $dbConnection); // connect to table
                    $results = $table->select(['id' => $customer_id]); // select row with 'customer_id'
                    $customer = $results->current(); // get array of current 'customer' row
                    $reseller = $customer->reseller; // get 'reseller' column value from row
                    $payload->set('reseller',$reseller); //Update Payload
                    return $payload;
                }

这篇关于示例过滤器挂钩可使用相关字段更新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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