duanchi1230 2017-02-13 07:16
浏览 54

Yii2关系功能

I have a relation function that works perfectly when there is a image_name in the database.

I can get the image file name using $model->fromProfile->mainImage->image_name and it works.

But if the query return whiteout image_name (in case that the user didn't upload any image that fit the query), I will get an error saying "Trying to get property of non-object".

But why? I have a rule in the function that should handle this, the function should check if the image_name exist and if not the image_name should be 'no_image.jpg';

This is the function that should handle this cases:

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;
}

this is the Profile Model :

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;
}

This is the Image model:

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条回答 默认 最新

  • duanlv1366 2017-02-13 08:50
    关注

    Could be you should check for $mainImage and not $mainImage->image_name

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

    Or check if you have correctly typed the column name image_name or try adding the table name for each part (due the ambiguty of the column_name

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

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)