Doctrine querybuilder多个在哪里

时间:2017-04-27 15:25:16

标签: php api doctrine-orm

我遇到一个问题,我遇到了麻烦。

我正在构建一个API,您可以使用PHP slim来使用不同的值过滤请求。

$app->get('/stats/getCrash/:dqaid(/:portal', function ($dqaid, $portal = null) use ($app) {
    $app->response->setStatus(200);
    $app->response->setBody(json_encode(APIResult::test($dqaid, $portal)));
    $app->response->headers->set('Content-Type', 'application/json');
});

我正在尝试进行查询的功能:

     /**
     * @param $filters CrashFilters
     */
    public static function test($dqaid, $filters){
        $em = DBConnection::getEntityManager();

        $whereClause = CrashFilters::getWhereClause($dqaid, $filters);

        $response = $em->createQuery("select c from crashes c $whereClause")->getResult();

    print_r($response);
}

我的课程,我在哪里存储过滤器,然后创建关闭的地方。

class CrashFilters
{
    private $app_id = "null";
    private $reason = "null";
    private $app_version_id = "null";
    private $reash_hash = "null";
    private $brand = "null";
    private $model = "null";
    private $os_version = "null";
    private $portal = "null";
    private $datecreated = "null";

/**
     * @param $filters CrashFilters
     * @return string
     */
    public static function getWhereClause($dqaid, $filters){
        $result = ' WHERE dqaid='."'".$dqaid."'";
        foreach ($filters as $key => $value) {
            if ($value != "null") {
                $result .= ' AND ';
                $result .= "$key='" . $value . "' ";
            }
        }
        return $result;
    }

我希望有一个查询按用户设置的参数执行select和filter。

我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

好吧,我找到了解决方案。

我将CrashFilters转换为数组(使用encode)并使用了findBy:

$crashes = $em->getRepository('Entities\Crash')
            ->findBy(array_filter($request->encode), null, self::LIMIT_RESULT);

array_filter以防止空值。

我的函数编码:

public function encode(){
        return [
            'dqaid' => $this->getDqaid(),
            'portal' => $this->getPortal(),
            'app_id' => $this->getAppId(),
            'app_version_id' => $this->getAppVersionId(),
            'reason' => $this->getReason(),
            'reason_hash' => $this->getReasonHash(),
            'brand' => $this->getBrand(),
            'model' => $this->getModel(),
            'os_version' => $this->getOsVersion(),
            'datecreated' => $this->getDatecreated()
        ];
    }

现在有效。