cakephp paginator last record delete error - cakephp

Sample here, please check that happens or not
<?php
echo $this->Paginator->prev
($this->Html->image('prev.png'), array('escape' => false),
array(), null, array('class' => 'prev'));
echo $this->Paginator->counter
('Page {:page} of {:pages}, Total Records {:count}');
echo $this->Paginator->next($this->Html->image
('next.png'), array('escape' => false),
array(), null, array('class' => 'next'));
?>

Check out this, hope it works
public function index() {
try {
$paginatedData = $this->Paginator->paginate();
} catch (NotFoundException $e) {
//get current page
$page = $this->request->params['named']['page'];
if( $page > 1 ){
//redirect to previous page
$this->redirect( array( "page" => $page-1 ) );
}else{
$paginatedData = array(); //no data to paginate so use empty array()
//you will have to check for this in the view and no longer display the pagination links, since they will NOT be defined
}
}
}

Related

cakephp 2, edit record in modal by bootstrap

How to edit a record with Cakephp and modal bootstrap?
Because when I edit a contact, I get the error 500?
missing view The view for UserContactsController:: admin_modal_edit()
was not found.
In to controller admin_modal_edit() I have set $this->layout = NULL;
These are the files of the app.
File: user/view.ctp with list of contact and the modal for add or edit.
BUTTON FOR EDIT OR DELETE
<?php echo $userContact['UserContactType']['title']; ?>: <?php echo $userContact['contact']; ?>
<?php echo __('Edit'); ?>
BOOTSTRAP MODAL
<?php
echo $this->Form->create('UserContact', array('url' => array('admin' => true, 'prefix' => 'admin', 'plugin' => 'user', 'controller' => 'user_contacts', 'action' => 'modal_edit')));
?>
<?php echo $this->Form->input('UserContact.id', array('class' => 'form-control')); ?>
<?php echo $this->Form->input('UserContact.user_id', array('default' => $user_id, 'type' => 'hidden')); ?>
<?php echo $this->Form->input('UserContact.user_contact_type_id', array('class' => 'form-control', 'empty' => true)); ?>
<?php echo $this->Form->input('UserContact.contact', array('class' => 'form-control')); ?>
<?php echo $this->Form->submit(__('Save'), array('div' => false, 'class' => 'btn btn-success')); ?>
<?php echo $this->Form->end(); ?>
File: controller/UsersController.php that generates view.ctp
public function admin_view($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$user_id = $id;
$options = array(
'contain' => array('UserContact' => 'UserContactType', 'UserGroup', 'UserState', 'UserGender', 'UserAddress' => 'UserAddressType', 'UserPaymentType', 'Item', 'Comment'),
'conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
// blocco find list
$userContactTypes = $this->UserContactType->find('list');
$userAddressTypes = $this->UserAddressType->find('list');
$this->set(compact(array('userContactTypes', 'userAddressTypes', 'user_id')));
}
File: controller/UserContactsController.php for the modal
public function admin_modal_edit() {
$id = $this->request->query('id');
$this->layout = NULL;
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->UserContact->save($this->request->data)) {
$this->Session->setFlash(__('The record has been saved'), 'flash/success');
$this->redirect(array('controller' => 'users', 'action' => 'view', $this->request->data['UserContact']['user_id']));
} else {
$this->Session->setFlash(__('The record could not be saved. Please, try again.'), 'flash/error');
}
} else {
if (!empty($id)) {
$options = array('conditions' => array("UserContact.{$this->UserContact->primaryKey}" => $id));
$this->request->data = $this->UserContact->find('first', $options);
}
}
}
Your issue is that CakePHP is trying to render the 'admin_modal_edit.ctp' View template.
If you don't want CakePHP to render anything set autoRender to false:-
public function admin_modal_edit() {
$this->autoRender = false;
}
This will prevent CakePHP from looking for a View template to render.
$this->layout = null does not stop Cake from attempting to render templates. Which I suspect is what you're trying to achieve.
This error is showing because cakephp is complaining about the function admin_modal_edit for not having a view since it cannot find the admin_modal_edit.ctp in the view folder of your controller.
To fix this, add this
$this->autoRender = false
to your function admin_modal_edit to disable the rendering of view for that function.
So your function should look like this.
public function admin_modal_edit() {
/** -------------- **/
$this->autoRender = false;
/** -------------- **/
$id = $this->request->query('id');
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->UserContact->save($this->request->data)) {
$this->Session->setFlash(__('The record has been saved'), 'flash/success');
$this->redirect(array('controller' => 'users', 'action' => 'view', $this->request->data['UserContact']['user_id']));
} else {
$this->Session->setFlash(__('The record could not be saved. Please, try again.'), 'flash/error');
}
} else {
if (!empty($id)) {
$options = array('conditions' => array("UserContact.{$this->UserContact->primaryKey}" => $id));
$this->request->data = $this->UserContact->find('first', $options);
}
}
}

