yii parent child Clistview paging issue - php

I am using Yii framework of php. My scenario is I've number of projects and all the projects have posts. For projects i used Clistview and in the 'itemView' of this control another page is specified named '_post'. In '_post' page again ClistView is specified for showing post details.
But the problem is when paging occur in post details Clistview of the projects and changing the page number of the one post detail to next page number, all the post detail clistview page number changes.
I have also specified post detail clistview 'id' but in no vain.
For Projects:
<?php
$this->pageTitle=Yii::app()->name . ' - Project Post Details';
echo '<div class="listViewBorder">';
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$dataProvider,
'id'=>'projectListView',
'itemView'=>'_post', // refers to the partial view
'enablePagination'=>true,
));
echo '</div><br />'
?>
For Post detail:
<?php
echo '<h2>Project: '. CHTML::encode($data->title).' </h2>';
echo '<div class="listViewBorder">';
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$this->CallProjectPosts($data->id),
'id'=>'postListView'.$data->id,
'itemView'=>'_postDetail',
'enablePagination'=>true,
));
echo '</div>'
?>

This is how i finally got it working, it will work until you find a better solution.
i'm assuming that project_id is a foreign key in your posts table.
Which will generate(using gii) the necessary HAS_MANY relation in your project model, which in turn enables us to access the posts of a project easily, without calling the CallProjectPosts method.
So modify your Post view(_post.php):
<?php
echo '<h2>Project: '. CHTML::encode($data->title).' </h2>';
echo '<div class="listViewBorder">';
$relatedPosts=new CArrayDataProvider($data->posts, // this is where the HAS_MANY relation comes into play
array(
'pagination'=>array(
'pageSize'=>1, // whatever your size was
)
)
);
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$relatedPosts,
'id'=>'postListView'.$data->id,
'itemView'=>'_postDetail',
'enablePagination'=>true,
));
echo '</div>'
?>
For Project list :
<?php
$this->pageTitle=Yii::app()->name . ' - Project Post Details';
echo '<div class="listViewBorder">';
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$dataProvider,
'id'=>'projectListView',
'itemView'=>'_post', // refers to the partial view
'enablePagination'=>true,
'ajaxUpdate'=>false
)
);
echo '</div><br />'
?>
As you will see, i have disabled ajaxUpdate for the project list view, if it is enabled, then the solution will not work, so if your requirement is to display/update the project list also through ajax then this will not work, currently only the project posts are updated through ajax.
Hope this helps.

Related

Can't display picture in CDetailView Yii 1.1

I am learning PHP Yii 1.1
I'm trying to upload picture while creating a new user and display the picture in the view (in CDetailView).
In uploading picture process, I could save the name of the pic in the database and save the picture in a folder called banner which is included in protected folder.
I want now to display the pic in the view, I tried all the solutions I've found, but still doesn't work with me.
<h1>View User #<?php echo $model->id; ?></h1>
<?php
//die(Yii::app()->baseUrl . '/protected/banner/' . $model->picture);
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'username',
'address',
'email',
array(
'name'=>'picture',
'type'=>'raw',
'value'=>CHtml::image(Yii::app()->baseUrl . '/protected/banner/' . $model->picture),
)
),
)); ?>
I checked the image URL by executing this command, and it gave me the right path of the saved image:
die(Yii::app()->baseUrl . '/protected/banner/' . $model->picture);
Access to protected directory is disallowed to protect all internal files of your applications. You need to create this directory outside of protected directory if you want to link to files inside of it:
array(
'name'=>'picture',
'type'=>'raw',
'value'=>CHtml::image(Yii::app()->baseUrl . '/banner/' . $model->picture),
)

Yii - CListView - External Sorting Links and Pagination

I'm using 'CListView' in my View page. I need to sort the CListView by external links like 'A-Z' or 'Z-A'. These links are present above the list.
Here is the code in my View page -
$model = new Whisky();
$dataProvider = $model->dashboard_whisky_list();
$this->widget('zii.widgets.CListView', array(
'id'=>'whisky-grid',
'dataProvider'=>$dataProvider,
'itemView'=>'_whisky_list',
'template'=>'{pager}{items}{pager}',
'ajaxUpdate'=>true,
'beforeAjaxUpdate' => 'js:function(id) {showLoader();}',
'afterAjaxUpdate' => 'js:function(id, data) {hideLoader();}',
));
My view contains 2 more CListViews.
Is it possible to sort by this way ? Also my pagination is not working by Ajax.
Click Here to see the screenshot.
Please help.

Need help to create CRUD screen with dropdown (relation) using yii-framework

