PDA

View Full Version : اضافه شدن دو رکورد هنگام ذخیره سازی



googoli
چهارشنبه 02 مهر 1393, 10:05 صبح
با سلام
من هنگامی که می خوام یک رکورد ذخیره میشه به جای اینکه یه رکورد ذخیره کنه دو رکورد رو ذخیره می کنه
هیچ کد اضافی هم توی کنترلرم ندارم که بگم اون داره این کار رو انجام میده
میشه بگید چطور میشه این اشکال رو برطرف کرد ؟

Veteran
چهارشنبه 02 مهر 1393, 10:18 صبح
کدتون رو قرار بدید

googoli
چهارشنبه 02 مهر 1393, 10:28 صبح
این کد کنترلر من هست


<?php

class MaterialinfoController extends Controller
{

public $mat_id ;
private $_assetsBase;
private $forceCopyAssets;
public $print = 0 ;
public $update = 0 ;

public function init()
{
$this->registerCoreCss();
$this->registerScript();
}

public function getAssetsBase()
{
if ($this->_assetsBase === null) {
$this->_assetsBase = Yii::app()->getAssetManager()->publish(
Yii::getPathOfAlias('application.assets'),
false,
-1,
defined('YII_DEBUG') && YII_DEBUG
);
}
return $this->_assetsBase;
}

protected function registerCoreCss()
{
$css_arr = array('bootstrap.css','main.css','styles.css','tab le.css');
foreach($css_arr as $filename)
Yii::app()->clientScript->registerCssFile($this->getAssetsBase().'/css/'.$filename);
}

protected function registerScript(){
$js_arr = array('bootstrap.min.js','menu.js'); //put what js file name that you need to import from admin assets folder
foreach($js_arr as $filename){
Yii::app()->getClientScript()->registerScriptFile($this->getAssetsBase().'/js/'.$filename);
}
}

/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout='//layouts/column2';

/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}

/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','ChangeDropdown'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','ChangeDropdown'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}

public function actionChangeDropdown()
{

if(isset($_POST['id']))
{
$id = $_POST['id'];
$criteria = new CDbCriteria() ;
$criteria->addCondition('Hall='.$id);
$data = Material::model()->findAll($criteria);
echo '<option value="">--یک مورد را انتخاب کنید--</option>';
foreach ($data as $material)
echo '<option value="'.$material->id.'">'.$material->name.'</option>' ;


}
}

/**
* Displays a particular model.
* @param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}

/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Materialinfo;

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['Materialinfo']))
{
$model->attributes=$_POST['Materialinfo'];
if($model->save())
$this->redirect(array('admin'));
}

$this->render('create',array(
'model'=>$model,
));
}

/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$this->update = 1 ;
$model=$this->loadModel($id);


// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['Materialinfo']))
{
$model->attributes=$_POST['Materialinfo'];
if($model->save())
$this->redirect(array('admin'));
}

$this->render('update',array(
'model'=>$model,
));
}

/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
$this->loadModel($id)->delete();

// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}

/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Materialinfo');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}

/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new Materialinfo('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Materialinfo']))
$model->attributes=$_GET['Materialinfo'];

$this->render('admin',array(
'model'=>$model,
));
}

/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer $id the ID of the model to be loaded
* @return Materialinfo the loaded model
* @throws CHttpException
*/
public function loadModel($id)
{
$model=Materialinfo::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}

/**
* Performs the AJAX validation.
* @param Materialinfo $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='materialinfo-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}




این هم کد مدل



<?php

/**
* This is the model class for table "{{materialinfo}}".
*
* The followings are the available columns in table '{{materialinfo}}':
* @property integer $id
* @property string $Date
* @property integer $productionmix_id
* @property integer $Material_id
* @property string $Hall
* @property double $Buy
* @property double $Minus
* @property double $Stock
*
* The followings are the available model relations:
* @property Material $material
*/
class Materialinfo extends CActiveRecord
{
const HALL_PVC=1000;
const HALL_EVA = 1001;
/**
* @return string the associated database table name
*/
public function tableName()
{
return '{{materialinfo}}';
}

/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('productionmix_id, Material_id', 'numerical', 'integerOnly'=>true),
array('Buy, Minus, Stock', 'numerical'),
array('Date, Hall', 'length', 'max'=>255),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, Date, productionmix_id, Material_id, Hall, Buy, Minus, Stock', 'safe', 'on'=>'search'),
);
}

/**
* @return array relational rules.
*/
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(
'material' => array(self::BELONGS_TO, 'Material', 'Material_id'),
);
}

/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'Date' => 'Date',
'productionmix_id' => 'Productionmix',
'Material_id' => 'Material',
'Hall' => 'Hall',
'Buy' => 'Buy',
'Minus' => 'Minus',
'Stock' => 'Stock',
);
}

/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.

$criteria=new CDbCriteria;

$criteria->compare('id',$this->id);
$criteria->compare('Date',$this->Date,true);
$criteria->compare('productionmix_id',$this->productionmix_id);
$criteria->compare('Material_id',$this->Material_id);
$criteria->compare('Hall',$this->Hall,true);
$criteria->compare('Buy',$this->Buy);
$criteria->compare('Minus',$this->Minus);
$criteria->compare('Stock',$this->Stock);

return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}

/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Materialinfo the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}

public function getTypeHall()
{
return array(
self::HALL_PVC=>'سالن PVC',
self::HALL_EVA=>'سالن EVA',

);
}

public function getTypeText()
{
$typeOptions=$this->getTypeHall();
return isset($typeOptions[$this->Hall]) ? $typeOptions[$this->Hall] : "unknown type ({$this->Hall})";
}

}

Veteran
پنج شنبه 03 مهر 1393, 21:08 عصر
از اینجا که نتونستم متوجه مشکل بشم :متفکر: تیم ویوور بدید،ببینیم مشکل از چیه :متفکر:

SlowCode
شنبه 05 مهر 1393, 23:57 عصر
فکر نمیکنم کد شما مشکلی داشته باشه!
تو یکی از پروژه های منم این اتفاق افتاد، موقع دانلود کردن یه فایل آمار رو تصاعدی ثبت میکرد، گاهی اوقات 3 تا گاهی اوقات 5 تا:لبخندساده:
آخرش هم دلیلش رو نفهمیدم! اگه کسی اطلاع داشت به ما هم بگه.