Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Beispiel für einen Filter-Hook zum Aktualisieren von Elementen mit verwandten Feldern

Meine Lösung dafür war die Verwendung einer ZendDB-Abfrage anstelle der Get-Funktion.

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