Creative crypter
Creative crypter

Reputation: 1496

Doctrine2 - join on non related table

I have some some tables which are not related and I try to join tables in my query:

$builder = $this->em()->createQueryBuilder();

$builder->select('main')
            ->from($this->getEntityName(), 'main')
            ->leftJoin('\Bundle\Path\To\Article', 'a', 'WITH', 'a.id = main.articleID');

$query = $builder->getQuery();

$query->setHydrationMode(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);

$paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);

return [
    'success' => true,
    'total'   => $paginator->count(),
    'data'    => $paginator->getIterator()->getArrayCopy()
];

and I get this message:

Cannot count query which selects two FROM components, cannot make distinction

Anybody could help me with this issue ?

Upvotes: 1

Views: 326

Answers (1)

mfisher91
mfisher91

Reputation: 807

There's two options.

Option 1

SELECT t1.name, t2.date FROM table1 t1, table2 t2;

Obviously this would need modifying to meet your SQL statement/code structure, but I hope you get the point.

Option 2

SELECT t1.name, t2.date FROM table1 t1 CROSS JOIN table2 t2;

Hope this helps :). However, in the future can I suggest googling first as this is a popular question.

Upvotes: 1

Related Questions