everyone I am working on a project that allow user do upload and download data. I want to show only a user's own uploaded data in CGridView.
Here's my relation table
I have tried to edit my page --> views/file/myUploads.php
<?php
$isMine=Yii::app()->user->id; // I have tried this
if($isMine){
$this->widget('zii.widgets.CGridView',array(
'id'=>'file-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
'name'
'description',
'category'
'uploader'
'upload_date',
array(
'header'=>'Actions',
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{delete}', //'visible'=> (Yii::app()->user->getLevel()==1),
'deleteConfirmation'=>"js: 'Are you want to delete '+$(this).parent().parent().children(':first-child').text()+ '?'",
'buttons'=>array(
'delete' => array(
'visible'=> '$data->pengunggah==Yii::app()->user->id',
),
)
),
),
)); }
?>
I have tried that codes above but the page still display all data, not user's own data.
I have tried to edit myUpload function in FileController.php too
public function actionMyUpload()
{
$isMine=Yii::app()->user->id; // I have tried this
if($isMine){
$model=new File('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['File']))
$model->attributes=$_GET['File'];
$this->render('myUpload',array(
'model'=>$model,
));
}
}
But still display all data.
Can anyone help me? Thanks a lot.
Revert all your changes back, Open Your Model Class File ie "File.php" and add this line in your search() function:
$criteria=new CDbCriteria;
// Simply add this line
$criteria->addCondition('id_user='.Yii::app()->user->id);
Related
I have a search form. I would like to pagination the result.
$criteria something like this:
if($_POST["tipus"] != 4){
$criteria->compare('t.tipus',$_POST["tipus"],true);
}
if($_POST["varos"] != 0){
$criteria->compare('`apartman`.`city`', $_POST["varos"], true);
}
if($_POST["ferohely"] != 0){
$criteria->compare('t.ferohely', $_POST["ferohely"], true);
}
My dataprovider:
$dataProvider= new CActiveDataProvider('UserAndApartman', array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'t.id DESC',
),
'pagination'=>array(
'pageSize'=>2,
),
));}
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'kereses_eredmenyek_view',
));
So if i click on the second page, the post doesn't come again. How can i post it automatic?Can i get the $dataprovider with pagination?
You're using $_POST, the $_POST['tipus'] and other data is lost when
you're navigating to the next page, since you're not posting it to
page 2, 3 etc. You can do two things:
Store the $_POST['tipus'] in a session, so the next page 'knows' what year it has to use.
Store it in the url as a $_GET parameter, so instead of $_POST['tipus'] use $_GET['tipus']. When you navigate to page 2, the $_GET['tipus'] is available in the url on the next page.
The last one is the easiest one i think, and that's how i usually use it.
IF you use the get method, change the CActiveForm line to this:
<?php $this->beginWidget('CActiveForm', array(
'id'=>'fromid',
'method' => 'get'
)); ?>
I have modified my model so that the data displayed in cgridview is unique per user, depending on the account type...
However I need to create a form from another model where I could get the data from the cgridview via dropdown...
I used this code at first...
<?php
$this->widget('ext.select2.ESelect2',array(
'model'=>$model,
'attribute'=>'pr_id',
'data'=>$model->searchPatient(),//function to provide data
// or
//'data'=>CHtml::listData(PatientRecord::model()->findAll(), 'id', 'first_name')
);
?>
but it returns all of the contents of the PatientRecord model, I tried using a condition before planning to retrieve the contents from the cgridview...
$doctor= Yii::app()->user->id;
CHtml::listData(PatientRecord::model()->findAll( array(
'condition'=>'doctor_id=:doctor_id',
'params' => array(':doctor_id' => $doctor)
)
);), 'id', 'first_name')
it didn't have an error but it didn't display anything on the dropdown either...
any suggestions?
I think the problem is with a ; and ) in your model code, try this:
$doctor= Yii::app()->user->id;
CHtml::listData(PatientRecord::model()->findAll( array(
'condition'=>'doctor_id=:doctor_id',
'params' => array(':doctor_id' => $doctor)
)
), 'id', 'first_name');
You should always enable error logging in local environment, this will help you find any errors in your code. Here is a link on how to enable error logging.
Hope that helps :)
I have merged admin into create file by copying the gridview into create.php.
<?php $this->renderPartial('_form', array('model'=>$model)); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'center-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
It is saving data but the search functionality of grid view is not functioning. Whereas, the same code of CGridView is functioning in admin.php.
here is my controller:
public function actionCreate()
{
$model=new Center;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Center']))
{
$model->attributes=$_POST['Center'];
if($model->save())
$this->redirect(array('create','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
After the model is saved you are redirecting to the same url i.e create but without the data from $_POST. The solution is to remove the call since you are essentially loading the same view, and would like to keep some of the data from the previous page.
public function actionCreate()
{
$model=new Center;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Center']))
{
$model->attributes=$_POST['Center'];
/*if($model->save())
$this->redirect(array('create','id'=>$model->id));*/
}
$this->render('create',array(
'model'=>$model,
));
}
HOWEVER
Reusing the same model for searching and inserting isn't a good approach since you will retain the data for the model that has been entered into the database (including its primary key(s)). A better one would be to use separate variables for the search and create and different name and id prefixes for fields of both forms. Then you can pass both variables into the views, and something entered into one field will not affect the other. You can also reuse the code in actionAdmin to refresh your grid via ajax without having to send the create form too.
I made a small app: there is a lost of users , I check those who I want , click a button and a certain message is being sent to their emails.
I'm using yii's checkbox column and ajax in order to send the users' emails to the action in the controller.
Now, in order to be able to debug this app , I set the destination address to mine , and the users' emails in the message variable.
The app works! BUT , I get the users' ids instead of their emails.
Checkbox column sends the model's id by default , but one can change it with 'name' or 'value' , HOWERVER , for some reason both those lines simply doesn't work.
I'm sitting on this for four days , I wrote another ajax , but I'm still gettin the id.
I'd be vey gladful if anyone can help me. Here's the code:
The gridview:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'test-person-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
'email',
array(
'id'=>'col',
'class'=>'CCheckBoxColumn',
'selectableRows'=>'2',
'value'=>'$data->email',
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
The ajax:
<?php
Yii::app()->clientScript->registerScript('send','
$("#sender").click(function(){
var checked=$("#test-person-grid").yiiGridView("getChecked","col");
var count=checked.length;
if(count>0 && confirm(" do you want to send "+count+" message? "))
{
$.ajax({
data:{checked:checked},
url:"'.CHtml::normalizeUrl(array('testPerson/sender')).'",
success:function(data){$("#test-person-grid").yiiGridView("update",{});},
});
}
});
');
?>
The controller action:
public function actionSender()
{
if(Yii::app()->request->getIsAjaxRequest())
{
$checkedMails=$_GET['checked'];
foreach($checkedMails as $message)
{
mail('mark#gmail.com', 'Test mail', $message);
}
}
}
You have the userID so I would do a findByPK on in the controller.
This question on the YII forum is the same and the answer given by the staff is the same.
http://www.yiiframework.com/forum/index.php/topic/48235-fnyiigridviewgetchecked-returns-empty-values/
Thanks! Finally it works.
I used only one foreach , and under it I used the findByPk like this:
$checkedIds=$_GET['checked'];
foreach($checkedIds as $id)
{
$message=TestPerson::model()->findByPk($id)->email;
mail('mark#gmail.com', 'Test mail', $message);
}
Cheers,
Mark.
I have CListView and inside im redering a view that has a button, when clicked it opens a CJUIDialog.
But when i go through to the next page with the page controller. The CJUIDialog content loads to the page without clicking the button.
Any idea why it's coming like that?
It'll be great if anyone can help me out.
Thanks!
Ok, Yii generate the ids for lot of controls in an automatic way, so to avoid collision with events I recommend you to take out the interaction handling out of the item view in the following way:
In the page where the CListView is generated:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_post', // refers to the partial view named '_post'
'sortableAttributes'=>array(
'title',
'create_time'=>'Post Time',
),
));
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'dialog',
'options'=>array(
'title'=>'Dialog',
'autoOpen'=>false,
),
));
$this->endWidget('zii.widgets.jui.CJuiDialog');
In the item view page:
echo CHtml::htmlButton('Button',array('onclick' => '$("#dialog").dialog("open");'));
In case you need to do something with the data row (like using the id property of that data), you can create a custom javascript function that will receive the data when the button is clicked.
echo CHtml::htmlButton('Button',array('onclick' => 'myFunction('.$data->id.')'));
And the previous example then would be:
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_post', // refers to the partial view named '_post'
'sortableAttributes'=>array(
'title',
'create_time'=>'Post Time',
),
));
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'dialog',
'options'=>array(
'title'=>'Dialog',
'autoOpen'=>false,
),
));
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
<script type="text/javascript">
function myFunction(id) {
// you can put whatever you need inside the dialog
$("#dialog").html(id);
// open the dialog
$("#dialog").dialog("open");
}
</script>