View Single Post
  #7  
Old 08-13-2019, 09:51 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 so much for your patience.


Quote:
Originally Posted by cflsystems


If the $products variable after you do the "find" gives you all the products as expected then the following errors are somewhere else in your code. However - you are trying to change what getData returns and this may also change the type of data being returned. So this could be the reason you are seeing the error.


As a matter of fact,
Code:
$author->getProducts
and the following give exactly same set of products, but somehow they don't contain same informations.
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; }


Now I have the above code in Model/Repo/Product.php, and the part of

View/ItemsList/Products/Customer/BooksBy.php looks like
Code:
protected function getData(\XLite\Core\CommonCell $cnd, $countOnly = false) { if(!isset($this->BooksbyAuthor)) { $author_id = \XLite\Core\Request::getInstance()->author_id; $this->BooksbyAuthor = \XLite\Core\Database::getRepo('XLite\Model\Produc t')->findBooksBy($author_id); } return true == $countOnly ? count($this->BooksbyAuthor) : $this->BooksbyAuthor; }
and I get the correct ItemsList.


I am not very happy with this solution for two reasons: basically here I am defining a
variant of getProducts() from scratch that will fit in getData() without using what I already have

in getProducts().


And the second is

Quote:
Originally Posted by cflsystems
Best is to modify the $cnd variable and then call the parent getData with the new conditions.


I believe you are right, but somehow having gone through the dev doc, I have been unable to find the syntax for $cnd in search()







Quote:
Originally Posted by cflsystems
These are just fragments of code you are posting so it is really hard to give you exact code.

Now I have realized that in my first post Model/Author.php is not complete and Model/Product.php is missing. Model/Author.php is attached in the other thread https://forum.x-cart.com/attachment.php?attachmentid=5352&d=1565023268

and the relevant portion of Model/Product.php looks like
Code:
/** * Authors * * @var \Doctrine\Common\Collections\ArrayCollection * * @ManyToMany (targetEntity="XLite\Module\EdB\Librairie\Model\Author", inve rsedBy="products") * @JoinTable (name="product_author_links", * joinColumns={@JoinColumn (name="product_id", referencedColumnName="p roduct_id", onDelete="CASCADE")}, * inverseJoinColumns={@JoinColumn (name="author_id", referencedColumnN ame="author_id", onDelete="CASCADE")} * ) */ protected $authors;
(I know, I am not quoting the entire code, but the class contains whole bunch of functions which are unrelated to the problem we discuss here and the file is quite large). I also have classes related to the images, editing author entities in admin area etc, but these shouldn't really affect my current problem.




Quote:
Originally Posted by cflsystems
But it does sound like if you apply author condition to it will give you what you need. So you may just need to add this to the $cnd variable in getData. Again - you will need to know what is already there, what exactly you are passing, etc.
If your author field in the products table is not author but authors (since you have Many To Many relationship you may need to pass a collection of authors even if it is just one.




The relationship is Many To Many, so in the products table there is no such field as author or authors, instead there is product_author_link table, consisting of id, product_id and author_id.
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote