Symfony 4:数据表搜索过滤器问题

时间:2019-06-28 12:23:08

标签: symfony doctrine-orm symfony4

我想用symfony 4创建一个票务管理系统,所以我使用 Mapsred/TicketingBundle捆绑包。

此捆绑包用于查看和搜索票证,该票证来自datatables.net。我在票证搜索过滤器上坚持了学说。

我希望能够在此表中搜索故障单作者的客户名称

UserEntity:

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\Table(name="app_user")
 */
class User implements UserInterface
{
    /**
     * @ORM\Column(type="string", length=180, unique=true)
     * @AssertPerso\Email
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=255, nullable=false)
     */
    private $username;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Formation")
     * @ORM\JoinColumn(nullable=false)
     */
    private $formation;

FormationEntity:

/**
 * @ORM\Entity(repositoryClass="App\Repository\FormationRepository")
 */
class Formation
{
    /**
     * @ORM\Column(type="array", nullable=true)
     */
    private $formations = [];

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     */
    private $customerName;

我希望能够在搜索表中执行“ customerName”列

我被困在捆绑软件的“ TicketRepository”文件中。

TicketRepository:

/** @var array $joins */
private $joins;

/**
 * @param string|null $globalSearch
 * @param array $columns
 * @param array $fields
 * @param array $sortOrder
 * @param int $offset
 * @param int $limit
 * @param bool $isCount
 * @param string $status
 * @param string $type
 * @param UserInterface|null $user
 * @return array|int
 * @throws \Doctrine\ORM\NonUniqueResultException
 */
public function searchDataTable(string $globalSearch = null, array $columns, array $fields, array $sortOrder, int $offset,
                                int $limit, bool $isCount, $status, $type, UserInterface $user = null)
{
    $this->joins = ['q.status' => 'status'];
    $parameters = ['_status' => $status];
    $qb = $this->createQueryBuilder('q')
        ->where('status.name = :_status');

    $sort = key($sortOrder);
    $order = $sortOrder[$sort];
    $qb->orderBy($this->getX($sort), $order);

    if ($type === 'perso') {
        $qb->andWhere('q.author = :_author');

        $parameters['_author'] = $user;

    }

    if ($type === 'list_public') {
        $qb->andWhere($qb->expr()->orX('q.author = :_author', 'q.public = :_public'));

        $parameters['_author'] = $user;
        $parameters['_public'] = true;
    }

    if (null !== $globalSearch) {
        $qbWhere = [];
        $parameters['globalSearch'] = '%' . $globalSearch . '%';

        foreach ($fields as $field) {
            $qbWhere[] = $qb->expr()->like($this->getX($field), ':globalSearch');
        }

        $qb->andWhere(call_user_func_array([$qb->expr(), 'orX'], $qbWhere));
    } elseif (!empty($columns)) {
        $qbWhere = [];
        foreach ($columns as $field => $value) {
            $qbWhere[] = $qb->expr()->like($this->getX($field), ':' . $field);
            $parameters[$field] = '%' . $value . '%';
        }

        $qb->andWhere(call_user_func_array([$qb->expr(), 'andX'], $qbWhere));
    }

    foreach ($this->joins as $join => $alias) {
        $qb->leftJoin($join, $alias);
    }

    $qb->setParameters($parameters);

    if ($isCount) {
        return $qb->select('COUNT(q.id)')->getQuery()->getSingleScalarResult();
    }

    dump($qb);
    return $qb->setFirstResult($offset)->setMaxResults($limit)->getQuery()->getResult();
}

/**
 * @param string $field
 * @return string
 */
private function getX(string $field)
{
    if ($field === 'author') {
        $this->joins['q.author'] = 'author';

        return 'author.username';
    } elseif ($field === 'customername') {
        $this->joins['q.author'] = 'author';

        return 'author.formation';
    } elseif ($field === 'category') {
        $this->joins['q.category'] = 'category';

        return 'category.name';
    } elseif ($field === 'status') {
        $this->joins['q.status'] = 'status';

        return 'status.value';
    } elseif ($field === 'priority') {
        $this->joins['q.priority'] = 'priority';

        return 'priority.value';
    } elseif ($field === 'assignated') {
        $this->joins['q.assignated'] = 'assignated';

        return 'assignated.username';
    }

    return 'q.' . Inflector::camelize($field);
}

当我在CustomerName字段中搜索时,我得到:

[Semantical Error] line 0, col 126 near 'formation LIKE': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

有人可以帮我吗?我对Doctrine不太了解,对我来说这很新

1 个答案:

答案 0 :(得分:0)

查询似乎不正确,应该在完整的堆栈跟踪中找到完整的查询,然后尝试在数据库中运行,在具有SQL验证功能的IDE中检查有效性

要查看查询,请在以下位置附加断点:

\Doctrine\DBAL\Connection::executeQuery

您的查询应该在$query变量中