[英]How to link attribute in Yii 2 to another model's field?
我通过 hasOne 关系拥有 Products 模型和 ProductProperties:
class Product extends ActiveRecord
{
...
public function getProductProperties()
{
return $this->hasOne(ProductProperties::class, ['product_id' => 'id']);
}
...
}
我在 Products 中有价格属性,我想删除它(包括数据库中的列)并将其链接到 ProductProperties 模型的价格属性。 有可能吗?我该怎么做? 首先,我尝试像这样覆盖属性方法:
public function fields()
{
return [
'price' => function () {
return ProductProperties::find(['product_id' => $this->id])->price;
}
]
...
但我不确定是否可以使用箭头方法分配值。 此外,fields() 方法在返回任何内容之前使用$this->price
:
public function fields()
{
if ($this->price){*some manipulations with price*}
...
return [
'price',
..*other fields*
];
}
问题是如何从模型中删除价格并使用另一个模型的价格属性而不会有太多痛苦?
如果你只想显示价格,你可以这样做
class Product extends ActiveRecord
{
...
public function getProductProperties()
{
return $this->hasOne(ProductProperties::class, ['product_id' => 'id']);
}
public function getPrice() {
return $this->productProperties->price;
}
...
}
并使用它
$product = Product::findOne(1);
echo $product->price; // this is a shortcut
echo $product->productProperties->price; // same as this which is the complete route
要保存数据,您应该首先确定如何处理用户数据收集,因为您有不同的模型并且每个模型都有自己的验证。
但是,如果您想将价格保存为产品属性(我不推荐),您可以执行以下操作
class Product extends ActiveRecord
{
public $price;
public function rules () {
return [
[['price'], 'integer'] // for massive assignment
];
}
public function afterFind()
{
parent::afterFind();
$this->price = $this->productProperties->price;
}
public function getProductProperties()
{
return $this->hasOne(ProductProperties::class, ['product_id' => 'id']);
}
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
if (array_key_exists('price', $changedAttributes)) {
// You should make sure that $this->productProperties exists.
$this->productProperties->price = $this->price;
$this->productProperties->save();
}
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.