Home > Blockchain >  QueryBuilder Invalid PathExpression. Must be a StateFieldPathExpression
QueryBuilder Invalid PathExpression. Must be a StateFieldPathExpression

Time:10-19

Trying to fetch all supplierUsers where the id is DISTINCT and hydrate the results.

The SQL query to get all the results would be the following

SELECT DISTINCT supplier_user_id FROM job_item_quote

The Query builder to fetch the above.

$qb = $this->createQueryBuilder('a')
        ->select('a.supplierUser')
        ->distinct(true);
$result = $qb->getQuery()->getResult();

Outputted getQuery(). Which is exactly what I'm looking for.

SELECT DISTINCT a.supplierUser FROM Project\Entities\JobItemQuote a

The error thrown when trying to fetch distinct users

[Semantical Error] line 0, col 18 near 'supplierUser,': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

I've tried adding joins in for supplierUser in hopes it would fix. Same error thrown.

JobItemQuote Entity

/**
 * @ORM\Entity(repositoryClass="Project\Repositories\JobItemQuote\JobItemQuoteRepository")
 * @ORM\Table(name="job_item_quote")
 */
class JobItemQuote extends BaseEntity
{
    public static $joins = [
        'supplierUser' => SupplierUser::class,
        'jobItem' => JobItem::class
    ];
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @var int
     */
    protected $id; // thekey

    /**
     * @ORM\ManyToOne(targetEntity="JobItem", inversedBy="quotes")
     * @var JobItem
     */
    protected $jobItem;

    /**
     * @ORM\ManyToOne(targetEntity="SupplierUser")
     * @var SupplierUser
     */
    protected $supplierUser;

    
    ....

}

SupplierUser Entity

/**
 * @ORM\Entity(repositoryClass="Project\Repositories\SupplierUser\SupplierUserRepository")
 * @ORM\Table(name="supplier_user")
 */


class SupplierUser extends User {

    public static $joins = [
        'supplier' => Supplier::class,
        'supplierGroup' => SupplierGroup::class
    ];

    /**
     * @ORM\OneToOne(targetEntity="Supplier", inversedBy="supplierUser", cascade={"persist"})
     * @var Supplier
     */
    protected $supplier;

    /**
     * @ORM\ManyToOne(targetEntity="SupplierGroup")
     * @var Group
     */
    protected $supplierGroup;

    ....
}

CodePudding user response:

Your need is to retrieve the list of supplierUsers associated with JobItemQuote, so you should make the query in JobItemQuoteRepository making a join with supplierUsers, you find bellow the example :

$qb = $this->createQueryBuilder('jiq')
          ->select('su')
          ->join(SupplierUser::class, 'su', Join::With, 'su.id = jiq.supplierUser')
          ->distinct(true)
      ;

$result = $qb->getQuery()->getResult();

By this query you will have the list of SupplierUser (distinctly) assoiated to JobsItemQuote.

  • Related