0

I need to get data from 2 tables in Doctrine (Symfony 4).

I've create Entity and repository for tables.

Here sql code

SELECT * 
  FROM wp_terms AS t
  LEFT JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id
 WHERE tt.taxonomy = 'nav_menu';
#Repository/WpTermsRepository.php
#Simple query not working
public function findOneBySomeField($value): ?WpTerms
    {
        return $this->createQueryBuilder('w')
            ->andWhere('w.name = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
            ;
    }

returns An exception occurred in driver: SQLSTATE[HY000] [2002] ���

Develop
  • 35
  • 7
  • https://stackoverflow.com/questions/7198271/doctrine-2-1-map-entity-to-multiple-tables – Yoann MIR Oct 04 '19 at 09:49
  • 1
    Possible duplicate of [Doctrine 2.1 - Map entity to multiple tables](https://stackoverflow.com/questions/7198271/doctrine-2-1-map-entity-to-multiple-tables) – John Oct 04 '19 at 11:21

1 Answers1

0

If you have correctly mapped your entities, then each WpTermTaxonomy entity should aggregate one WpTerms entity that should be accessed via the WpTerms::getTerm() method.

Moreover your research is focused on the WpTermTaxonomy::taxonomy property, and there is already one built-in method to find one by in the doctrine EntityRepository. So within your controller try this:

/**
 * The EntityRepository::findOneBy() method return either 
 * an instance of WpTermTaxonomy or NULL.
 * @var WpTermTaxonomy|null $wpTermTaxonomy
 */
$wpTermTaxonomy = $this->get('doctrine')
    ->getManager()
    ->getRepository(WpTerms::class)
    ->findOneBy(['taxonomy'=>'nav_menu'])
;

And retrieve your term name if result is not null:

if($wpTermTaxonomy) 
{
    $term = $wpTermTaxonomy->getTerm();
    $name = $term->getName();
    echo("Term found: $name");
}
else 
{
    echo('Term not found');
}

Hope this helps