How to implement pagination with search in CakePHP

My pagination is working, even the search is also working. But the problem that I'm having is when i click on next page link in the pagination links. The search is not working for the next page of the pagination. Also I need to know how I send other parameters through the url, and use them in query of pagination. I need help on this as I am novice in CakePHP.
In controller page I have used this code:
class StatesController extends AppController {
public $components = array('Paginator');
public $paginate = array(
'limit' => 2,
'fields' => array('State.id', 'State.state','State.code'),
'order' => array(
'State.state' => 'asc'
)
);
public function admin_index() {
$this->layout = false;
$this->layout = 'adminlayout';
//****** pagination starts
$search=$this->request->data('State.search');
$this->Paginator->settings = $this->paginate;
// similar to findAll(), but fetches paged results
$stateListAr = $this->Paginator->paginate('State',
array('State.state LIKE' => "%".$search."%")
);
$this->set('stateListAr', $stateListAr);
//****** pagination ends
$this->set('stateListAr',$stateListAr);
$this->render('admin_index');
}
}
In view page I have used this code:
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->numbers(array('first' => 'First page')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
i have got solution to my problem , here it is . I have used a code in view page , here $search variable i have setted data from controller.
$search);
$this->Paginator->options(array(
'url' => $urlParamAr
));
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->numbers(array('first' => 'First page'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
?>

afterFind not working with recursive level 2

Issue with afterFind().
Current code :
<?php
public function afterFind($results, $primary = false)
{
foreach ($results as $key => $val)
{
if (isset($val['User']['country_code']) && isset($val['User']['mobile']))
{
$results[$key]['User']['mobile'] = trim($val['User']['country_code']).trim($val['User']['mobile']);
}
}
return $results;
}
?>
It's working with $this->User->find(), but not working with other models.
I have 3 models. Room, Place and User.
<?php
$this->Place->bindModel(array('belongsTo' => array('User')));
$this->Room->bindModel(array('belongsTo' => array('Place')));
?>
When I try to find Room data :
<?php
$data = $this->Room->find('first');
array(
[Room] => array()
[Place] => array(
[User] => array(
[mobile] => /* here after find not working it should content country code + mobile */
)
)
)
?>
That might be problem with $reset while binding flyby. http://api.cakephp.org/2.1/source-class-Model.html#868-910
Please set $reset to false.
$this->Model->bindModel(array(...),false);

How to do multiple pagination without page not found error in Cakephp

I have paginate 2 models (Income,Expanse) in the Student model view page .
I have really page not found problem with cakephp's pagination.
It have no any problem when the paginate result has only 1 page or the same, but It will error if one has more paginate result than other.
for example.
-income has paginate result 1 page and expanse has 1 page. No problem at all.
-income has 10 pages and expanse has 10 pages no problem at all.
-income has 2 pages and expanse has 1 page. income page 2 page not found error.
-income has 5 pages and expanse has 2 pages. income page 3,4,5 page not found error.
-income has 10 pages and expanse has 13 pages. expanse page 11,12,13 page not found error.
for example(not real one) , Student's view have income and expense items ,Both are display as pagination.
//this is how I config paginator in Student controller.
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'recursive'=>0
),
'Expense' => array (
'order'=>array('expense_id'=>'ASC'),
'limit'=>10,
'recursive'=>0,
)
);
<div class="paging">//this is how I config Income paginator in Student view
<?php
echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Income'), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('model'=>'Income','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
echo $this->Paginator->next(__('next') . ' >', array('model'=>'Income'), null, array('class' => 'next disabled'));
?>
</div>
//this is how I config Expanse paginator in Student view
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Expanse'), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('model'=>'Expanse','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
echo $this->Paginator->next(__('next') . ' >', array('model'=>'Expanse'), null, array('class' => 'next disabled'));
?>
</div>
Please help me. sorry for my english
If you have any question , please ask me.
Thank you.
Yes It is possible.....
Step1 : Make paging.ctp in View/Elements/paging.ctp
<?php if (!isset($this->Paginator->params['paging'])) {
return;
}
if (!isset($model) || $this->Paginator->params['paging'][$model]['pageCount'] < 2) {
return;
}
if (!isset($options)) {
$options = array();
}
$options['model'] = $model;
$options['url']['model'] = $model;
$this->Paginator->__defaultModel = $model;
?>
<div class="paging">
<ul class="pagination pull-right">
<?php
echo $this->Paginator->prev('<<', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
echo $this->Paginator->numbers(am($options,array('tag' => 'li', 'separator' => '', 'currentClass' => 'active', 'currentTag' => 'a')));
echo $this->Paginator->next('>>', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
?>
</ul>
</div>
Step2: Now add this function to Controllers/AppController
public function pageForPagination($model) {
$page = 1;
$sameModel = isset($this->request->params['named']['model']) && $this->request->params['named']['model'] == $model;
$pageInUrl = isset($this->request->params['named']['page']);
if ($sameModel && $pageInUrl) {
$page = $this->request->params['named']['page'];
}
$this->passedArgs['page'] = $page;
return $page;
}
Step3: Now in controller action do some conditions so that proper page should be call
if (empty($this->request->params['named'])) {
$page = $this->pageForPagination('Income');
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page,
'recursive'=>0
),
'Expense' => array (
'order'=>array('expense_id'=>'ASC'),
'limit'=>10,
'page' => $page1,
'recursive'=>0,
));
} else if ($this->request->params['named']['model'] == 'Income') {
$page = $this->pageForPagination('Income');
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page,
'recursive'=>0
));
} else if ($this->request->params['named']['model'] == 'Expense') {
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
'Expense' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page1,
'recursive'=>0
));
Step 4: Now call the paging element in your student view
<?php echo $this->element('paging', array('model' => 'Income'));?>
<?php echo $this->element('paging', array('model' => 'Expense'));?>
Note: Please take care of brackets and semicolon.......and sorry to be late but will help others...Thanks..
I think if you use two different iframes to load different URLs for pagination, you can handle this.
OR you have to call ajax functions to paginate 2 models in one page without reloading the page....

