View Single Post
  #5  
Old 08-12-2019, 11:08 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 very much, and sorry for not understanding all the subtilities of object-oriented PHP, but I am a little bit confused.


Is your code meant to be used "as is" with some "obvious" modification, or is it some sort of "meta code" where I am supposed to write some query in place of

find($data-author_id)? (So probably we need findBy instead of find?) And, should the return of getData $data, $books, or something else?


If I write
Code:
protected function getData(\XLite\Core\CommonCell $cnd, $countOnly = false) { $data = parent::getData($cnd, $countOnly); $author_id = \XLite\Core\Request::getInstance()->author_id; $author = \XLite\Core\Database::getRepo('XLite\Module\EdB\Librairie\Model\Au thor')->find($author_id); $products = $author->getProducts(); return $products;

(dump shows that $products give all books by the author) this will give

Code:
ERROR: "0" (code N/A) Argument 2 passed to XLite\Core\Model\EntityVersion\BulkEntityVersionFetcher::__construct() must be of the type array, null given, called in /srv/http/newbtq72/xcart-53/var/run/classes/XLite/View/ItemsList/Product/Customer/ACustomerAbstract.php on line 918
The result of dump on $data, as is, is

Code:
int(46) int(46)


Basically all I want to do is :
  • get the author_id from the cotroller
  • get the books by the author using getProducts()
  • feed the results into getData so that we can output as itemsList
and, of course, this can be done, for example, in a sql query

Code:
select * from `xc_products` join xc_product_author_links on xc_products.product_id=xc_product_author_links.product_id where author_id=$author_id
and presumably this can be translated into findBy() syntax without too much pains. (Then again, I am not completely sure how to transform the query syntax for findBy to the syntax for search() condition, but this is another issue).



However, since I have already pulled all properties of the author (including Products), I have a feeling that it should be more "economic" to feed the results of the query already made, instead of making another query.
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote