Even i tried using this
$array = Yii::app()->db->createCommand(‘SELECT * FROM tbl_project_user’)->queryAll();
instead of accessing the model directly. Even with this it is throwing the same error.
Hi,
do you have a div with id=user_id?
//$model=new ProjectUser;
//$posts=User::model()->findAll();
I think it should be wrong, i assume you got a model name ProjectUser, i think you show write like this:
$modelProjectUser = new ProjectUser;
$posts = $modelProjectUser::model()->findAll();
Hi hoonglc,
I have mentioned like even if i use anyone of those lines it is throwing error
$model=new ProjectUser; or $posts=User::model()->findAll();
If i call a model or instantiate a model or execute a query like this it is throwing error
$array = Yii::app()->db->createCommand(‘SELECT * FROM tbl_project_user’)->queryAll();
Hi raotwits,
I don;t see any error in your view to declare the dropdownlist, mostly error is from controller. I don’t like to use DAO method, i suggest to use AR. use gii generate a model for table tbl_project_user, and the model file should be like TblProjectUser.php inside your model folder. In your controller
public function actionAssign()
{
if(isset($_POST['project_id']))
{
$modelTblProjectUser =new TblProjectUser;
$array = $modelTblProjectUser->getAllInfo();
foreach($array as $projectInfo){
echo $projectInfo['fieldname you want'];
}
}
in model TblProjectUser (TblProjectUser.php), add code
public function getAllInfo(){
$result = $this->model()->findAll();
return = $result;
}
Please post your complete view/controller/model and also the error that’s being thrown. Did you generate your model with gii?
view code
<?php
$this->breadcrumbs=array(
'Users'=>array('index'),
$model->user_id,
);
$this->menu=array(
array('label'=>'List User', 'url'=>array('index')),
array('label'=>'Create User', 'url'=>array('create')),
array('label'=>'Update User', 'url'=>array('update', 'id'=>$model->user_id)),
array('label'=>'Delete User', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->user_id),'confirm'=>'Are you sure you want to delete this item?')),
array('label'=>'Manage User', 'url'=>array('admin')),
);
?>
<div id="wrapper">
<div id="content">
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'project-assign-form',
'enableAjaxValidation'=>false,
)); ?>
<p><b>Assign User to Project</b></p>
<br>
Project
<?php
//Project::model()->Project_id = 2;
echo CHtml::activeDropDownList(Project::model(),'Project_id', CHtml::listData(Project::model()->findAll(), 'Project_id', 'Project_Name'), array('prompt'=>'--Select Project--',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('user/assign'),
'update' => '#nuser_id',
'data'=>array('project_id'=>'js:this.value'),
)
));
?>
<br>
<br>
<div style="float:left">
Select User
<?php
echo $form->listBox($model,'user_id',CHtml::listData(User::model()->findAll(),'user_id','user_name'),array('multiple'=>'multiple','size'=>'10','class'=>'multiselect'));//,'options' => array('1'=>array('selected'=>true))
echo CHtml::dropDownList('nuser_id','', array(""=>""));
?>
</div>
<!--
<script type="text/javascript"
src="http://jqueryui.com/themeroller/themeswitchertool/">
</script>
<div id="switcher"></div>-->
<div class="row buttons" style="float:left">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
</div>
</div>
–Controller Action Code–
public function actionAssign()
{
if(isset($_POST['project_id']))
{
$array = Yii::app()->db->createCommand('SELECT id,B.user_id,user_name FROM tbl_project_user A, tbl_user B where A.user_id and B.user_id and project_id ='.$_POST['project_id'])->queryAll();
$data=CHtml::listData($array,'user_id','user_name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
exit;
/*
$model=$this->loadModel($_POST['project_id']);
//$posts=User::model()->findAll();
echo 'HERE';
exit;
$data = ProjectUser::model()->findAll('project_id=:project_id',
array(':project_id'=>(int) $_POST['project_id']));
*/
}
$model=new ProjectUser;
if(isset($_POST['ProjectUser']) && isset($_POST['Project']))
{
foreach ($_POST['ProjectUser']['user_id'] as $user_id) {
$puser = $model->find('project_id=:project_id AND user_id=:user_id',
array(
':project_id'=>$_POST['Project']['Project_id'],
':user_id'=>$user_id,
));
if(count($puser) == 0)
{
$model->attributes=array("project_id" => $_POST['Project']['Project_id'],"user_id" => $user_id);
if($model->save())
echo 'Inserted';
}
elseif(count($puser) > 0 && $puser['is_active'] == 'No')
{
$puser->is_active= 'Yes';
if($puser->saveAttributes(array('is_active')))
echo 'Updated';
}
}
}
$this->render('assign',array(
'model'=>$model,
));
}
—Model Code—
<?php
/**
* This is the model class for table "tbl_user".
*
* The followings are the available columns in table 'tbl_user':
* @property integer $user_id
* @property string $user_name
* @property string $user_login
* @property string $user_password
* @property string $user_rights
* @property integer $employee_id
* @property string $is_active
* @property string $created
*/
class User extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return User the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_user';
}
/**
* @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('user_name, user_login, user_password, user_rights, employee_id', 'required'),
array('employee_id', 'numerical', 'integerOnly'=>true),
array('user_name', 'length', 'max'=>30),
array('user_login, user_password', 'length', 'max'=>25),
array('user_rights,is_active,created', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('user_id, user_name, user_login, user_password, user_rights, employee_id, is_active, created', '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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'user_id' => 'User ID',
'user_name' => 'User Name',
'user_login' => 'User Login',
'user_password' => 'User Password',
'user_rights' => 'User Rights',
'employee_id' => 'Employee ID',
'is_active' => 'Active',
'created' => 'Created',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('user_id',$this->user_id);
$criteria->compare('user_name',$this->user_name,true);
$criteria->compare('user_login',$this->user_login,true);
$criteria->compare('user_password',$this->user_password,true);
$criteria->compare('user_rights',$this->user_rights,true);
$criteria->compare('employee_id',$this->employee_id);
$criteria->compare('is_active',$this->is_active,true);
$criteria->compare('created',$this->created,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* perform one-way encryption on the password before we store it in the database
*/
protected function afterValidate()
{
parent::afterValidate();
$this->user_password = $this->encrypt($this->user_password);
}
public function encrypt($value)
{
return md5($value);
}
}