Example filter hook to update item with related field

元气小坏坏 提交于 2020-07-19 09:35:28

问题


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.

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

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

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

Dot Notation get() and has() method can use dot-notation to access child elements. eg: get('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').

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;
                }

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'.

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

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


回答1:


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;
                }


来源:https://stackoverflow.com/questions/55347047/example-filter-hook-to-update-item-with-related-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!