因此,我正在创建一个与PSA集成并从平台接收数据的软件,该平台用于对报价等进行数字标牌处理。

由于计划将其与当前正在构建的PSA集成在一起,因此我们将所有这些记录在laravel应用程序中,并将通过它的数据存储到当前的PSA中。

我创建了一个特征,该特征首先在我们的数据库中执行,然后在PSA中的实体记录中执行。最初创建或查找时,一切工作都很好,但是一旦找到它并将其持久保存到我们的数据库中,我就遇到了查询生成器忽略值的问题,我将该值传递给了它,而是执行以下查询。

SELECT * FROM `accounts` WHERE `AccountName` = ?


下面是执行查找功能的代码:

 /**
     * Performs a double lookup, first in the local DB and then against Autotask
     * if nothing is returned from autotask or DB then null will be returned
     *
     * @param string $entityType The Entity type used in queries in Autotask (eg. Ticket)
     * @param string $field The Column name to run the query against (eg. EMailAddress)
     * @param string $value The value that will be looked up (eg. [email protected])
     *
     * @return self|null
     */
    public static function doubleLookup(/*string $entityType, */string $field, string $value, string $op = 'Equals')
    {
        preg_match('/([^\\\]+$)/', self::class, $output_array);
        $entityType = $output_array[0];
        $result = self::where($field, $op, $value)->get(); // ngl this is fucking cool. I'm very pleased with this tbh
        // dd(self::where($field, $op, "Example Name")->toSql()); // this is what dumps the SQL. Tested with var which is never null and with static value to same result.

        if ($result->count()) {
            return $result->first();
        }

        // If we've gotten to this point we can assume that it doesn't exist in our database so we
        // we should try and find a reference in Autotask
        $client = self::client();

        // build out our query using our supplied variables.
        $query = new Query($entityType);
        $queryField = new QueryField($field);
        $queryField->addExpression($op, $value);
        $query->addField($queryField);

        // Because Autotask is cancer we need to wrap this in a try catch.
        // Integrator account often locks by itself and idk why'
        // This prevents us getting loads of low level 500 Errors that are imposible to debug
        try {
            $result = $client->query($query)->queryResult->EntityResults->Entity;
        } catch (\Throwable $th) {
            return dd($th);
        }

        // if $result->EntityResults->Entity != truthy (i.e null or false)
        if (!$result)
            return null;

        // because autotask is just straight up cancer it can return either a single entity or an array.
        // the array for some unknown reason can be empty which is just seriously dumb.
        if (!is_array($result)) {
            // we have matched a single result in autotask. This is honestly the best case scenario.
            $entity = new self(Arr::except((array) $result, ['UserDefinedFields']));
            $entity->AutotaskID = $result->id;
        } else {
            // we have matched an array of entities in autotask. This is honestly the best case scenario.
            $entity = new self(Arr::except((array) $result[0], ['UserDefinedFields']));
            $entity->AutotaskID = $result[0]->id;
        }

        $entity->save();

        return $entity;
    }


该函数由控制器中的上下文方法调用

    public function quotient(Request $request)
    {
        $bag = json_decode($request->getContent(), true);
        $account = Account::doubleLookup('AccountName', $bag['quote_for']['company_name']);
        ...


任何帮助将不胜感激,因为我抓挠头,到目前为止仍无法找到答案。

提前致谢。

最佳答案

原来我使用的是在Autotask函数中使用的运算符,此外在使用toSql时还使用了?符号作为占位符。

关于php - 为什么QueryBuilder忽略我的输入参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58062867/

10-10 10:22