I need to create CRUD screen using yii-framework. Simple CRUD screen using one table is working perfectly fine. I'm facing problem/issue while using dropdown (linking table).
I have installed giix extension which is suppose to create CRUD with dropdown if FK is specified but I dnt have MySql Engine InnoDB on my hosting provider, so I'm not able to use that extension. I need to do it manually.
I have two tables
main:-
id
store_id
prefix
store:-
id
name
Now store_id of main is FK to id of store table. And I want to create CRUD for main table.
So that Add Screen should show:-
Store Name:- Dropdown
prefix:- Textbox
View screen should use name column of store table instead of showing store_id
Thanks in anticipation.
Generate CRUD using Gii, then read my blog. http://jmmurphy.blogspot.com/2013/05/using-yii-model-relations.html
Basically you will have something like this for your store_id field after Gii Generation
<?php echo $form->labelEx($model,'store_id'); ?>
<?php echo $form->textField($model, 'store_id', array('size'=>60,'maxlength'=>255));?>
<?php echo $form->error($model,'store_id'); ?>
The textField line is replaced by:
<?php $list = CHtml::listData(Store::model()->findAll(), 'id', 'name'); ?>
<?php echo $form->dropDownList($model, 'store_id', $list, array('empty'=>'(Select a Store)')); ?>
You also need to define relations in your Main model so that you can access related tables (even if your database does not support foreign keys) like this:
public function relations()
{
return array(
'store'=>array(self::BELONGS_TO, 'Store', 'store_id'),
);
}
And to complete this relation, you should also add the following relation to your Store model:
public function relations()
{
return array(
'main'=>array(self::HAS_MANY, 'Main', 'store_id'),
);
}
These relations define a One to Many relation between Store and Main where store is the parent, and Main is the Child. To make it a One to One relationship change HAS_MANY to HAS_ONE. The HAS_* goes in the parent model and points to the foreign key attribute in the child table. The BELONGS_TO goes in the child and tells the attribute in the child table that points to the primary key in the parent.
Now to see the store name in the view action, you need to change 'store_id' in your view.php to an array that looks like:
array(
'name' => 'store_id',
'value' => $model->store->name,
)
The admin view is slightly different. I am not sure exactly why, but to view the store name instead of the id in the admin view you will need to use an array that looks like:
array(
'name' => 'store_id',
'value' => '$data->store->name',
)
Note that Gii generates this so that $data is the model instead of $model, and also it does a funky double indirection thing so that you need to put the variable declaration in single quotes.
Thanks jmarkmurphy for your help. You helped me lot and I have marked your answer as correct only as you gave me guidance. Just posting exact code in detail.
I changed view.php with below code:-
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'store.name',
'prefix',
),
));
Also changed admin.php with below code:-
<?php echo CHtml::encode($data->store->name); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'main-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'store.name',
'prefix',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Thanks once again jmarkmurphy. Thanks a lot . My application is now working and exactly the way I wanted.

CakePHP 2.0 - Pagination for an image gallery (with different item limits)

I want to build a small image gallery with CakePHP. Therefore I created an index view which displays a 3x3 table with image thumbnails. For pagination and sorting I added the following code:
ImagesController.php:
var $paginate = array(
'limit' => 9,
'order' => array('Image.id' => 'desc')
);
public function index() {
$data = $this->paginate('Image');
$this->set('images', $data);
}
index.ctp:
<?php echo $this->Paginator->sort('id', 'Id'); ?> |
<?php echo $this->Paginator->sort('name', 'Title'); ?>
...
<?php echo $this->Paginator->prev(' << Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->numbers(array('first' => 2, 'last' => 2)); ?>
<?php echo $this->Paginator->next('Next >> ', null, null, array('class' => 'disabled')); ?>
This works great. Now, when you click on a thumbnail a single image will be displayed using a view named 'view' (e.g. localhost/gallery/images/view/1). Like the index view, this view should have a pagination too, where the image with the id of 1 is currently selected and where you can navigate to the n previous and next images relative to the currently selected one. Moreover the images should be sorted by the same criteria as the pictures on the index view (e.g. by name). Is there an elegant way to solve this problem with CakePHP?
I'd approach this by passing the page number from the original pagination to the detail view.
If you were looking at Image ID 1 on page 2, your URL would look something like:
/gallery/images/view/1/2
That is:
/gallery/images/view/:id/:page
From this information, you can query the same paginated data from the index action on the specified page, and provide back and next navigation options from that data.

Yii CGridView - Custom Columns

been looking for a solution to add a feature for "Custom Columns"... Meaning, I present a list of columns that I can show the user and he selects the ones he wants to see and after the selection the table is updated and add/removes the needed columns.
Didn't find anything on Google (perhaps it has a different name than what I was looking for...)
Anyone has an Idea on how it can be accomplished?
Thanks in advance!
This is not a complete sample, but can give you some clues on how to implement it. You've to define some kind of form to collect the data about how your grid has to be rendered. I recommend you to create a CFormModel class if there are more than 3 input fields. Create a view file with the form and a div or renderPartial of a file containing a grid:
$form = $this->beginWidget('CActiveFormExt');
echo $form->errorSummary($model);
echo $form->labelEx($model,'column1');
echo $form->dropDownList($model
echo $form->error($model,'column1');
echo CHtml::ajaxSubmitButton('UpdateGrid',array('controller/grid'),
array('update'=>'#grid'),
$this->endWidget();
// you can render the 'default options' before any ajax update
$this->renderPartial('_grid',array($customColumns=>array('id','name'),'dataProvider'=>$dataProvider));
In the _grid.php view file:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'grid',
'dataProvider'=>$dataProvider,
'columns' => $customColumns;
));
In the controller:
function actionGrid(){
// recover the form data, and build the custom columns array
$customColumns = array();
$customColumns[] = '.....';
$dataProvider = ...;
$this->renderPartial('_formTrabajo', array('customColumns' => $idSiniestro, 'dataProvider' => $dataProvider'), false);
}
When you click the ajaxSubmitButton, the form is sent to the url specified through ajax, and the reply from the controller must contain the renderPartial of the view containing the grid, so the jQuery call can replace the html correctly. You must pass an array from your controller to the partial view of the grid, with the custom list of columns you want to display.

Categories