| ||||||||||
Shopping cart software Solutions for online shops and malls | ||||||||||
|
X-Cart Home | FAQ | Forum rules | Calendar | Mark Forums Read | User manuals | Login |
How to show all products related to an entity in another model | |||
|
|
Thread Tools | Search this Thread |
#1
|
|||||||
|
|||||||
How to show all products related to an entity in another model
First of all, here is what I would like to achieve. The module I am working on is for X-cart 5.3.6.0. We sell books, and we would like
to have a page of an author, where we have the name, photo, description of the author (this part has been done), and all his books underneath. As I wrote in the thread, https://forum.x-cart.com/showthread.php?t=77082 I made a model for entities "Author" in ManyToMany relationship with the model Product. I have following files to output the page Controller file Controller/Customer/Author.php with Code:
page viewer file View/Page/Customer/Author.php (basically a modification of View/Page/Customer/Category.php ) Code:
Code:
This part is working. Now, to output the list of books by the author, I have another widget Librairie/View/ItemsList/Product/Customer/BooksBy.php with the following content (below, I removed the part that "should" define which products to show, so with this we get all products). Code:
Of course, I could join the xc_products table with xc_product_author_links table and create a query condition, write a function in Repo class, and this might take less time to finish writing up. However, as I already have pulled the author entity with all properties, including getProducts() which gives an array of products, I would rather use this array to "feed" the getData function. Now, I encounter two problems.
Code:
I have seen is https://forum.x-cart.com/showthread.php?t=70741 so I tried the following (among others) Code:
Code:
and in the log, I find, Code:
So, would anyone have any advice on this?
__________________
X-cart 5.2.12, php 5.6 Ed from Grenoble, France Last edited by Ed B. : 08-11-2019 at 09:11 AM. Reason: Corrected a dead link as well as gave the version number. |
|||||||
#2
|
|||||||||
|
|||||||||
Re: How to show all products related to an entity in another model
Quote:
First of all why are you making Many To Many relationship? If you do this it means one book can be written by more than one author. I mean there are cases when a book has multiple co-writers but they are usually treated as team? I would change that to One Book One Writer and One Writer Many Books relationship. The you can easily get Author/Books by using Book->getAuthor() and Author->getBooks() Even with Many To Many connection you still use Entity->getBooks() and Entity->getAuthors() There is no need of any fancy functions.
__________________
Steve Stoyanov CFLSystems.com Web Development |
|||||||||
#3
|
|||||||
|
|||||||
Re: How to show all products related to an entity in another model
Quote:
reasons (and others), we have made this choice. Quote:
Author->getBooks() is actually working (as long as I define the method for each widget class!), my problem is to use the output of this to "feed" getData(). For example, if I do Code:
Code:
__________________
X-cart 5.2.12, php 5.6 Ed from Grenoble, France |
|||||||
#4
|
|||||||||
|
|||||||||
Re: How to show all products related to an entity in another model
getData() works off of the request so you need to look at what your request is and what getData() gives you.
Use the Doctrine dump to check the data if you are not sure. You should have Author if you want to get the books associated with the entity. So you can do like Code:
If you want to use Code:
__________________
Steve Stoyanov CFLSystems.com Web Development |
|||||||||
|
#5
|
|||||||
|
|||||||
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:
(dump shows that $products give all books by the author) this will give Code:
Code:
Basically all I want to do is :
Code:
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 |
|||||||
#6
|
|||||||||
|
|||||||||
Re: How to show all products related to an entity in another model
I don't really know what you are doing or how or what your full module looks like. So the code is intended to give you a hint and not to be used directly. You probably have to modify it to fit your module.
For example I keep saying "books" as you call them that but then in your code you use "products" - so obviously you have to modify this snippet of code to fit your module and needs. The "find" method on the entity will search by the table primary id field (whatever that is) so there is no need to use "findby". 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. Best is to modify the $cnd variable and then call the parent getData with the new conditions. These are just fragments of code you are posting so it is really hard to give you exact code. 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.
__________________
Steve Stoyanov CFLSystems.com Web Development |
|||||||||
|
#7
|
|||||||
|
|||||||
Re: How to show all products related to an entity in another model
Thank you so much for your patience.
Quote:
As a matter of fact, Code:
Code:
Now I have the above code in Model/Repo/Product.php, and the part of View/ItemsList/Products/Customer/BooksBy.php looks like Code:
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:
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:
and the relevant portion of Model/Product.php looks like Code:
Quote:
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 |
|||||||
#8
|
|||||||||
|
|||||||||
Re: How to show all products related to an entity in another model
https://devs.x-cart.com/getting_started/working-with-database.html
search in the page for "// defining condition object"
__________________
Steve Stoyanov CFLSystems.com Web Development |
|||||||||
#9
|
|||||||
|
|||||||
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:
something like Code:
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:
Code:
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 |
|||||||
|
Thread Tools | Search this Thread |
|
|
|
|||
X-Cart forums © 2001-2020
|