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']);
}