CrazyEnimal Posted September 18, 2019 Share Posted September 18, 2019 Hello! I had a problem which is as follows: using the $pages->find($selectors) method; I can get an array of pages in the system. Is there any way to get an array of fields instead of an array of pages? for example as in SQL query using not SELECT * FROM table WHERE fieldname=value; And I need to get an array of one field as here: SELECT some_field FROM table WHERE another_field=value;? Let me explain: There is an array of pages with the template "Car" where there are fields of type Manufacturer, Model, etc. which are of type Page Reference. Suppose I found all Cars with certain parameters $selectors = "parent=$parent,template=page_car,dealer={$dealer_id}"; $cars = $pages->findMany($selectors); And if I need to find all the manufacturers that can be in $cars I do it in the following way: $manufacturerParent = wire("pages")->get($manufacturerParentID); $manufacturerList = wire("pages")->findMany("parent={$manufacturerParent}"); foreach($manufacturerList as $manufacturerItem) { // I'm get all cars with selectors and with this manufacturer. $carsInManifacturerCount = wire("pages")->count($selectors.",manufacturer=$manufacturerItem"); // if I have cars with this manufacturer I'm do something if($carsInManifacturerCount > 0 ){ $resultManifacturerList = new StdClass(); $resultManifacturerList->id = $manufacturerItem->id; $resultManifacturerList->title = $manufacturerItem->title; $resultManifacturerList->count = $carsInManifacturerCount; $marksListArray[] = $resultMarkList; } } So here's my question, is there a more correct way to get all the Manufacturer without brute force in the same way as the SQL query given? Thanks! Link to comment Share on other sites More sharing options...
wbmnfktr Posted September 18, 2019 Share Posted September 18, 2019 // Source: https://processwire.com/talk/topic/3553-handling-categories-on-a-product-catalogue/?do=findComment&comment=37833 $manufacturers = new PageArray(); foreach($pages->findMany("template=carTemplate") as $car) { // add manufacturer from car page to manufacturers array $manufacturers->add($car->manufacturerField); }; // do whatever you want with the $manufacturers pages Something like this? 1 Link to comment Share on other sites More sharing options...
OLSA Posted September 18, 2019 Share Posted September 18, 2019 There is another option to use Processwire explode. As example: $selectors = "parent=$parent,template=page_car,dealer={$dealer_id}"; $cars = $pages->findMany($selectors); $manufacturers = $cars->explode("manufacturer"); Also later you can do and other things, $manufacturers->sort("title") etc... And there are and other nice WireArray methods like implode, filter, and many others... Regards. 4 Link to comment Share on other sites More sharing options...
CrazyEnimal Posted September 19, 2019 Author Share Posted September 19, 2019 11 hours ago, wbmnfktr said: // Source: https://processwire.com/talk/topic/3553-handling-categories-on-a-product-catalogue/?do=findComment&comment=37833 $manufacturers = new PageArray(); foreach($pages->findMany("template=carTemplate") as $car) { // add manufacturer from car page to manufacturers array $manufacturers->add($car->manufacturerField); }; // do whatever you want with the $manufacturers pages Something like this? Sorry, but it is same brute force. Link to comment Share on other sites More sharing options...
CrazyEnimal Posted September 19, 2019 Author Share Posted September 19, 2019 9 hours ago, OLSA said: There is another option to use Processwire explode. As example: $selectors = "parent=$parent,template=page_car,dealer={$dealer_id}"; $cars = $pages->findMany($selectors); $manufacturers = $cars->explode("manufacturer"); Also later you can do and other things, $manufacturers->sort("title") etc... And there are and other nice WireArray methods like implode, filter, and many others... Regards. Yes! explode() sounds like cool method! But after explode() i try to do unique() of filter() and get 500 error ? in erro message I see "Fatal Error: Uncaught Error: Call to a member function unique() on array in" I need to get a unique manufacturer array as well as the number of manufacturer repeats in the array. Do you now how I can do this ? Thanks a lot! Link to comment Share on other sites More sharing options...
3fingers Posted September 19, 2019 Share Posted September 19, 2019 What about: // Convert a regular array into a WireArray again $wireManufacturers = new WireData(); $wireManufacturers->setArray($manufacturers); Idea comes from here. Link to comment Share on other sites More sharing options...
Robin S Posted September 19, 2019 Share Posted September 19, 2019 @CrazyEnimal, please insert your code inside a code block in forum posts. 16 hours ago, CrazyEnimal said: for example as in SQL query using not SELECT * FROM table WHERE fieldname=value; You can use SQL in ProcessWire when it suits you. That's what the $database API variable is for. Here is one way you could get a listing of manufacturers with the number of occurrences within a selection of cars. // Get the IDs of the cars $car_ids = $pages->findIDs("parent=$parent, template=page_car, dealer=$dealer_id"); // Get table for manufacturer Page Reference field $table = $fields->get('manufacturer')->getTable(); // Get manufacturers that are selected in the car pages $query = $database->query("SELECT data FROM $table WHERE pages_id IN (" . implode(',', $car_ids) . ")"); $manufacturer_ids = $query->fetchAll(\PDO::FETCH_COLUMN); // Count how many times each manufacturer occurs in the results $manufacturer_occurrences = array_count_values($manufacturer_ids); // Sort the results in order of occurrences, highest to lowest arsort($manufacturer_occurrences); // Get the manufacturer pages $manufacturers = $pages->getById(array_keys($manufacturer_occurrences)); // Output a list of manufacturer titles and the number of occurrences foreach($manufacturers as $manufacturer) { echo "<p>{$manufacturer->title} ({$manufacturer_occurrences[$manufacturers->id]})</p>"; } 5 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now