Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2关系功能_Php_Oop_Yii2 - Fatal编程技术网

Php Yii2关系功能

Php Yii2关系功能,php,oop,yii2,Php,Oop,Yii2,我有一个关系函数,当数据库中有一个图像名称时,它可以完美地工作 我可以使用$model->fromProfile->main image->image\u name获取图像文件名,它可以正常工作 但是如果查询返回whiteout image_name(如果用户没有上传任何适合查询的图像),我会得到一个错误,说“尝试获取非对象的属性” 但是为什么呢?我在函数中有一个规则可以处理这个问题,函数应该检查image_名称是否存在,如果不存在,image_名称应该是'no_image.jpg' 这是处理这

我有一个关系函数,当数据库中有一个图像名称时,它可以完美地工作

我可以使用
$model->fromProfile->main image->image\u name
获取图像文件名,它可以正常工作

但是如果查询返回whiteout image_name(如果用户没有上传任何适合查询的图像),我会得到一个错误,说“尝试获取非对象的属性”

但是为什么呢?我在函数中有一个规则可以处理这个问题,函数应该检查image_名称是否存在,如果不存在,image_名称应该是'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']);
}

可能是您应该检查$mainImage,而不是$mainImage->image\u name

 if(!isset($mainImage))
 {  
    $mainImage = new Image();
    $mainImage->image_name = 'no_image.jpg';
  }
或者检查您是否正确键入了列名
image\u name
或者尝试为每个部分添加表名(由于列名的模糊性)

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

您确定列名吗?…更新了您的问题并添加了图像模型和具有函数getMainImagePlease的模型还添加了配置文件模型..我添加了配置文件模型您有错误?.错误结果?…意外结果?.请解释您的问题..BetterGeting未知属性:yii\db\ActiveQuery::Image\name$mainImage存在,当我使用一个关系时,配置文件模型被传递到$mainImage,$mainImage从不为空…我用一些简短的建议更新了asnwer,希望在存在图像名称时有用我使用:$Model->fromProfile->mainImage->image\u name从视图中获取图像名称。当存在不是图像,然后我只是得到一个错误,说“尝试获取非对象的属性”Ok,所以我有一个关系函数,当数据库中有图像名称时,它可以完美地工作。我可以使用“$model->fromProfile->main image->image\u name”获取图像文件名,它可以工作。但是如果查询返回whiteout image\u name(如果用户没有上传任何符合查询的图像),我会得到一个错误,说“尝试获取非对象的属性”。但是为什么?我在函数中有一个规则来处理这个问题,函数应该检查图像名称是否存在,如果不存在,图像名称应该是“no_image.jpg”;