Yii2关系功能

时间:2017-02-13 07:16:42

标签: php oop yii2

我有一个关系函数,当数据库中有image_name时,它可以很好地工作。

我可以使用$model->fromProfile->mainImage->image_name获取图像文件名,但它可以正常工作。

但是如果查询返回whiteout image_name(如果用户没有上传任何符合查询的图像),我将收到一条错误,上面写着“试图获取非对象的属性”。

但为什么呢?我在函数中有一个规则应该处理这个,函数应该检查image_name是否存在,如果不存在,则image_name应该是'no_image.jpg';

这是应该处理这种情况的函数:

public function getMainImage(){
    $mainImage = $this->hasOne(Image::className(), ['profile_id' = > 'profile_id'])
        ->andOnCondition(['main' = > '1']);

    if (!isset($mainImage->image_name)) {
        $mainImage = new Image();
        $mainImage->image_name = 'no_image.jpg';
    }

    return $mainImage;
}

这是个人资料模型:

public static function tableName(){
    return 'tbl_user_profile';
}

public function rules(){
    return[
        [['user_id'], 'required'],
            [['user_id'], 'integer'],
            [['place'], 'in', 'range' = >array_keys(self::PLACE)],
            [['meeting'], 'in', 'range' = >array_keys(self::MEETING)],
            [['expiriance'], 'in', 'range' = >array_keys(self::EXPIRIANCE)],
            [['lookingfor'], 'in', 'range' = >array_keys(self::LOOKINGFOR)],
            [['about'], 'string'],
            [['user_id'], 'exist', 'skipOnError' = > true, 'targetClass' = > User::className(), 'targetAttribute' = >['user_id' = > 'user_id']],
    ];
}

public function attributeLabels(){
    return[
        'profile_id' = > 'Profile ID',
            'user_id' = > 'User ID',
            'about' = > 'about',
            'place' = > 'place',
            'meeting' = > 'meeting',
            'expiriance' = > 'expiriance',
            'lookingfor' = > 'Looking for',
    ];
}


public function getMainImage(){
    $mainImage = $this->hasOne(Image::className(), ['profile_id' = > 'profile_id'])
        ->andOnCondition(['main' = > '1']);

    if (!isset($mainImage->image_name)) {
        $mainImage = new Image();
        $mainImage->image_name = 'no_image.jpg';
    }

    return $mainImage;
}

这是图像模型:

public static function tableName(){
    return 'tbl_images';
}

public function rules() {
    return[
        [['profile_id', 'image_name'], 'required'],
            [['profile_id', 'is_private', 'main'], 'integer'],
            ['image_name', 'image', 'minWidth' = > 250, 'minHeight' = > 250, 'extensions' = > 'jpg, gif, png, jpeg', 'maxSize' = > 1024 * 1024 * 2],
            [['image_name'], 'string', 'max' = > 255],
            [['profile_id'], 'exist', 'skipOnError' = > true, 'targetClass' = > Profile::className(), 'targetAttribute' = >['profile_id' = > 'profile_id']],
    ];
}

public function attributeLabels(){
    return[
        'image_id' = > 'Image ID',
            'profile_id' = > 'Profile ID',
            'image_name' = > 'Image Name',
            'is_private' = > 'Private Image',
            'main' = > 'Main Image',
    ];
}

public function getUser(){
    return $this->hasOne(User::className(), ['user_id' = > 'user_id']);
}

public function getViewers(){
    return $this->hasOne(View::className(), ['viewed_profile_id' = > 'profile_id']);
}


public function getProfile(){
    return $this->hasOne(Profile::className(), ['profile_id' = > 'profile_id']);
}

1 个答案:

答案 0 :(得分:0)

你应该检查$ mainImage而不是$ mainImage-> image_name

 if(!isset($mainImage))
 {  
    $mainImage = new Image();
    $mainImage->image_name = 'no_image.jpg';
  }

或者检查您是否正确输入了列名image_name 或者尝试为每个部分添加表名(由于column_name的限制

  $mainImage = $this->hasOne(Image::className(), 
        ['tbl_user_profile.profile_id' => 'tbl_images.profile_id'])