I am using contain behavior and i want to be able to sort the data as always but I am having troubles.
It might be because the columns i want to sort are not columns on the paginate table but on the contain tables.
Subscriptions table only contains the "user_id" and the "post_id".
At the controller i have:
$this->paginate = array(
'conditions' => array('Subscription.user_id' => $this->Session->read('Auth.User.id')),
'paramType' => 'querystring',
'limit' => 20,
'contain'=> array('User', 'Post' => array('Priority', 'Status'))
);
$this->set('subscriptions', $this->paginate('Subscription'));
And in my view, this:
<!-- example of not working sort -->
<th class="td_subject"><?php echo $this->Paginator->sort('subject'); ?></th>
The data of subject, for example, is contained on the array $subscription with the normal format:
$subscription['Post']['subject']
And i print it this way:
foreach ($subscriptions as $subscription):
echo ...
echo '<td>'.h($subscription['Post']['subject'].'</td>';
echo ...
endforeach;
How can i use pagination in this case? Thanks.
Don't use contain in this case, manually join the tables:
$this->paginate = array(
'conditions' => array('Subscription.user_id' => $this->Session->read('Auth.User.id')),
'paramType' => 'querystring',
'limit' => 20,
'joins'=>array(
array(
'table'=>'users',
'alias'=>'User',
'conditions'=>array(
'Subscription.user_id = User.id
)
),
array(
'table'=>'posts',
...
),
array(
'table'=>'priorities',
...
),
array(
'table'=>'status',
...
)
)
);
Your view:
<?php echo $this->Paginator->sort('Post.subject', 'Subject'); ?>
Related
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 table in which i want to get only two columns data .. right now i am using findAll method ... i don't know how can I get the specific two fields data in CakePHP
$recentContacts = $this->Contact->find('all',
array(
'order'=>'Contact.idContacts DESC',
'limit' => 6,
'conditions' => array(
'Contact.User_id' => $id)));
in my contacts table there are two fields one is "name" and other is "number"
which I want to extract ...
You can do this by adding fields attribute.
$recentContacts = $this->Contact->find('all',
array
(
'order'=> array( 'Contact.id' , 'Contacts DESC'),
'limit' => 6,
'fields' => array(
'Contact.name',
'Contact.number'
),
'conditions' => array
(
'Contact.User_id' => $id
)
));
you can use like this way with your same code to add fields
$recentContacts = $this->Contact->find('all',
array(
'order'=>'Contact.idContacts DESC',
'limit' => 6,
'fields' => array(
'Contact.name',
'Contact.number'
),
'conditions' => array(
'Contact.User_id' => $id)));
in previous answer they have changed you id instead of idContacts, you can just copy my code and solve your problem.
let me know if i can help you more.
now i got a table contact.
the contact have 2 row which is id and name.
now i got 3 user in my contact,
1 A
2 B
3 C
question.
how do i make a select input as the code of below ( in cakephp ):
<select name="contact" id="UserField">
<?php for($i=1;$i<=3; $i++) ?>
<option value="1"><?php echo $contact['Contact']['name']; ?></option>
</select>
In your controllers action you can set a list of the names
$names = $this->Contact->find('list', array(
'contain' => array(),
'fields' => array(
'Contact.id',
'Contact.name'
),
'order' => array(
'Contact.name' => 'ASC'
)
));
$this->set(compact('names'));
And then in your view
echo $this->Form->input('contact', array(
'label' => 'Contact',
'options' => $names
));
I am trying to implement selectbox in my site.Option for that selectbox is set in my controller like below
$tmp_user = $this->User->find('first',array('id'=>$this->Auth->user('id')));
$zip_info = $this->Zipcode->find('first',array('id'=>#$tmp_user['User']['zip_id']));
$region_admins = $this->AdminRegion->find('all',array('conditions'=>array('AdminRegion.region_id'=>#$zip_info['Zipcode']['region_id'])));
if(!empty($region_admins)){
foreach($region_admins as $radmn):
//pr($radmn);
$admin_user = $this->User->find('list',array('conditions'=>array('id'=>$radmn['AdminRegion']['user_id']),'fields'=>array('id','username')));
pr($admin_user);
$this->set('users',$admin_user);
endforeach;
I am getting value like this when i print from controller
Array
(
[137] => governmentuser1
)
Array
(
[198] => testadmin
)
Array
(
[215] => adminregion
)
Array
(
[224] => testcompany1234
)
Array
(
[225] => testuser12345678
)
but only last value is set in select box....
Where did i made mistake?
How about something like this:
<?php
$tmp_user = $this->User->find('first', array(
'id' => $this->Auth->user('id')
));
$zip_info = $this->Zipcode->find('first', array(
'id' => #$tmp_user['User']['zip_id']
));
$region_admins = $this->AdminRegion->find('list', array(
'conditions' => array(
'AdminRegion.region_id' => #$zip_info['Zipcode']['region_id']
),
'fields' => array('AdminRegion.user_id', 'AdminRegion.user_id')
));
$admin_users = $this->User->find('list', array(
'conditions' => array(
'id' => $region_admins
),
'fields' => array(
'id','username'
)
));
?>
A few side notes:
Why do another query for the user data? Isn't it all in the session?
I wouldn't use the # anywhere, it's slower and suppresses errors. It will make debugging a nightmare too.
I think you missed array:
$admin_user[] = $this->User->find('list',array('conditions'=>array('id'=>$radmn['AdminRegion']['user_id']),'fields'=>array('id','username')));
Hope it helps
HI,
why not work "order" function?
$this->paginate = array(
'limit' => 5,
'order' => array(
'User.name' => 'desc'
),
'fields' => array('Post.id', 'Post.title', 'User.name AS aut_name'),
'joins' => array(
array(
'table' =>'users',
'alias' =>'User',
'type' =>'LEFT',
'conditions' => array(
'Post.user_id = User.user_id'
)
)
)
);
$posts = $this->paginate();
$this->set(compact('posts'));
DB structure:
posts:
id, title,body, created, updated, user_id
users:
user_id, name
Looking quickly your code... Does field user_id exist in User table?
'conditions' => array(
'Post.user_id = User.id'
)
Because you're specifying User.name As aut_name in your fields, you won't be able to order by User.name unless you also have User.name in your fields list. Alternatively use:
'order' => array(
'aut_name' => 'desc'
),
NOTE: This is for the initial query only, to sort by aut_name from the View you'll need to use a Virtual Field in the User model.
Also, as #Min said, are your conditions correct?
Adding a solution on the same problem:
This work for cakePHP 2.3.4
CakePHP Paginate conditions on Join Table