Пытаюсь сохранить данные в связанные таблицы с помощью этого поведения, но возникает ошибка Setting read-only property: app\models\Order::orderproducts. Прошу совета как решить проблему
Код order.php
Код: Выделить всё
namespace app\models;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
/**
* This is the model class for table "sm_order".
*
* @property int $id_ord
* @property int $customer_id
* @property string $name
* @property string $email
* @property string $telephone
* @property string $total
* @property string $comment
* @property int $ip
* @property string $user_agent
* @property string $accept_language
* @property string $date_added
* *
* @property OrderProduct[] $orderProduct
*/
class Order extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'rd_order';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return ['saveRelations' => ['class' => SaveRelationsBehavior::className(), 'relations' => ['orderproducts']]];
}
//Использовать транзакции для SaveRelationsBehavior
public function transactions()
{
return [
self::SCENARIO_DEFAULT => self::OP_ALL,
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['customer_id', 'ip','], 'integer'],
[['total'], 'number'],
[['comment'], 'string'],
[['date_added'], 'safe'],
[['name'], 'string', 'max' => 70],
[['email'], 'string', 'max' => 96],
[['telephone'], 'string', 'max' => 20],
[['user_agent', 'accept_language'], 'string', 'max' => 255],
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getOrderProducts()
{
return $this->hasMany(OrderProduct::className(), ['order_id' => 'id_ord']);
}
}
Код: Выделить всё
namespace app\models;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
/**
* This is the model class for table "sm_order_product".
*
* @property int $id_order_prodcut
* @property int $order_id
* @property int $product_id
* @property string $name
* @property string $model
* @property int $quantity
* @property string $price
* @property string $total
*
* @property Order $order
*/
class OrderProduct extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'sm_order_product';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['order_id', 'product_id', 'name', 'ru_model', 'model', 'quantity', 'price', 'total'], 'required'],
[['order_id', 'product_id', 'quantity'], 'integer'],
[['price', 'total'], 'number'],
[['order_id'], 'exist', 'skipOnError' => true, 'targetClass' => Order::className(), 'targetAttribute' => ['order_id' => 'id_ord']],
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getOrders()
{
return $this->hasOne(Order::className(), ['id_ord' => 'order_id']);
}
}
Код: Выделить всё
$order = new Order();
$order->customer_id = 1;
$order->name = 'имя';
$order->telephone = '554545454';
$order->email = 'test@localhost.ru';
$order->total = '1.5';
$order->status_order = 1;
$product = new OrderProduct();
$product->product_id = 1;
$product->name= 'name';
$product->quantity = 1;
$product->price = '15,5';
$product->total = '15,5';
$order->orderproducts = $product;
$order->save();