我正在尝试在ProductReview
的实例上创建具有CRUD和模型和数据库tabel的实例,但我得到一个空对象。我尝试了其他型号,但它们都按预期工作。你可以提前吗?
这就是我在控制器中得到的:
$review = new ProductReview();
var_dump($review);die;
返回:
object(backend\modules\products\models\ProductReview)#322 (8) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> NULL ["_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(0) { } }
因此我在ActiveForm
中收到错误:
Call to a member function isAttributeRequired() on string
为什么会发生这种情况,为什么只有ProductReview
才会发生?你们有线索吗?谢谢!
修改 型号:
<?php
namespace backend\modules\products\models;
use Yii;
use backend\modules\products\models\Product;
/**
* This is the model class for table "product_review".
*
* @property integer $id
* @property integer $product_id
* @property integer $user_id
* @property integer $active
* @property string $language
* @property string $names
* @property string $email
* @property string $title
* @property string $review
* @property integer $rating
* @property string $dt
*/
class ProductReview extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'product_review';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['product_id', 'language', 'names', 'review', 'rating'], 'required'],
[['product_id', 'user_id', 'active', 'rating', 'is_home', 'status'], 'integer'],
[['review'], 'string'],
[['dt'], 'safe'],
[['language'], 'string', 'max' => 10],
[['names', 'title'], 'string', 'max' => 255],
[['email'], 'string', 'max' => 100],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'product_id' => Yii::t('app', 'Product'),
'user_id' => Yii::t('app', 'Customer'),
'active' => Yii::t('app', 'Active'),
'language' => Yii::t('app', 'Language'),
'names' => Yii::t('app', 'Names'),
'email' => Yii::t('app', 'Email'),
'title' => Yii::t('app', 'Title'),
'review' => Yii::t('app', 'Review'),
'rating' => Yii::t('app', 'Rating'),
'dt' => Yii::t('app', 'Date'),
'is_home' => Yii::t('app', 'home'),
];
}
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
public function getProduct()
{
return $this->hasOne(Product::className(), ['id' => 'product_id']);
}
public function changeActiveForm() {
$active = "";
if ($this->active == 1) {
$active = 'checked="checked"';
}
return '<label class="switchery switchery-default taCenter">
<input type="checkbox" class="js-switch" data-color="#99d683" data-secondary-color="#f96262" value="1" id="check_' . $this->id . '" name="field_types" class="legend-switch" ' . $active . ' onchange="changeStatusActive(' . $this->id . ', \'product-review\');"></input>
<label data-off="'.Yii::t('app', 'Не').'" data-on="'.Yii::t('app', 'Да').'" for="check_' . $this->id . '"></label>
<span></span>
</label>';
}
public function changeHome() {
$active = "";
if ($this->is_home == 1) {
$active = 'checked="checked"';
}
return '<label class="switchery switchery-default taCenter">
<input type="checkbox" class="js-switch" data-color="#99d683" data-secondary-color="#f96262" value="1" id="checkH_' . $this->id . '" name="field_types" class="legend-switch" ' . $active . ' onchange="changeHome(' . $this->id . ', \'product-review\');"></input>
<label data-off="'.Yii::t('app', 'Не').'" data-on="'.Yii::t('app', 'Да').'" for="checkH_' . $this->id . '"></label>
<span></span>
</label>';
}
public function changeStatus() {
$active = "";
if ($this->status == 1) {
$active = 'checked="checked"';
}
return '<label class="switchery switchery-default taCenter">
<input type="checkbox" class="js-switch" data-color="#99d683" data-secondary-color="#f96262" value="1" id="checkS_' . $this->id . '" name="field_types" class="legend-switch" ' . $active . ' onchange="changeStatus(' . $this->id . ', \'product-review\');"></input>
<label data-off="'.Yii::t('app', 'Не').'" data-on="'.Yii::t('app', 'Да').'" for="checkS_' . $this->id . '"></label>
<span></span>
</label>';
}
public function getUserList() { // could be a static func as well
$users = User::find()->all();
$arrayUsers = array();
if($users){
foreach($users as $user){
$arrayUsers[$user->id] = $user->username;
}
}
return $arrayUsers;
}
public function getProductList() { // could be a static func as well
$products = Product::find()->all();
$arrayProducts = array();
if($products){
foreach($products as $product){
$arrayProducts[$product->id] = $product->title;
}
}
return $arrayProducts;
}
public function showRating() {
$rating = $this->rating;
$ratingHTML = '';
$ratingHTML .= '<ul class="ratingList">';
$ratingHTML .= ($rating >= 1) ? '<li class="active">': '<li>';
$ratingHTML .= '<i class="fa fa-star-o empty tr_all_hover"></i>';
$ratingHTML .= '<i class="fa fa-star active tr_all_hover"></i>';
$ratingHTML .= '</li>';
$ratingHTML .= ($rating >= 2) ? '<li class="active">': '<li>';
$ratingHTML .= '<i class="fa fa-star-o empty tr_all_hover"></i>';
$ratingHTML .= '<i class="fa fa-star active tr_all_hover"></i>';
$ratingHTML .= '</li>';
$ratingHTML .= ($rating >= 3) ? '<li class="active">': '<li>';
$ratingHTML .= '<i class="fa fa-star-o empty tr_all_hover"></i>';
$ratingHTML .= '<i class="fa fa-star active tr_all_hover"></i>';
$ratingHTML .= '</li>';
$ratingHTML .= ($rating >= 4) ? '<li class="active">': '<li>';
$ratingHTML .= '<i class="fa fa-star-o empty tr_all_hover"></i>';
$ratingHTML .= '<i class="fa fa-star active tr_all_hover"></i>';
$ratingHTML .= '</li>';
$ratingHTML .= ($rating >= 5) ? '<li class="active">': '<li>';
$ratingHTML .= '<i class="fa fa-star-o empty tr_all_hover"></i>';
$ratingHTML .= '<i class="fa fa-star active tr_all_hover"></i>';
$ratingHTML .= '</li>';
return $ratingHTML;
}
}