Вот связи в БД
Вот связь в категориях:
Код: Выделить всё
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(
'product' => array(self::MANY_MANY, 'Product', 'gyp_product_category(product_id, category_id)'),
);
}
Код: Выделить всё
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(
'category' => array(self::MANY_MANY, 'Category', 'gyp_product_category(product_id, category_id)'),
);
}
Код: Выделить всё
public function actionAdd_product () {
$model = new Product();
if(isset($_POST['Product'])) {
$product = new Product();
$product->attributes = $_POST['Product'];
if($product->validate()) {
if($product->save(false)) {
ProductCategory::saveCategories($product->id, $_POST['Product']['category']);
}
$this->redirect('products');
}else {
$this->render('add_product', array('model' => $product));
}
}
$this->render('add_product', ['model' => $model]);
}
Код: Выделить всё
public static function saveCategories($product_id, $categories) {
$pc = new ProductCategory();
foreach($categories as $category) {
$category = (int) $category;
$pc->unsetAttributes();
$pc->attributes = ['product_id' => $product_id, 'category_id' => $category];
if($pc->validate())
$pc->save(false);
}
}
Но проблема в том, что сохраняется либо одна категория, либо ошибка
Со вчерашнего дня пытаюсь решить, голова не варит уже.CDbCommand не удалось исполнить SQL-запрос: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'