我分别完成了这两个任务,但现在我无法在一个地方使用这两个任务。
问题陈述:
我有一个表名Business_items
,具有表business
和items
的外键。在模型类中,是关系函数。
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'business' => array(self::BELONGS_TO, 'Business', 'business_id'),
'items' => array(self::BELONGS_TO, 'Items', 'items_id'),
'itemReviews' => array(self::HAS_MANY, 'ItemReview', 'business_items_id'),
);
}
好的,在创建业务页面中,我有两个字段,
business name, items name
和第三件事,是upload image
。这两个字段都是可搜索的下拉列表。我正在借助外键获取公司名称和商品名称。这样我就可以看到business_items
中曾经用作键的值。我通过更改此代码来做到这一点。 public function actionCreate()
{
$model=new PackageItems;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['PackageItems']))
{
$temp=$model->items_id=$_POST['PackageItems']['items_id']; //items_id is a multiple list field
foreach($temp as $t)
{
$model->unsetAttributes();
$model->setIsNewRecord(true);
$model->package_id=$_POST['PackageItems']['package_id']; //package_id is a repeated field
$model->items_id=$t;
$model->insert();
}
if($model->save())
$this->redirect(array('admin','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
所以我想要我真正完成的是
id ----项目----包装
1 ------蛋糕-----买一送一
2 -----糕点-----买一送一
第二部分:
我知道如何在yii中上传图片,我点击了此链接,并成功运行了
http://www.yiiframework.com/wiki/349/how-to-upload-image-photo-and-path-entry-in-database-with-update-functionality/
现在分开的问题是我想要这样的东西
id----items----package-----------------------image
1------cake-----buy one get one free------1.jpg
2----- pastry-----buy one get one free------1.jpg
但问题是
public function actionCreate()
{
$model=new Banner; // this is my model related to table
if(isset($_POST['Banner']))
{
$rnd = rand(0,9999); // generate random number between 0-9999
$model->attributes=$_POST['Banner'];
$uploadedFile=CUploadedFile::getInstance($model,'image');
$fileName = "{$rnd}-{$uploadedFile}"; // random number + file name
$model->image = $fileName;
if($model->save())
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../banner/'.$fileName); // image will uplode to rootDirectory/banner/
$this->redirect(array('admin'));
}
}
$this->render('create',array(
'model'=>$model,
));
}
我要如何同时使用这两个代码(使用外键代码和图片上传代码获取价值),我想上传图片以及使用其他代码从外表中获取其他值。
我知道它很复杂,但我需要帮助。
预先感谢和抱歉。
最佳答案
我不确定是否能正确解决您的问题,但据我了解,您想上传一个文件,并将此文件的名称插入您的一个数据库表中。如果是这样,解决方案可能如下:
首先,在数据库表中添加一个新字段,在其中存储文件名,还将其添加到ActiveRecord类和视图中。
然后添加您的代码以保存您的相关记录(我认为您的foreach循环适用于此)。
接下来,添加必要的代码以上传图像。在上传图像的代码中,您可以看到Yii会将文件字段视为普通文本字段,在其中您将存储上传的文件名。
最后,您应该保存模型,如果成功,则继续将文件保存在服务器中。
希望这可以帮助。
更新
我将放置一些代码以阐明答案。
您说第一部分对您有用,然后我将开始讨论。
您的模型PackageItems需要一个新字段,使其为image。
接下来,我假设用户填写了表单,所以我将跳过if和render部分
$temp=$model->items_id=$_POST['PackageItems']['items_id'];
$uploadedFile=CUploadedFile::getInstance($model,'image');//get uploaded image info
$rnd = rand(0,9999);
$fileName = "{$rnd}-{$uploadedFile}"; // random number + file name
$model->image = $fileName;//store the new file name in the model
foreach($temp as $t){
$model->unsetAttributes();
$model->setIsNewRecord(true);
$model->package_id=$_POST['PackageItems']['package_id'];
$model->items_id=$t;
$model->insert();
}
if($model->save()){
$uploadedFile->saveAs(Yii::app()->basePath.'/../yourPath/'.$fileName);//if the record was saved in the database, then proceed to save the image in the server
$this->redirect(array('admin','id'=>$model->id));
}
如果要上传多个文件,请检查此链接Yii 1.1: Uploading multiple images with CMultiFileUpload
关于mysql - 外键和在yii 1.1中上传照片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32554836/