Yii Framework Forum: CListView - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CListView trouble between 2 tables

#1 User is offline   Richi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-December 14

Posted 07 December 2014 - 11:29 PM

So guys here comes that line that you read all the time:

I'm new with yii and i need help please


So that said this is my case:

I have a table User and a table Survey in the same database. The user create surveys and when it happens the user saves his id in the table survey. Untill there everything fine. The trouble appears when i try to show the surveys with the crud list surveys. I will see all the surveys of the data base and not only the surveys that the user created.

i*ve been trying with something like this code but i get errors all the time.

if (Yii::app()->user->id == Yii::app()->survey->id){
... Show list
}


I was thinking as well to go trough the data provider searching for the data that has the id of the user (Yii::app()->user->id) save that in an extra array and then send it to the CListView. I'm understanding wrong how the ClistView works or i'm just totally lost on what i said?

$this->widget('zii.widgets.CListView', array('dataProvider'=>$dataProvider, //here is the problem, i need an array with the surveys of the user that is logged in

'itemView'=>'_view',

'sortableAttributes'=>array(
'title',
'start_date',
'end_date',
),




Any advice?
0

#2 User is offline   alirz23 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,245
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 08 December 2014 - 01:35 AM

you are on the right track here

Quote

I was thinking as well to go trough the data provider searching for the data that has the id of the user (Yii::app()->user->id) save that in an extra array and then send it to the CListView. I'm understanding wrong how the ClistView works or i'm just totally lost on what i said?


here is an example


<?php
public function actionMySurvey()
{
	// currently logged in user id
	$userID = Yii::app()->user->id;
	// create a new criteria
	$criteria = new CDbCriteria;
	// compare the userid with logged in user id
	$criteria->compare('user_id', $userID);

	$dataProvider=new CActiveDataProvider('Survey',array(
			'criteria'=>$criteria
	));

	$this->render("mySurvey", array("dataProvider" => $dataProvider));
}

0

#3 User is offline   Richi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-December 14

Posted 28 December 2014 - 04:33 PM

when i try to put that code i become some sort of errors. This is the whole code of that site. When i activate this lines

$criteria = new CDbCriteria;
$dataProvider = new CActiveDataProvider('Survey',array('criteria'=>$criteria)

i get this error.
syntax error, unexpected T_VARIABLE in line 37 ( where the dataProvider comes into the widget)


What i'm doing wrong?


<?php
/* @var $this SurveyController */
/* @var $dataProvider CActiveDataProvider */

/* here we have to make a search of the list of surveys of the user that is logged in . This should help if (Yii::app()->user->id == Yii::app()->survey->id)*/


$this->breadcrumbs=array(
'Surveys',



);

$this->menu=array(
array('label'=>'Create Survey', 'url'=>array('create')),
array('label'=>'Manage Survey', 'url'=>array('admin')),
);
?>
<h1>Surveys</h1>

<?php


$userID = Yii::app()->user->id;

// create a new criteria
$criteria = new CDbCriteria;

// compare the userid with logged in user id
$criteria->compare('user_id', $userID);

$dataProvider = new CActiveDataProvider('Survey',array('criteria'=>$criteria)

// $this->render("mySurvey", array("dataProvider" => $dataProvider,itemView'=>'_view','sortableAttributes'=>array ('title','start_date','end_date' ) );

$this->widget ('zii.widgets.CListView', array ('dataProvider'=>$dataProvider,'itemView'=>'_view','sortableAttributes'=>array ('title','start_date','end_date' ) ) );


?>
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users