Yii XUpload无法保存到模型
您好,我正在使用XUpload在我的应用程序中上载照片,但是当它上载到相应的文件夹时,它无法保存到我的数据库。下面是添加照片的代码。模型是旋转木马。请帮忙。谢谢Yii XUpload无法保存到模型,yii,model,yii-xupload,Yii,Model,Yii Xupload,您好,我正在使用XUpload在我的应用程序中上载照片,但是当它上载到相应的文件夹时,它无法保存到我的数据库。下面是添加照片的代码。模型是旋转木马。请帮忙。谢谢 public function afterSave() { $this->addImages(); parent::afterSave(); } public function addImages() { //If we have pending images if( Yii::app( )->user->has
public function afterSave()
{
$this->addImages();
parent::afterSave();
}
public function addImages()
{
//If we have pending images
if( Yii::app( )->user->hasState('carouselphoto') ) {
$userImages = Yii::app( )->user->getState('carouselphoto');
//Resolve the final path for our images
$path = Yii::app( )->getBasePath( )."/../images/uploads/{$this->id}/";
//Create the folder and give permissions if it doesnt exists
if( !is_dir( $path ) ) {
mkdir( $path );
chmod( $path, 0777 );
}
//Now lets create the corresponding models and move the files
foreach( $userImages as $image ) {
if( is_file( $image["path"] ) ) {
if( rename( $image["path"], $path.$image["filename"] ) ) {
chmod( $path.$image["filename"], 0777 );
$img = new Carousel();
$img->size = $image["size"];
$img->mime = $image["mime"];
$img->name = $image["name"];
$img->photo_name = "/images/uploads/{$this->id}/".$image["filename"];
$img->id = $this->id;
if( !$img->save( ) ) {
//Its always good to log something
Yii::log( "Could not save Image:\n".CVarDumper::dumpAsString(
$img->getErrors( ) ), CLogger::LEVEL_ERROR );
//this exception will rollback the transaction
throw new Exception( 'Could not save Image');
}
}
} else {
//You can also throw an execption here to rollback the transaction
Yii::log( $image["path"]." is not a file", CLogger::LEVEL_WARNING );
}
}
//Clear the user's session
Yii::app( )->user->setState( 'carouselphoto', null );
}
}
谢谢乔·米勒。这是我的访问规则代码片段。但是,没有任何错误
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('photo_name', 'required'),
array('gallery_id', 'numerical', 'integerOnly'=>true),
array('photo_name, alt', 'length', 'max'=>256),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, photo_name, gallery_id, alt', 'safe', 'on'=>'search'),
array('photo_name', 'file', 'types' => 'jpg,jpeg,gif,png'),
);
}
这里是设置actionUpload的地方
//Now we need to save this path to the user's session
if( Yii::app( )->user->hasState('carouselphoto') ) {
$userImages = Yii::app()->user->getState('carouselphoto');
} else {
$userImages = array();
}
$userImages[] = array(
"path" => $path.$filename,
//the same file or a thumb version that you generated
//"thumb" => $path.$filename,
"filename" => $filename,
'size' => $model->size,
'mime' => $model->mime_type,
'name' => $model->name,
);
Yii::app( )->user->setState( 'carouselphoto', $userImages );
我有同样的问题,有一件事你不应该做的是打电话如果$img->save how@Joe Miller说你有一个递归 我尝试在分配完后打印$img->name=$image['name'],但它不起作用 但这种变化并没有反映在数据库字段上。。不知道为什么 我打算这样做:
if(Yii::app()->user->hasState('xuploadFiles' )) {
$this->addImages();
return true;
}parent::afterSave();
因为可能返回parent::afterSave;始终返回false,这样它就不会保存字段。
我不知道。。我需要先检查一下
解决了的
对我来说,这样做:$this->saveAttributesarray'image_url'
如果对你有效,你应该试试。
$this->saveAttributesarray'size,mime,name,photo_name,id' 嗨,欢迎来到这个网站!您可以添加模型验证规则吗?另外,您是否会生成任何错误?你试过调试吗?只是一个想法,但是上面的代码是来自你的模型转盘吗?如果是这样的话,在afterSave方法上有递归;如果出现以下情况,将再次在线调用它$img->save。我不确定会有什么影响,但不可能是好的!您还可以在控制器中显示上载操作吗?这就是setState'carouselphoto'所在的位置。解决这个问题的唯一方法是在IDE中使用调试器。您需要在if!之后逐行执行命令$img->save,然后检查$img属性。它应该有一个属性“errors”,它将为您提供有关模型未保存原因的更多信息。如果你能在这里发布输出,那将是一个很大的帮助。我应该在哪里添加呢?在控制器或模型中?如果需要,请删除所有这些$img->save{}并使用这个$this->saveAttributesarray'size,mime,name,photo_name,id';我做到了,但仍然没有改变。它没有保存到数据库中