Cannot full text search child entities Symfony 4

I am trying to do a fulltext search on a Symfony/doctrine entity which is working for the fields in the entity, however my entity has a One-to-One relationship with another entity and I would like to search some of the fields in that entity too using the same search.

I have followed the full text search instructions here:

https://medium.com/@ikerib/symfony-4-and-fulltext-search-5600186c7cbf

An this has proven that itr works for the fields in the entity ApprenticUser (apprentice_user), e.g. student_number.

But when I search the entity field user.first_name (USer being the OneToOne) I get the error:

[Semantical Error] line 0, col 87 near ‘firstName) AGAINST(:searchterm’: Error: Class AppEntityApprenticeUser has no field or association named user.firstName

I have tried a.user.firstName too.

My Code:

User.php

/**
 * @ApiResource()
 * @ORMEntity(repositoryClass="AppRepositoryUserRepository")
 * @ORMTable(indexes={@ORMIndex(columns={"first_name", "last_name"}, flags={"fulltext"})})
 * @UniqueEntity(fields={"email", "username"}, message="There is already an account with this email")
 */
class User implements UserInterface, TenantAware, Serializable
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    protected $id;

    /**
     * @ORMColumn(type="string", length=45)
     */
    private $firstName;

    /**
     * @ORMColumn(type="string", length=45)
     */
    private $lastName;

ApprenticeUser.php

**
 * @ORMEntity(repositoryClass="AppRepositoryApprenticeUserRepository")
 * @ORMTable(indexes={@ORMIndex(columns={"student_number"}, flags={"fulltext"})})
 */
class ApprenticeUser
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMOneToOne(targetEntity="AppEntityUser", cascade={"persist", "remove"})
     * @ORMJoinColumn(nullable=false)
     */
    private $user;

    /**
     * @ORMColumn(type="string", length=45)
     */
    private $student_number;

ApprenticeService.php

class ApprenticeService {
    public function search($searchTerms, $pageNum) {

        /** @var QueryBuilder $queryBuilder */
        $queryBuilder = $this->entityManager->getRepository(ApprenticeUser::class)->createQueryBuilder('a');

        if ($searchTerms) {
            $queryBuilder->where('MATCH_AGAINST(a.student_number, a.user.first_name) AGAINST(:searchterm boolean) > 0')->setParameter('searchterm', $searchTerms);
        }

        $query = $queryBuilder->getQuery();

        return $this->paginator->paginate(
            $query,   /* query NOT result */
            $pageNum, /*page number*/
            $_ENV['AP_PAGINATION_ITEMS'] /*limit per page*/
        );
    }

I have several entities that interact with the User entity in a similar way, so this is a necessity for me..

Thanks in advance to anyone that hen help me fix this.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *