Yii: return a variable from model to form with Ajax -
how change model send varibale model form ajax? want can happen in model in ( public function comp_group ) connect form. want model return $maxid _form.php
this _form.php
<?php /* @var $this textcontroller */ /* @var $model text */ /* @var $form cactiveform */ ?> <div class="form"> <?php $form=$this->beginwidget('cactiveform', array( 'id'=>'text-form', // please note: when enable ajax validation, make sure corresponding // controller action handling ajax validation correctly. // there call performajaxvalidation() commented in generated controller code. // see class documentation of cactiveform details on this. 'enableajaxvalidation'=>true, 'focus'=>array($model,'group'), // 'clientoptions'=>array( // 'validateonchange'=>true, // default. validate when input changes // 'validateontype'=>'true', // validate every keystroke, hooray! // 'validationdelay'=>10, // not related post--but cool! // // default delay 200 ms // ), )); ?> <p class="note">fields <span class="required">*</span> required.</p> <?php echo $form->errorsummary($model); ?> <div class="row"> <?php echo $form->labelex($model,'subject'); ?> <?php echo $form->textfield($model,'subject',array('size'=>60,'maxlength'=>80)); ?> <?php echo $form->error($model,'subject'); ?> </div> <div class="row"> <?php echo $form->labelex($model,'matn'); ?> <?php echo $form->textfield($model,'matn',array('size'=>60,'maxlength'=>255)); ?> <?php echo $form->error($model,'matn'); ?> </div> <div class="row"> <?php echo $form->labelex($model,'group'); ?> <?php echo $form->textfield($model,'group',array('size'=>20,'maxlength'=>20)); ?> <?php echo $form->error($model,'group'); ?> </div> <?php $userx=yii::app()->session['idx']; ?> <div class="row"> <?php // echo $form->labelex($model,'user_id'); ?> <?php echo $form->textfield($model,'user_id',array ('value'=>$userx,'type'=>"hidden")); ?> <?php // echo $form->error($model,'user_id'); ?> </div> <div class="row"> <?php echo $form->labelex($model,'privacy'); ?> <?php echo $form->dropdownlist($model,'privacy', array('1'=>'public','2'=>'only me')); ?> <?php echo $form->error($model,'privacy'); ?> </div> <div class="row buttons"> <?php echo chtml::submitbutton($model->isnewrecord ? 'create' : 'save'); ?> </div> <?php $this->endwidget(); ?> </div><!-- form --> <?php $sqlname='show columns `group`'; $gpnamet = yii::app()->db->createcommand($sqlname); $gpall =$gpnamet->queryall(); $sqlnumber="select count(*) information_schema.columns table_name = 'group' "; $sqlnumberquery= yii::app()->db->createcommand($sqlnumber); $sqlnumberall=$sqlnumberquery->queryall(); $numbercolumns=( $sqlnumberall[0]['count(*)']); ($t=1 ; $t<$numbercolumns ;$t++) { $columnsarray[]=($gpall[$t]['field']); } print_r($columnsarray); //$mastercommand = yii::app()->db->createcommand(); ?>
this model:
/** * model class table "text". * * followings available columns in table 'text': * @property integer $id * @property string $subject * @property string $matn * @property string $group * @property integer $user_id * @property integer $privacy * * followings available model relations: * @property users $user */ class text extends cactiverecord { /** * @return string associated database table name */ public function tablename() { return 'text'; } /** * @return array validation rules model attributes. */ public function rules() { // note: should define rules attributes // receive user inputs. return array( array('subject, user_id, privacy ,group', 'required'), array('user_id, privacy', 'numerical', 'integeronly'=>true), array('subject', 'length', 'max'=>80), array('matn', 'length', 'max'=>255), array('group', 'length', 'max'=>20 , 'min'=>2), array('group', 'comp_group'), // following rule used search(). // @todo please remove attributes should not searched. array('id, subject, matn, group, user_id, privacy', 'safe', 'on'=>'search'), ); } public function getidx() { $user= yii::app()->db->createcommand(); // select users.username users inner join text on text.user_id=users.id // select users.username users, text users.id=text.user_id; $user = yii::app()->db->createcommand() ->select('users.id') ->from('users') ->where('users.id=:username' , array(':username'=>1)) // ->where('id=:id', array(':id'=>$id)) ->queryrow(); yii::app()->session['idx']=$user; return $user; } /** * @return array relational rules. */ public function relations() { // note: may need adjust relation name , related // class name relations automatically generated below. return array( 'user' => array(self::belongs_to, 'users', 'user_id'), ); } /** * @return array customized attribute labels (name=>label) */ public function attributelabels() { return array( 'id' => 'id', 'subject' => 'subject', 'matn' => 'matn', 'group' => 'group', 'user_id' => 'user', 'privacy' => 'privacy', ); } /** * retrieves list of models based on current search/filter conditions. * * typical usecase: * - initialize model fields values filter form. * - execute method cactivedataprovider instance filter * models according data in model fields. * - pass data provider cgridview, clistview or similar widget. * * @return cactivedataprovider data provider can return models * based on search/filter conditions. */ public function search() { // @todo please modify following code remove attributes should not searched. $criteria=new cdbcriteria; $criteria->compare('id',$this->id); $criteria->compare('subject',$this->subject,true); $criteria->compare('matn',$this->matn,true); $criteria->compare('group',$this->group,true); $criteria->compare('user_id',$this->user_id); $criteria->compare('privacy',$this->privacy); return new cactivedataprovider($this, array( 'criteria'=>$criteria, )); } /** * returns static model of specified ar class. * please note should have exact method in cactiverecord descendants! * @param string $classname active record class name. * @return text static model class */ public static function model($classname=__class__) { return parent::model($classname); } public function comp_group($attributes , $params) { $sqlname='show columns `group`'; $gpnamet = yii::app()->db->createcommand($sqlname); $gpall =$gpnamet->queryall(); $sqlnumber="select count(*) information_schema.columns table_name = 'group' "; $sqlnumberquery= yii::app()->db->createcommand($sqlnumber); $sqlnumberall=$sqlnumberquery->queryall(); $numbercolumns=( $sqlnumberall[0]['count(*)']); ($t=1 ; $t<$numbercolumns ;$t++) { $columnsarray[]=($gpall[$t]['field']); } //print_r($columnsarray); foreach ($columnsarray $u) { $mastercommand = yii::app()->db->createcommand(); $gname=$this->group; if($this->group == $u) { $this->adderror('group' ," $gname registered"); $mastercommand->reset(); $mastercommand->insert('group', array ( $this->group=>'1', )); $maxid = $mastercommand->select('max(id)')->from('group')->queryall(); $maxid= $maxid[0]['max(id)']; break; } else { $mastercommand->reset(); $mastercommand->addcolumn('group' , $gname , 'int(1)'); $mastercommand->reset(); $mastercommand->insert('group', array( $this->group=>'1', )); // break; $maxid = $mastercommand->select('max(id)')->from('group')->queryall(); $maxid= $maxid[0]['max(id)']; } } } }
Comments
Post a Comment