still a novice, but I am fighting with the paginator to limit my view to 10 records [properties] and to sort it on a field in the properties model. What am I doing wrong or miss in my code to get the paginator to control my view???
I have 2 models, Regions and Properties, a region has many properties, for example, my view is /region/view/13 This shows all properties for region 13.
The paginator is displaying the correct amount of properties, set the pages correct and all seems correct, but the paginator is not limiting my view, it just displays all properties by that region in one huge list instead of limit the view to 10 per page.
The sort per sleeps doesn't work either although the url in the browser seems to be ok.
/regions/view/13/sort:sleeps/direction:asc
/regions/view/13/sort:sleeps/direction:desc
Model:
<?php
App::uses('AppModel', 'Model');
/**
* Region Model
*
* #property Country $Country
* #property Property $Property
*/
class Region extends AppModel {
/**
* Display field
*
* #var string
*/
public $displayField = 'regionname';
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* #var array
*/
public $hasMany = array(
'Property' => array(
'className' => 'Property',
'foreignKey' => 'region_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
Controller:
public function view($id = null) {
if (!$this->Region->exists($id)) {
throw new NotFoundException(__('Invalid region'));
}
$this->Region->recursive = 2; // related to associated model data, Region -> Properties -> PropertyImages
$options = array('conditions' => array('Region.' . $this->Region->primaryKey => $id));
$total = $this->Region->Property->find('count', array(
'conditions' => array('Property.region_id' => $id)
));
$this->set('total', $total); // Push to counter the view.
$this->set('region', $this->Region->find('first', $options)); // Push the properties to the view.
$this->paginate = array(
'limit' => 10,
'conditions' => array('Property.region_id' => $id),
);
$this->Region->Property->virtualFields['sleeps'] = 'Property.sleeps';
$this->set('regions', $this->paginate('Property'));
}
View:
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled')); ?>
</div>
<br>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}'))); ?><br>
Total properties: <?php echo $total . "\n"; ?><br>
Sort by: <?php echo $this->Paginator->sort('sleeps'); ?>
<?php if (!empty($region['Property'])): ?>
<div class="regionviewfooter"></div>
<?php
$i = 0;
foreach ($region['Property'] as $property): ?>
<!-- We only need 1 image to show up -->
<?php foreach ($property['PropertyImage'] as $key =>$propertyImage): ?>
<div class="regionview">
<div class="regionviewleft">
<?php echo '<span style="font-size:12px;line-height:18px;font-weight:bold;">'; echo $this->Html->link(__($property['description']), array('controller' => 'properties', 'action' => 'view', $property['id'])); echo '</span>'; ?>
<div class="regionviewheader">Sleeps <?php echo $property['sleeps']; echo ' :: ' ?>
<?php
if ($property['minPrice'] == 0) {
echo 'Please call for prices';
} else {
echo 'Prices from ';
echo $this->Number->currency($property['minPrice'], 'GBP');
if ($property['maxPrice'] == 0) {
echo ' PW';
} else {
echo ' - ';
echo $this->Number->currency($property['maxPrice'], 'GBP');
echo ' PW';
}
}
echo '<span style="font-size:11px;line-height:18px;font-weight:normal;">'; echo ' :: ';
echo $this->Html->link(__('View Property'), array('controller' => 'properties', 'action' => 'view', $property['id'])); echo '</span>'; ?>
</div>
<?php echo ($property['shortDesc']); ?><br>
Add to my Enquiry List :: Property Ref. (<?php echo strtoupper($property['ref']); ?>)
</div>
<div class="regionviewright">
<!-- display image -->
<?php echo $this->Html->image(($propertyImage['filename']), array('alt' => ($propertyImage['description']),'width' => '150' ,'height' => '77')); $key ?>
<?php $key++; ?>
</div>
</div>
<div class="clear"></div>
<div class="regionviewfooter"></div>
<?php if ($key == 1) {
break; // Stop, we did 1 image.
}
?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
<br>Top of Page<br><br>
You're setting your paginated results to $regions, yet in your view, you're looping through and displaying $region (singular), which is a straight-up find() above, with recursive 2, which will pull all properties.
So - although there are many other things that need cleaned up with your code, for now, just use $regions (plural) instead of $region singular in your View.
Related
I is not What is the problem, I made a simple blog in Croogo, the problem is it is not adding or modifying or delete, because Arabic language ??
function add in controller
`public function admin_add() {
$this->set('title_for_layout', __('Add Part'));
if (!empty($this->request->data)) {
$this->Part->create();
if ($this->Part->save($this->request->data)) {
$this->Session->setFlash(__('The Part has been saved'), 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));//, $this->Part->id));
} else {
$this->Session->setFlash(__('The Part could not be saved. Please, try again.'), 'default', array('class' => 'error'));
}
}
}`
=>
model
`class Part extends AppModel {
public $name = 'Part';
var $belongsTo = array(
'Market' => array(
'className' => 'Market',
'foreignKey' => 'market_id',
)
);
protected $_displayFields = array(
'id',
'num_part',
'prix_part',
'prop_Pledge',
'prix_Pledge',
);
}
`
add view
<?php $this->extend('/Common/admin_edit'); ?>
<?php echo $this->Form->create('Part');?>
<fieldset>
<div class="tabs">
<ul>
<li><span><?php echo __('Part'); ?></span></li>
<?php echo $this->Layout->adminTabs(); ?>
</ul>
<div id="role-main">
<?php
echo $this->Form->label('num_part', 'العدد : ');
echo $this->Form->input('num_part',array('label' => false ));
echo $this->Form->label('prix_part', 'االمبلغ : ');
echo $this->Form->input('prix_part',array('label' => false ));
echo $this->Form->label('prop_Pledge', 'إقتراح التعهد : ');
echo $this->Form->input('prop_Pledge',array('label' => false ));
echo $this->Form->label('prix_Pledge', 'مبلغ التعهد : ');
echo $this->Form->input('prix_Pledge',array('label' => false ));
?>
</div>
<?php echo $this->Layout->adminTabs(); ?>
</div>
</fieldset>
<div class="buttons">
<?php
echo $this->Form->end(__('Save'));
echo $this->Html->link(__('Cancel'), array(
'action' => 'index',
), array(
'class' => 'cancel',
));
?>
</div>
message error : The requested address was not found on this server.
I don't think it is because of Arabic language because when you do :
echo $this->Form->label('prop_Pledge', 'إقتراح التعهد : ');
The second parameter is a text. I don't know which Cake version you're using but I think that the first field should be Part.prop_Pledge (Cake v3).
I suggest you to try to use debug() and exit to see where is the real problem or try to put remove arabic characters to see if this is the reason of you're problem.
Just a question where is your $this->Form->create() in view ?
line added to the database successfully but does not retoure to index
it displays an error message
Could you print the message error hello21 please ?
Hope this will help you.
in my project I have database
"Products" hasAndBelongsToMany "Sizes"
"ProductsSize" belongsTo "Products"
"ProductSize" belongsTo "Size"
In ProductsSize, one product_id may have many size_id.
hence, i want to do a form that have a select type input that list down all the size_id where the product_id = $id.
What i have done is:
in controller:
$size = $this->Product->ProductsSize->find('all', array(
'conditions' => array('ProductsSize.product_id' => $id),
'fields' => array('ProductsSize.size_id'),
));
in view:
<?php echo $this->form->create('Cart', array('action' => 'add')); ?>
<?php echo $this->form->input('size_id', array('label' => 'Size', 'options' => $size)); ?>
then i got error: Undefined index: ProductsSize
but when i put foreach, the data shown:
<?php foreach ($size_apparel as $size): ?>
<?php echo $size['ProductsSize']['size_id'];?><br/>
<?php endforeach; ?>
can anyone please help me to do the foreach in options.
The problem is that you are using an invalid array for the options. You are requesting:
$size = $this->Product->ProductsSize->find('all', array(
'conditions' => array('ProductsSize.product_id' => $id),
'fields' => array('ProductsSize.size_id'),
));
What you should be requesting is:
$size = $this->Product->ProductsSize->find('list', array(
'conditions' => array('ProductsSize.product_id' => $id),
));
This will return the options for the form field in the format it expects which is:
array(
{id} => {size},
{id} => {size},
{id} => {size},
{id} => {size},
...
)
I think you meant to say
<?php echo $this->form->input('size_id', array('label' => 'Size', 'options' => $size)); ?>
note 'options' instead of 'value'
$List = Set::classicExtract($size, '{n}.ProductSize.product_id');
$List will contain key value(ProductSize.product_id) pair.
OR you can change your query as
$size = $this->Product->ProductSize->find('list', array(
'conditions' => array('ProductSize.product_id' => $id),
));
Hope it will work for you
I have a form that has multiple entries to the same table, the problem I'm having is that it only sends the last entry to the database and doesn't send the rest. I have used the debug and it only prints out the last field in the form.
With each iteration of field its meant to input into the fields_invoices table a invoice_id, field_id, entered_value
Here is my form
<?php echo $this->Form->create('FieldsInvoice'); ?>
<?php foreach ($fields as $field): ?>
<?php echo $this->Form->hidden('FieldsInvoice.id'); ?>
<?php echo $this->Form->hidden('FieldsInvoice.field_id', array('default' =>$field['Field']['id'])); ?>
<?php echo $this->Form->input('FieldsInvoice.invoice_id', array('default' =>$invoice_id, 'type'=>'hidden')); ?>
<?php echo $this->Form->Input('FieldsInvoice.entered_value', array('label'=>$field['Field']['name'], 'default' =>$field['Field']['default_value'])); ?>
<?php endforeach ;?>
<?php echo $this->Form->End('Submit');?>
Here is the related controller
public function create($id)
{
$this->set('title_for_layout', 'Create Invoice');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
if (!is_numeric($id)) throw new BadMethodCallException('I need an ID');
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) throw new NotFoundException('Invalid ID');
$this->set('invoice_id',$id);
$names = $this->Invoice->find('list',array(
'fields'=>array('template_id'),
'conditions'=>array('id'=>$id)));
$fields = $this->Field->find('all', array(
'conditions'=>array(
'template_id'=>$names)));
$this->set(compact('fields'));
$this->set(compact('invoice_id'));
$this->set('name',$names);
$this->Invoice->create();
if(empty($this->data)){
$this->data= $this->Field->read($id);
}
else{
if($this->request->is('post'))
{
//die(debug($this->data));
$this->Invoice->create();
if($this->FieldsInvoice->save($this->request->data, array('deep'=>true)));
{
$this->Session->setFlash('The field has been updated');
$this->redirect(array('controller'=>'invoices', 'action'=>'index'));
}
//else{
$this->Session->setFlash('Could not be saved');
//}
}
}
}
\app\Controller\InvoicesController.php (line 134)
array(
'FieldsInvoice' => array(
(int) 0 => array(
'id' => '',
'field_id' => '0',
'invoice_id' => '97',
'entered_value' => '1'
),
(int) 1 => array(
'id' => '',
'field_id' => '99',
'invoice_id' => '97',
'entered_value' => '2'
),
(int) 2 => array(
'id' => '',
'field_id' => '999',
'invoice_id' => '97',
'entered_value' => '3'
),
(int) 3 => array(
'id' => '',
'field_id' => '9999',
'invoice_id' => '97',
'entered_value' => '4'
)
)
)
To post multiple fields, use:
foreach ($fields as $idx => $field):
echo $this->Form->hidden('FieldsInvoice.'.$idx.'.id');
echo $this->Form->hidden('FieldsInvoice.'.$idx.'.field_id', ...);
...
and then use $this->FieldsInvoice->saveMany() (instead of ->save()) to save multiple records.
I currently have this:
Hotels Controller
class HotelsController extends AppController {
var $name = 'Hotels';
function admin_add() {
$this->set('hotel_categories', $this->Hotel->HotelCategory->find('list'));
if ( ! empty($this->data)) {
$this->data['Page']['title'] = $this->data['Hotel']['title'];
$this->data['Page']['layout'] = 'index';
if ($this->Hotel->saveAll($this->data)) {
$this->Session->setFlash('Your hotel has been saved', 'flash_good');
$this->redirect(array('action' => 'admin_add'));
}
}
}
HotelCategory Model
class HotelCategory extends AppModel {
var $name = 'HotelCategory';
var $hasAndBelongsToMany = array(
'Hotel' => array(
'className' => 'Hotel'
)
);
Hotel Model
class Hotel extends AppModel {
var $name = 'Hotel';
var $hasAndBelongsToMany = array(
'HotelCategory' => array(
'className' => 'HotelCategory'
)
);
View
<div id="main">
<h2>Add Hotel</h2>
<?php echo $this->Session->flash();?>
<div>
<?php
debug($hotel_categories);
echo $this->Form->create('Hotel');
echo $this->Form->input('Hotel.title');
echo $this->Form->input('HotelCategory', array('options' => 'select', 'multiple' => 'checkbox'));
echo $this->Form->input('Hotel.body', array('rows' => '3'));
echo $this->Form->input('Page.meta_keywords');
echo $this->Form->input('Page.meta_description');
echo $this->Form->end('Save Hotel');
?>
</div>
<!-- main ends -->
</div>
I can confirm that when I debug($hotel_categories); that there are values.
The problem I am having is that the $this->Form->input('HotelCategory', array('options' => 'select', 'multiple' => 'checkbox')) doesn't produce any options.
That should be:
echo $this->Form->input('HotelCategory', array(
'type' => 'select',
'multiple' => 'checkbox',
'options'=>$hotel_categories));
try explicitly setting the options list in the view
<?php echo $this->Form->input('HotelCategory', array(
'type'=>'select',
'options' => $hotel_categories,
'multiple' => true)); ?>
My controller action is:
function add() {
if (!empty($this->data)) {
$this->Customer->create();
if ($this->Customer->save($this->data)) {
$this->Session->setFlash('A new Customer has been added');
$this->redirect(array('action'=>'index'));
}
else {
$this->Session->setFlash('The customer cannot be added this time. Try again later.');
$this->redirect(array('action'=>'index'));
}
}
}
My model is:
class Customer extends AppModel {
var $name = 'Customer';
var $validate = array(
'name' => array(
'length'=> array(
'rule' => array('between', 4,50),
'message' => 'Name must be minimum 4 and maximum 50 characters long.'
),
'checkUnique'=> array(
'rule' => 'isUnique',
'message' => 'This Name is already registered'
)
));
and this is my view:
<div class="customers form">
<?php echo $form->create('Customer',array('action'=>'add'));?>
<fieldset>
<legend><?php __('Add Customer');?></legend>
<?php
echo $form->input('Customer.name');
echo $form->input('Customer.balance',array('type'=>'hidden','default'=>0));
?>
</fieldset>
<?php echo $form->end('Submit');?>
</div>
every time I submit the form it splashes:
The customer cannot be added this time. Try again later.
For the hidden input field use 'value' instead of 'default' in your view:
$form->input('Customer.balance', array('type' => 'hidden', 'value' => 0));