Hi I have a database where i use a link table to link products and categories.
The relationships look like this:
Product ProductCategories Category
Id Id Id
Name ProductId Name
CategoryId
So the productCategory table links Products to Categories
My problem is when im trying to find all Products under the category with the Id of 1
I use this code but it doesnt seem to be working:
$models = Products::model()->with('productcategories')->findByPk(1);
This is the Products Relationships:
public function relations()
{
return array(
'productcategories' => array(self::HAS_MANY, 'Productcategories', 'ProductId'),
);
}
public function relations()
{
return array(
'productcategories' => array(self::HAS_MANY, 'Productcategories', 'ProductId'),
'Categories' => array(self::HAS_MANY, 'Category', '',
'through'=>'productcategories',
'on' => 'Categories.Id = productcategories.CategoryId',
),
);
}
// Get all Product with a Category with id = 1
$models = Products::model()->with('Categories')->findAll('Categories.Id = 1');
Related
I have certain products..now i want to create a subcategory for those product..i created a table for subcategory with category as foreign key...
Please anybody help me to achieve this
I think the best way to do it is to go to YOURDOMAIN/index.php/gii
And you create your models and the relations using the interface.
Thanks
You can create a model SubCategory like you did for Category, but with this relation:
public function relations()
{
return array(
'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
);
}
In Category you can add this relation:
public function relations()
{
return array(
'subCategories' => array(self::HAS_MANY, 'SubCategory', 'category_id'),
);
}
I have a HAS_MANY relation between 2 models, i use bookID as foreign key
model 1 - Importedbooks, Importedbooks can have many CountryOfImport
public function relations()
{
return array(
'CountryOfImportObj'=>array(self::HAS_MANY, 'CountryOfImport', 'bookID')
);
}
model 2 CountryOfImport, CountryOfImport belongs to Importedbooks
public function relations()
{
return array(
'ImportBooksObj'=>array(self::BELONGS_TO, 'Importedbooks', 'bookID')
);
}
Now, For my CGridView i am using a model->search()of the Importedbooks model as my dataProvider, From here is where i get stuck.
CGridView
$data = $model->search();
$data->setPagination(array('pageSize'=>'5'));
$data->criteria->addCondition('active = "yes"');
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$data
,'filter'=>$model
,'pager'=>array('header'=>'')
,'columns'=>array(
'id',
'bookYear',
'bookTitle',
'DISPLAY VALUE FROM OTHER model HERE'
)
)
);
DataProvider of the Imported books model, i'm using this as my data provider for the grid
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('bookID',$this->bookID);
$criteria->compare('countryName',$this->countryName,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
The relation works because i can use below code in my controller to get a field(countryName) from the other model
$model = Importedbooks::model()->with('CountryOfImportObj')->findbyPK(3);
print_r($model->CountryOfImportObj[0]->countryName);
Such as Importedbooks have many CountryOfImport, in one row you must show or all country names or concreet names depending on ID or countryName or some other attribute.
You can pass anonymous function as value:
'columns' => array(
'id',
'bookYear',
'bookTitle',
array(
'header' => 'Countries',
'type' => 'raw',
// if you have defined counrtyName attribute in 'Importedbooks' model, if not you can remove this row
// also CGridView 'uses' attribute 'name' for filtering or sorting
'name' => 'countryName',
'value' => function($data, $row) { //$data is item of DataProvider, $row is number of row (starts from 0)
$countries = CHtml::listData((array)$data->CountryOfImportObj, 'id', 'countryName');
return implode(', ', $countries);
}
)
)
If you want to filter or sort by external attributes too, you must "declare them"
Thanks!
I'm still new to Yii, but I'm trying to use relations() to join two tables and get all data from both table on each row pulled.
tables:
TABLE Artist KEYS(artist_Id, firstName, lastName)
TABLE Album KEYS(album_Id, title, artist_Id, genre)
// Album
public function relations()
{
return array(
'artist' => array(self::BELONGS_TO, 'Artist', 'artist_Id'),
'track' => array(self::HAS_MANY, 'Track', 'track_Id')
);
}
// Artist
public function relations()
{
return array(
'album' => array(self::HAS_MANY, 'Album', 'album_id')
);
}
// logic for getting information
$dataProvider=new CActiveDataProvider('Album');
foreach($dataProvider->getData() as $key){
echo '<br>' . $key->artist_Id; // does work
echo '<br>' . $key->firstName; // doesn't work
}
With this code, I can get and display the correct artist_Id for the album. However, I want to display the artist firstName and lastName with the artist_Id.
I needed to change my $dataProvider variable and how I was trying to access in the view index.php
$dataProvider = new CActiveDataProvider('Album');
-to-
$dataProvider = new CActiveDataProvider('Album', array('criteria' => array('with' => array('artist'))));
and changing view index.php to one of the two:
'name' => 'artist.lastName',
or
'value' => '$data->artist->firstName'
I have Categories associated to many Products but a Product can only have one category.
In my Category view, I have a list of all products assign to that particular category and outputted into a table. I'm trying to paginate this table, since it can easily grow in size.
My CategoriesController, View action:
public function admin_view($id=null) {
if (!$id) {$this->redirect('/admin/categories');}
$category = $this->Category->find('first', array(
'conditions' => array(
'Category.id' => $id
),
'contain' => array('Product')
));
if(!$category) {
$this->redirect('/admin/categories');
} else {
$this->set('category', $category);
}
Category Model: public $hasMany = array('Product');
Product Model:
public $belongsTo = 'Category';
This outputs the data with a Category array with all the Category data and a Products array with all the Products associated with this particular Category:
Question: How can I paginate the Products list that I have as a table in my Categories Admin_View view?
$this->Paginator->settings = array(
'conditions' => array(
'Category.id' => $id
),
'contain' => array('Product'),
'limit' => 25
);
$category = $this->paginate('Category');
I am new to yii and the model relation is new to me. I am currently doing a system that has a table structure:
products
PK id
brand
product_locales
PK id
FK product_id
name
locale
product_relations
PK id
FK product_id
FK related_id
my Product model relation:
public function relations()
{
return array(
'productlocales' => array(self::HAS_MANY, 'ProductLocale', 'product_id'),
'relations' => array(self::MANY_MANY, 'Product', 'product_relations(product_id, related_id)')
);
}
then my Product Locale Relation:
public function relations()
{
return array(
'product' => array(self::BELONGS_TO, 'Product', 'product_id')
);
}
in my Product Controller when I call this code:
$product = ProductLocale::model()->findByPk(1);
var_dump($product->product->relations);
it outputs the id and brand of a related product from the product table. but what I want to output is all the locales of the product, which is the name and the locale.
Can anyone help me out with this?
Thanks in advance.
I don't understand the product_relations table, and seems like the relation whith Product is HAS_MANY, not MANY_MANNY.
In any case, if I have understood, you need find the id of product and then find all the product_locale with this id:
$prodLoc=ProductLocale::model()->findByPk(1);
$arrayProdLoc=ProductLocale::model()->findAllByAttributes('product_id'=>$prodLoc->product_id);
ArrayProd will have models for all product_locale related with the same product.
If you want show this information in a gridview, you probabli will want a DataProvider:
$prodLoc=ProductLocale::model()->findByPk(1);
$provider=CActiveDataprovider('ProductLocale',array (
'criteria'=>array(
'condition'=>'product_id='.$prodLoc->product_id,
));
You can also define a new self-relation into product_locale Model:
public function relations()
{
return array(
'product' => array(self::BELONGS_TO, 'Product', 'product_id'),
'productsLocRelated'=>array(self::HAS_MANY,'ProductLocale',array('product_id'=>'product_id')),
);
}
So you can have all products_locale by:
$prodLoc=ProductLocale::model()->findByPk(1);
$arrayProdLoc=$prodLoc->productsLocRelated;
Note: I haven't tested the code.