Доброго времени .. .. У меня в Б/Д в таблицe *user* есть cтолбец *photo* такой код позволяет добавлять в комментарии автора- файлы img . Код (Text): public function saveComment($article_id) { $comment = new Comment; $comment->text = $this->comment; $comment->user_id = Yii::$app->user->id; $comment->article_id = $article_id; $comment->status = 1; //подтверждение коммента 0 $comment->user->photo; // $comment->profile->avatar(); $comment->date = date('Y-m-d'); return $comment->save(); } view Код (Text): <div class="comment-img"> <img width="100" class="img-circle" src="<?//= $comment->profile->avatar; ?>" alt=""> <img width="100" class="img-circle" src="<?= $comment->user->photo; ?>" alt=""> </div> Также есть таблица *profile* cтолбец *avatar* Я пытаюсь перенаправить запрос так: -(раскомментировав выше указанный код)-то есть чтобы рнр брал файлы img c таблицы *profile* cтолбец *avatar* и вставлял их в комменты,но получаю следующую ошибку:- Getting unknown property: app\models\Comment:rofile Пытался решить так правил пути - менял алиасы ,namespace Вообщем провозился я и решения не нашёл Можете помочь , разобрать этот вопрос.
глубокомысленная строка - она просто забирает и всталяет из Б.Д картинки к автору коммента,- --- Добавлено --- мне нужно чтоб он забирал img с это модели.. Код (Text): <?php /** * @copyright Copyright © Gogodigital Srls * @company Gogodigital Srls - Wide ICT Solutions * @website http://www.gogodigital.it * @github https://github.com/cinghie/yii2-user-extended * @license GNU GENERAL PUBLIC LICENSE VERSION 3 * @package yii2-user-extended * @version 0.6.2 */ namespace cinghie\userextended\models; use cinghie\traits\EditorTrait; use dektrium\user\models\Profile as BaseProfile; use yii\base\Exception; use yii\base\InvalidParamException; use yii\db\ActiveQueryInterface; use yii\web\UploadedFile; use yii\web\Upload; use app\models\CommentForm; use app\models\Comment; /** * Class Profile * * @property string $imagePath * @property string $imageUrl * @property string $socialImage * @property ActiveQueryInterface $account * @property Profile $accountAttributes */ class Profile extends BaseProfile { use EditorTrait; /** * @inheritdoc */ public function scenarios() { $scenarios = parent::scenarios(); if(\Yii::$app->getModule('userextended')->avatar) { $scenarios['create'][] = 'avatar'; $scenarios['update'][] = 'avatar'; $scenarios['register'][] = 'avatar'; } if(\Yii::$app->getModule('userextended')->birthday) { $scenarios['create'][] = 'birthday'; $scenarios['update'][] = 'birthday'; $scenarios['register'][] = 'birthday'; } if(\Yii::$app->getModule('userextended')->firstname) { $scenarios['create'][] = 'firstname'; $scenarios['update'][] = 'firstname'; $scenarios['register'][] = 'firstname'; } if(\Yii::$app->getModule('userextended')->lastname) { $scenarios['create'][] = 'lastname'; $scenarios['update'][] = 'lastname'; $scenarios['register'][] = 'lastname'; } if(\Yii::$app->getModule('userextended')->signature) { $scenarios['create'][] = 'signature'; $scenarios['update'][] = 'signature'; $scenarios['register'][] = 'signature'; } return $scenarios; } /** * @inheritdoc */ public function rules() { $rules = parent::rules(); if(\Yii::$app->getModule('userextended')->birthday) { $rules['birthdayLength'] = ['birthday', 'date', 'format' => 'yyyy-mm-dd']; $rules['birthdayRequired'] = ['birthday', 'required']; $rules['birthdayTrim'] = ['birthday', 'trim']; } if(\Yii::$app->getModule('userextended')->firstname) { $rules['firstnameLength'] = ['firstname', 'string', 'max' => 255]; $rules['firstnameRequired'] = ['firstname', 'required']; $rules['firstnameTrim'] = ['firstname', 'trim']; } if(\Yii::$app->getModule('userextended')->lastname) { $rules['lastnameLength'] = ['lastname', 'string', 'max' => 255]; $rules['lastnameRequired'] = ['lastname', 'required']; $rules['lastnameTrim'] = ['lastname', 'trim']; } if(\Yii::$app->getModule('userextended')->signature) { $rules['signatureLength'] = ['signature', 'string']; $rules['signatureTrim'] = ['signature', 'trim']; } return $rules; } /** * @inheritdoc */ public function attributeLabels() { return [ 'avatar' => \Yii::t('userextended', 'Avatar'), 'birthday' => \Yii::t('userextended', 'Birthday'), 'firstname' => \Yii::t('userextended', 'Firstname'), 'lastname' => \Yii::t('userextended', 'Lastname'), 'name' => \Yii::t('userextended', 'Name'), 'signature' => \Yii::t('userextended', 'Signature'), ]; } /** * Upload file * * @param string $filePath * * @return mixed * @throws Exception */ public function uploadAvatar($filePath) { $file = UploadedFile::getInstance($this, 'avatar'); // if no file was uploaded abort the upload if ( null === $file ) { return false; } // file extension $fileExt = $file->extension; // purge filename $fileName = \Yii::$app->security->generateRandomString(); // update file->name $file->name = $fileName.".{$fileExt}"; // update avatar field $this->avatar = $fileName.".{$fileExt}"; // save images to imagePath $file->saveAs($filePath.$fileName.".{$fileExt}"); return $file; } /** * fetch stored image file name with complete path * * @return string * @throws InvalidParamException */ public function getImagePath() { return $this->avatar ? \Yii::getAlias(\Yii::$app->getModule('userextended')->avatarPath).$this->avatar : null; } /** * fetch stored image url * * @return string * @throws InvalidParamException */ public function getImageUrl() { if ( !$this->avatar && $this->getAccountAttributes() !== null ) { $imageURL = $this->getSocialImage(); } else { $avatar = $this->avatar ?: 'default.png'; $imageURL = \Yii::getAlias(\Yii::$app->getModule('userextended')->avatarURL).$avatar; } return $imageURL; } /** * Process deletion of image * * @param string $avatarOld * * @return bool * @throws InvalidParamException */ public function deleteImage($avatarOld) { $avatarURL = \Yii::getAlias(\Yii::$app->getModule('userextended')->avatarPath).$avatarOld; // check if file exists on server if (empty($avatarURL) || !file_exists($avatarURL)) { return false; } // check if uploaded file can be deleted on server if (!unlink($avatarURL)) { return false; } // if deletion successful, reset your file attributes $this->avatar = null; return true; } /** * Get image form Social * * @return string */ public function getSocialImage() { $account = $this->getAccountAttributes(); switch($account['provider']) { case 'facebook': /** @var Account $account */ $imageURL = 'https://graph.facebook.com/' . $account['client_id'] . '/picture?type=large'; break; case 'twitter': /** @var Account $account */ $imageURL = ''; break; default: $imageURL = null; } return $imageURL; } /** * @return ActiveQueryInterface */ public function getAccount() { return $this->hasOne($this->module->modelMap['Account'], ['user_id' => 'user_id']); } /** * @return Profile [] */ public function getAccountAttributes() { return $this->hasOne($this->module->modelMap['Account'], ['user_id' => 'user_id'])->asArray()->one(); } } --- Добавлено --- мне нужно чтоб он забирал img с это модели.. B.-А что у тебя в модели? O.- можно ка кто ястнея задать вопрос,я ведь показал модель при создании темы.
С какого перепугу она что-то куда-то вставляет? Забирает и выбрасывает на помойку Ты показал, как ты создаёшь экземпляр модели Comment и сохраняешь его, а модель ты не показал
вот есть токая модель но она работает с админкой, . Может она при делах,.. Код (Text): <?php /** * Created by PhpStorm. * User: jessie * Date: 17.12.18 * Time: 4:34 */ namespace app\models; use Yii; use yii\web\UploadedFile; use yii\base\Model; class ImageUpload extends Model { public $image; public function rules() { return [ [['image'], 'required'], [['image'], 'file', 'extensions' => 'jpg,png,gif'] ]; } public function uploadFile(UploadedFile $file, $currentImage) { $this->image = $file; if ($this->validate()) { $this->deleteCurrentImage($currentImage); return $this->saveImage(); } } public function getFolder() { return Yii::getAlias('@web') . 'uploads/'; // return Yii::getAlias('@web') . 'img/users/'; } public function generateFilename() { return strtolower(md5(uniqid($this->image->baseName)) . '.' . $this->image->extension); } public function deleteCurrentImage($currentImage) { if ($this->fileExists($currentImage)) { unlink($this->getFolder() . $currentImage); } } public function fileExists($currentImage) { if (!empty($currentImage) && $currentImage != null) { return file_exists($this->getFolder() . $currentImage); } } public function saveImage() { $filename = $this->generateFilename(); $this->image->saveAs($this->getFolder() . $filename); return $filename; } }
да с понимание норм модель рабочия ,он просто не видит в базе profile если делать так $comment->user->profile->avatar = 'value'; то получаешь - Косвенная модификация перегруженного свойства app \ models \ User :: $ profile не имеет никакого эффекта
в модели профиля реализованна функция --- Добавлено --- Код (Text): public function uploadAvatar($filePath) { $file = UploadedFile::getInstance($this, 'avatar'); // if no file was uploaded abort the upload if ( null === $file ) { return false; } // file extension $fileExt = $file->extension; // purge filename $fileName = \Yii::$app->security->generateRandomString(); // update file->name $file->name = $fileName.".{$fileExt}"; // update avatar field $this->avatar = $fileName.".{$fileExt}"; // save images to imagePath $file->saveAs($filePath.$fileName.".{$fileExt}"); return $file; } /** * fetch stored image file name with complete path * * @return string * @throws InvalidParamException */ public function getImagePath() { return $this->avatar ? \Yii::getAlias(\Yii::$app->getModule('userextended')->avatarPath).$this->avatar : null; } /** * fetch stored image url * * @return string * @throws InvalidParamException */ public function getImageUrl() { if ( !$this->avatar && $this->getAccountAttributes() !== null ) { $imageURL = $this->getSocialImage(); } else { $avatar = $this->avatar ?: 'default.png'; $imageURL = \Yii::getAlias(\Yii::$app->getModule('userextended')->avatarURL).$avatar; } return $imageURL; } /** * Process deletion of image * * @param string $avatarOld * * @return bool * @throws InvalidParamException */ public function deleteImage($avatarOld) { $avatarURL = \Yii::getAlias(\Yii::$app->getModule('userextended')->avatarPath).$avatarOld; // check if file exists on server if (empty($avatarURL) || !file_exists($avatarURL)) { return false; } // check if uploaded file can be deleted on server if (!unlink($avatarURL)) { return false; } // if deletion successful, reset your file attributes $this->avatar = null; return true; }
Эта строка меняет значение, которое находится в оперативе. А чтоб записалось в базу, надо явно вызвать save у profile