Hello everyone
So I have been trying this for a while now, I will try my best to explain…should be simple
I have subcategories and Products, Most of my work is generated by yiic. Subcategories has a one-many relation with products. Now I need for when a user visits a specific subcategory to view the products for that subcategory. So for example: subcategory 1 > Product 1, Product 2, etc. I read the yii blg tutorial a few times already (please don’t refer me back). The best I got was to display the amount of products each subcategory has but not the actual products (like the yii blog).
Subcategories.php- I only changed the relations here as so…
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'products' => array(self::HAS_MANY, 'Products', 'PID'),
'sCAT' => array(self::BELONGS_TO, 'Categories', 'SCATID'),
'productsCount' => array(self::STAT, 'Products', 'SCATID'),
);
}
Subcategories/view.php
<div id="products">
<?php if($model->productsCount>=1): ?>
<h3>
<?php echo $model->productsCount . ' product(s)'; ?> // this displays No of products (correctly)
<?php echo $model->products; ?>// this just prints Array (literally)
</h3>
// the following code does nothing just calls _products which I will show anyway (got it from yii blog)
<?php $this->renderPartial('_products',array(
'subcategories'=>$model,
'products'=>$model->products,
)); ?>
<?php endif; ?>
</div>
subcategories/view/_products.php - it does nothing, not even sure if it’s the right way to go about this
<?php foreach($products as $products): ?>
<div class="products" id="c<?php echo $products->pid; ?>">
<?php echo CHtml::link("#{$products->pid}", $products->getUrl($subcategories)); ?>
<div class="content">
<?php echo(CHtml::encode($products->SHORTENAME)); ?>
</div>
</div><!-- products -->
<?php endforeach; ?>
Not sure if you need to see anything else but these are the only things I edited to try and display all products belonging to a subcategory.
Thanks