View Single Post
  #9  
Old 08-14-2019, 09:00 AM
 
Ed B. Ed B. is offline
 

X-Adept
  
Join Date: Apr 2016
Posts: 446
 

Default Re: How to show all products related to an entity in another model

Thank you for the reference. Unfortunately the explanation doesn't go far enough to cover my situation.



Since my code in last post
Code:
public function findBooksBy($author_id) { $author = \XLite\Core\Database::getRepo('XLite\Module\EdB\Librairie\Model\Au thor')->find($author_id); $products = $author->getProducts(); $products = \XLite\Core\Database::getRepo('\XLite\Model\Product') ->createQueryBuilder('avs')->linkInner('avs.authors','l') ->andWhere('l.author_id = :aid') ->setParameter('aid',$author_id) ->getResult(); return $products; }
does the job, one would think this can be translated easily to $cnd object
something like
Code:
prepareCndAuthoris(\Doctrine\ORM\QueryBuilder $queryBuild er, $value) { $result = $queryBuilder; $result ->linkInner('p.authors') ->andWhere('p.authors.author_id = :aid') ->setParameter('aid',$value); return $result; }
but this gives an error (p.authors has no field or association called author_id).
As a matter of fact Product->getAuthors give an array of Authors. Using
p.authors.0.author_id (obviously this would only get the first author if it worked)
doesn't work either.


I thought I could do something like
Code:
protected function getData(\XLite\Core\CommonCell $cnd, $countOnly = false) { $author_id = \XLite\Core\Request::getInstance()->author_id; $author = \XLite\Core\Database::getRepo('XLite\Module\EdB\Librairie\Mod el\Author')->find($author_id); $products = $author->getProducts(); $value=""; foreach ($products as $product) { $pid = $product->product_id; if($value == "") { $value = "p.product_id =$pid"; } else $value = " or p.product_id =$pid"; } $cnd = new \XLite\Core\CommonCell(); $cnd = Test->$value; return(\XLite\Core\Database::getRepo('\XLite\Model\Product')->search( $cnd, $countOnly); }
in the viewer class with
Code:
protected function prepareCndTest(\Doctrine\ORM\QueryBuilder $queryBuild er, $value) { $result = $queryBuilder; $result->andWhere('$value'); return $result; }
in repository class. Basically I get what I need as $value from the viewer class this way, but somehow it seems that I can't pass a string as variable in this way, and this leads to an error.


So, would there really be a "clean" way to do this by playing with $cnd ?
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote