$('.add-to-cart').on('click', function (e) {
e.preventDefault();
var id = $(this).data('id'),
size = $('.selected').attr('id'),
qty = $('#qty').val();
$.ajax({
url: '/cart/add',
data: {id: id, qty: qty, size: size},
type: 'GET',
success: function(res){
if(!res) alert('Ошибка!');
showCart(res);
},
error: function(){
alert('Error!');
}
});
});
<a href="<?= \yii\helpers\Url::to(['product/view', 'id' => $product->id]) ?>" class="pa db img">
<?= Html::img($mainImg->getUrl('225x225'), ['alt' => $product->name])?>
</a>
array(4) { ["id"]=> int(5) ["product_id"]=> string(1) "9" ["size_p"]=> string(3) "455" ["status"]=> int(0) }
object(app\models\Product)#119 (8) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(13) { ["id"]=> string(1) "9" ["category_id"]=> string(1) "4" ["article"]=> string(1) "9" ["name"]=> string(48) "Детские комнаты: Проект 1ad9" ["content"]=> NULL ["composition"]=> NULL ["price"]=> float(0) ["keywords"]=> NULL ["description"]=> NULL ["img"]=> string(12) "no-image.png" ["hit"]=> string(1) "0" ["new"]=> string(1) "0" ["sale"]=> string(1) "0" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(13) { ["id"]=> string(1) "9" ["category_id"]=> string(1) "4" ["article"]=> string(1) "9" ["name"]=> string(48) "Детские комнаты: Проект 1ad9" ["content"]=> NULL ["composition"]=> NULL ["price"]=> float(0) ["keywords"]=> NULL ["description"]=> NULL ["img"]=> string(12) "no-image.png" ["hit"]=> string(1) "0" ["new"]=> string(1) "0" ["sale"]=> string(1) "0" } ["_related":"yii\db\BaseActiveRecord":private]=> array(1) { ["size"]=> array(4) { [0]=> object(app\models\Size)#131 (8) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(4) { ["id"]=> int(3) ["product_id"]=> string(1) "9" ["size_p"]=> string(2) "33" ["status"]=> int(0) } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(4) { ["id"]=> int(3) ["product_id"]=> string(1) "9" ["size_p"]=> string(2) "33" ["status"]=> int(0) } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(1) { ["image"]=> object(rico\yii2images\behaviors\ImageBehave)#132 (3) { ["createAliasMethod"]=> bool(false) ["owner"]=> *RECURSION* ["_module":"rico\yii2images\behaviors\ImageBehave":private]=> NULL } } } [1]=> object(app\models\Size)#133 (8) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(4) { ["id"]=> int(4) ["product_id"]=> string(1) "9" ["size_p"]=> string(2) "44" ["status"]=> int(0) } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(4) { ["id"]=> int(4) ["product_id"]=> string(1) "9" ["size_p"]=> string(2) "44" ["status"]=> int(0) } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(1) { ["image"]=> object(rico\yii2images\behaviors\ImageBehave)#134 (3) { ["createAliasMethod"]=> bool(false) ["owner"]=> *RECURSION* ["_module":"rico\yii2images\behaviors\ImageBehave":private]=> NULL } } } [2]=> object(app\models\Size)#135 (8) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(4) { ["id"]=> int(5) ["product_id"]=> string(1) "9" ["size_p"]=> string(3) "455" ["status"]=> int(0) } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(4) { ["id"]=> int(5) ["product_id"]=> string(1) "9" ["size_p"]=> string(3) "455" ["status"]=> int(0) } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(1) { ["image"]=> object(rico\yii2images\behaviors\ImageBehave)#136 (3) { ["createAliasMethod"]=> bool(false) ["owner"]=> *RECURSION* ["_module":"rico\yii2images\behaviors\ImageBehave":private]=> NULL } } } [3]=> object(app\models\Size)#137 (8) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(4) { ["id"]=> int(6) ["product_id"]=> string(1) "9" ["size_p"]=> string(3) "666" ["status"]=> int(1) } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(4) { ["id"]=> int(6) ["product_id"]=> string(1) "9" ["size_p"]=> string(3) "666" ["status"]=> int(1) } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(1) { ["image"]=> object(rico\yii2images\behaviors\ImageBehave)#138 (3) { ["createAliasMethod"]=> bool(false) ["owner"]=> *RECURSION* ["_module":"rico\yii2images\behaviors\ImageBehave":private]=> NULL } } } } } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(1) { ["image"]=> object(rico\yii2images\behaviors\ImageBehave)#120 (3) { ["createAliasMethod"]=> bool(false) ["owner"]=> *RECURSION* ["_module":"rico\yii2images\behaviors\ImageBehave":private]=> NULL } } }
array(4) { ["id"]=> int(5) ["product_id"]=> string(1) "9" ["size_p"]=> string(3) "455" ["status"]=> int(0) }
array(4) { ["id"]=> int(6) ["product_id"]=> string(1) "9" ["size_p"]=> string(3) "666" ["status"]=> int(1) }
public function getProducts(){
return $this->hasMany(Product::className(), ['id' => 'product_id']);
}
public function getSize(){
return $this->hasMany(Size::className(), ['product_id' => 'id']);
}
<?php
foreach($products as $product) {
echo "<h2>Название : " . $product->name . "</h2>";
echo "<ul>";
foreach($product->size as $sizes) { // no query executed here
echo "<li>" . $sizes->$size_p . "</li>";
}
echo "</ul>";
}
?>
Array ( [id] => 7 [product_id] => 7 [size_p] => 777 [status] => 1 )
Getting unknown property: app\models\Size::