Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Filter complex DataObjects


Go to End


4 Posts   1990 Views

Avatar
Franckysnow

Community Member, 4 Posts

19 January 2016 at 11:11am

Edited: 19/01/2016 11:12am

Hi everyone,
I am trying to filter some DataObjects based on the Title field of a field in that DataObject. However the returned array is empty.

My Product.php :

class Product extends DataObject {
	private static $db = array (
		'Name' => 'Varchar'
	);
	private static $has_one = array (
		'Supplier' => 'SupplierPage'
	);
	private static $many_many = array (
		'Categories' => 'CategoryPage',
	);
}

My SupplierPage.php :

class SupplierPage extends Page {
	private static $db = array (
		'ShortDescription' => 'Text',
	);
	private static $has_many = array (
		'Products' => 'Product'
	);
}

My CategoryPage.php :

class CategoryPage extends Page {
	private static $db = array (
		'ShortDescription' => 'Text',
	);
	private static $belongs_many_many = array (
		'Products' => 'Product'
	);
}
class CategoryPage_Controller extends Page_Controller {
	public function GetSuppliers() {
		return SupplierPage::get();
	}
	public function GetProductsBySupplier($supplier) {
		return $this->Products(Null, 'Name ASC')->filter('Supplier.Title', $supplier);
	}
}

My template CategoryPage.ss

<!-- Sorting by Suppliers-->
<% loop GetSuppliers() %>
	<!-- listing products for supplier $Title -->
	<% loop $Up.GetProductsBySupplier($Title) %>
		<!-- found product $Name from $Supplier.Title -->
	<% end_loop %>
<% end_loop %>

The issue is that I never find any product. Could you please enlighten me? I looked in the API, but I could not understand what was the function call for the

$this->Products()
. Also, I found some infos on DataObjectSet which seem to be deprecated.
What is the current way of doing that?

Thank you in advance!

Avatar
Lexandclo

Community Member, 55 Posts

19 January 2016 at 10:46pm

Hi
Maybe try this in your Category page

function GetProductsBySupplier($num='') { 
     		$supplierpage = DataObject::get_one("SupplierPage"); 
     		return ($supplierpage) ? DataObject::get("SupplierPage", "", "", "", $num) : false;
     	}
 

Then In your template call

<% if $GetProductsBySupplier%>
			<% loop $GetProductsBySupplier %>							
				<li>$Title</li>
			<% end_loop %>
		<% end_if %>

Then you can add ASC , DEs and limits to the function

Let Me know how you get on

Thanks

Darren

Avatar
Franckysnow

Community Member, 4 Posts

1 February 2016 at 8:30am

Hi Daren,
Thank you for your answer. I am getting all the Supplier names entered in the whole site.
However, what I am trying to achieve is: on a CategoryPage, list all Suppliers. Then, under every Supplier, list all products manufactured by that supplier, and matching the current Category.

Any idea how I could achieve that?

Thank you in advance for your help!

Avatar
Lexandclo

Community Member, 55 Posts

8 February 2016 at 10:27pm

HI
Sorry for late reply.
Maybe the best option is to join the tables in the function?.
So join the Supplierpage ID to get the products listed against that ID form the products table ,

Thanks