我是yii的初学者。我正在建立一个基本结构 - 一名员工。我正在上传他们的图像并将图像名称保存在数据库中,并在我的视图页面上显示它。但我想将图像名称更改为员工ID。
对于Ex:
desert.jpg
更改为01.jpg
或任何其他员工ID。
请帮帮我。
控制器:
public function actioncreate(){
$dept_list = department_mod::model()->dept_list();
$data['dept_list'] = CHtml::listData($dept_list,'value', 'text');
$team_list = team_mod::model()->team_list();
$data['team_list'] = CHtml::listData($team_list,'value', 'text');
$des_list = employee_mod::model()->des_list();
$data['des_list'] = CHtml::listData($des_list,'value', 'text');
$report_to_list = report_to_mod::model()->report_to_list();
$data['report_to_list'] = CHtml::listData($report_to_list,'value', 'text');
$model_upload = new upload_mod;
$data['model_upload'] = $model_upload;
$this->render('create',$data);
}
public function actionsave(){
$q = Employee_mod::model()->data_save($_POST);
$data = $_POST;
$exp = 'profile';
$emp_upload_path = 'assests/images/';
$p = $emp_upload_path;
echo $p;
if (!is_dir($p)) {
mkdir($p,0777);
}
$path = $p.$exp.'/';
if (!is_dir($path)) {
mkdir($path,0777);
}
$model_upload = new upload_mod;
$file_name = "";
$model_upload->upload_file = CUploadedFile::getInstance($model_upload,'userfile');
if($model_upload->validate()) {
$model_upload->upload_file->saveAs($path . $model_upload->upload_file);
$file_name = $model_upload->upload_file;
}else{
var_dump($model_upload->getErrors());
}
$data['file_name'] = $file_name;
$q = Employee_mod::model()->imagesave($q , $file_name);
$this->redirect(array('employee/list'));
MODEL:
function data_save($data)
{
$input = array(
'first_name' => $data['first_name'],
'middle_name' => $data['middle_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'cnic' => $data['cnic'],
'dob' => $data['dob'],
'department' => $data['dept_id'],
'location' => $data['location'],
'created_by' => 'admin',
'team' => $data['id'],
'des' => $data['des_id'],
'report_to' => $data['id'],
'joining_date' => $data['joining_date'],
);
$q = Yii::app()->db->createCommand();
$q->insert('emp',$input);
$insert_id = Yii::app()->db->getLastInsertID();
return $insert_id;
}
function imagesave($empno , $file_name)
{
$file = array(
'image' => $file_name
);
$q = Yii::app()->db->createCommand();
$q->update('emp', $file, 'empno=' . $empno);
}
function imageeditsave($empno , $file_name)
{
$file = array(
'image' => $file_name
);
$q = Yii::app()->db->createCommand();
$q->update('emp', $file, 'empno=' . $empno);
}
function data_editsave($data,$emp_no)
{
$input = array(
'first_name' => $data['first_name'],
'middle_name' => $data['middle_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'cnic' => $data['cnic'],
'dob' => $data['dob'],
'department' => $data['dept_id'],
'location' => $data['location'],
'created_by' => $data['created_by'],
'team' => $data['id'],
'des' => $data['des_id'],
'report_to' => $data['id'],
'joining_date' => $data['joining_date'],
);
$q = Yii::app()->db->createCommand();
$q-> update('emp',$input,'empno =' . $emp_no);
$insert_id = Yii::app()->db->getLastInsertID();
return $insert_id;
}
答案 0 :(得分:0)
问题在于:
$q = Yii::app()->db->createCommand();
$q->update('emp', $file, 'empno=' . $empno);
update
是一个ActiveRecord方法,您正在将它与createCommand一起使用!您需要将该代码更改为:
Employee_mod::model()->updateByPk($empno, array('image' => $file_name));