CakePHP Edit Not Loading foreign Table Data

I am trying to get my edit to work I need the contact detail data to load when the user data loads. I have set the data in a similar manner to how I am retrieving the list of roles. I also don't know how to retrieve according to the model currently I was hard-coding it to retrieve 28. Would greatly appreciate any help provided.
public function edit($id = null) {
//Populate roles dropdownlist
$data = $this->User->Role->find('list', array('fields' => array('id', 'name')));
$this->set('roles', $data);
$data2 = $this->User->ContactDetail->find('first', array(
'conditions' => array('ContactDetail.id' =>'28')));
$this->set('contactdetails', $data2);
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
my view is set up in the following manner
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Edit User'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('role_id');
echo $this->Form->input('ContactDetail.name');
echo $this->Form->input('ContactDetail.surname');
echo $this->Form->input('ContactDetail.address1');
echo $this->Form->input('ContactDetail.address2');
echo $this->Form->input('ContactDetail.country');
echo $this->Form->input('ContactDetail.email');
echo $this->Form->input('ContactDetail.fax');
?>
<label>Are you interested in buying property in Malta?</label>
<?php
$interest_buy = array('0'=>'no','1' => 'yes');
echo $this->Form->input('ContactDetail.interest_buy_property',array('type'=>'radio','options'=>$interest_buy,'value'=>'0','legend'=>FALSE));
?>
<label>Are you interested in renting property in Malta?</label>
<?php
$interest_rent = array('0'=>'no','1' => 'yes');
echo $this->Form->input('ContactDetail.interest_rent_property',array('type'=>'radio','options'=>$interest_rent,'value'=>'0','legend'=>FALSE));
echo $this->Form->input('ContactDetail.mobile');
echo $this->Form->input('ContactDetail.phone');
echo $this->Form->input('ContactDetail.postcode');
echo $this->Form->input('ContactDetail.town');
echo $this->Form->input('ContactDetail.newsletter',array('type'=>'checkbox','label'=>'Would you like to register for the newsletter?' ,'checked'=>'1','legend'=>FALSE,));
?>
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
User Model
public $primaryKey = 'id';
public $displayField = 'username';
public function bindNode($user) {
return array('model' => 'Role', 'foreign_key' => $user['User']['role_id']);
}
public function beforeSave($options = array()) {
$this->data['User']['password'] = AuthComponent::password(
$this->data['User']['password']
);
return true;
}
public $belongsTo = array(
'Role' => array('className' => 'Role'));
public $hasOne = array(
'ContactDetail' => array(
'foreignKey' => 'id'));
public $actsAs = array('Acl' => array('type' => 'requester', 'enabled' => false));
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['role_id'])) {
$roleId = $this->data['User']['role_id'];
} else {
$roleId = $this->field('role_id');
}
if (!$roleId) {
return null;
} else {
return array('Role' => array('id' => $roleId));
}
}
}
ContactDetail Model
public $primaryKey = 'id';
public $displayField = 'name';
If I get you right, you have 1 contact details row for each user. In this case, you need to define in your User model:
public $hasOne = array(
'ContactDetail' => array(
'foreignKey' => 'id',
),
);
Like this, your ids will be synced in both tables. If you don't want to make the id a foreign key, you don't have to, it's just a suggestion.
Next, when you retrieve your data in the controller, you can do:
$this->User->Behaviors->load('Containable');
$user = $this->User->find('first', array(
'conditions' => array('User.id' => $id),
'contain' => array('ContactDetail'),
));
Now I don't know if there is an automated way to do this, but I sort my data manually to fill in the inputs. I am guessing you will get a structure like array('User' => array(), 'ContactDetail' => array()).
$user['User']['ContactDetail'] = $user['ContactDetail'];
unset($user['ContactDetail']);
$this->request->data = $user;
Then in your view just set the fields as the input array:
$this->Form->create('User');
$this->Form->input('User.some_user_field');
$this->Form->input('User.ContactDetail.some_contact_detail_field');
This should fill in your fields. When you go save your data, if your array is structured like this, you can use saveAssociated():
$this->User->saveAssociated($this->request->data, array('deep' => true));
EDIT
In case your relation is defined as User hasMany ContactDetail, then you need to structure your data like this:
$this->request->data = array(
'User' => array(
// user data
'ContactDetail' => array(
[0] => array(
//contact data
),
),
),
);
And in your view:
$this->Form->input('User.ContactData.0.field')
This is for 1 row only, If you need more rows on the child table with 1 input, do your logic accordingly.
Once your User model hasOne ContactDetail, you don't need retrieve the information twice, since you've done in $this->request->data line, all association model will retrieve too.
So, your Controller looks like this:
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
// Roles
$roles = $this->User->Role->find('list', array('fields' => array('id', 'name')));
$this->set(compact('roles');
if ($this->request->is(array('post', 'put'))) {
if ($this->User->saveAll($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->User->read(null, $id);
}
}
And your View, for ContactDetail's fields looks like this:
echo $this->Form->input('ContactDetail.name');
And so for all the fields related to the model ContactDetail. You can find more details here: Saving Your Data.
User model:
public $hasOne = array(
'ContactDetail' => array(
'className' => 'ContactDetail',
'foreignKey' => 'user_id',
'dependent' => true
)
);
The best solution I have found is by setting
public $belongsTo = array(
'Role' => array('className' => 'Role')
, 'ContactDetail' => array('className' => 'ContactDetail'));
This way the contact detail data loads. Although the data once saved does not update contactdetails.
For me to save I used
$this->User->saveAll($this->request->data)
this worked fully

Resources