what should be input into the brackets of $this->paginate() - cakephp

I am using cakephp 1.26.
I am doing some self-learning about Pagination in CakePHP.
I have tested the following code in my localhost, and it works fine.
I have altered a little change to the second line of code, and found that
nothing change to the result.
1st version:
$this->paginate=array('conditions'=>array('Testing.zero'=>'0'), 'limit' => 3);
$w = $this->paginate();
$this->set('postVariable', $w);
2nd version:
$this->paginate=array('conditions'=>array('Testing.zero'=>'0'), 'limit' => 3);
$w = $this->paginate('Testing');
$this->set('postVariable', $w);
3rd version:
$this->paginate=array('conditions'=>array('Testing.zero'=>'0'), 'limit' => 3);
$w = $this->paginate('helloworld');
$this->set('postVariable', $w);
4th version:
$this->paginate=array('conditions'=>array('Testing.zero'=>'0'), 'limit' => 3);
$w = $this->paginate($this->helloworld);
$this->set('postVariable', $w);
I have no idea what I should input into the brackets of $this->paginate()

The documentation says it all: http://api.cakephp.org/class/controller#method-Controllerpaginate
First parameter is the model name, second parameter is a scope, that is, an additional conditions array. Third parameter is currently useless.

The paginate function can be found in /cake/libs/controller/controller.php,line 934.It's a bit long but not that complex.And I think you can read it and find the reason yourself.Personally I prefer current model name as the parameter.In your code ,that would be
$w = $this->paginate("Testing");

Related

cakephp pagination set condistion from array

I am using a form with several check boxes i need to display only those data which is in check box category.
How to write conditions for that.
for($i=0;$i<count($this->request->data['filter']['delivering']);$i++)
{
$opt1=".'Gig.bangsalsodeliverings' => ".$this->request->data['filter']['delivering'][$i];
$opt2=$opt2.$opt1.',';
}
$options=array('conditions' => array($opt2));
$this->Paginator->settings = $options;
$agetGigsItem = $this->Paginator->paginate('Gig');
But getting error.
Thanks in advance
It seems you're using a string contatenation instead of array to build the conditions array.
Also it's not clear to me if the filter delivering is a set of strings or integers.
I guess you can try:
// Merge the filters into a csv string
$filters = array();
foreach($this->request->data['filter']['delivering'] as $v){
$filters[] = "'{$v}'";
}
$csv_filters = implode(",", $filters);
// Use the csv to make a IN condition
$this->Paginator->settings = array('conditions' => array(
"Gig.bangsasodeliverings IN ({$csv_filters})",
));
Please note that sql injection can be made here, so prepare your data before creating $csv_filters.

Cakephp value from array

I've read many similar topics here but not one helps me with problem.
i'm trying to get sections_id value from query in controller.
$query_id = "SELECT sections_id FROM sections WHERE name='".$table_name."'";
debug($id = $this->Info->query($query_id)); die();
there is debug result
array(
(int) 0 => array(
'sections' => array(
'sections_id' => '14'
)
)
)
and i tried in controller get value of id typing $id['sections']['sections_id'], or
$id['sections_id'] and many other types, nothing works. Do you have any idea ?
use $id[0]['sections']['sections_id'] to access it
For one result use
$data[0]['sections']['sections_id']
If query returns more than one result the use below code:
foreach($id as $data){
echo $data['sections']['sections_id']
}
Look for array index's carefully! Iinjoy

cakephp sort link not working

Can't make sort links work. (concrete or virtual fields).
Vitual fields for my sum() field on this action:
$this->Qca->virtualFields['comps'] = 'Sum(CASE WHEN Qca.qca_tipcode = 1 THEN 1 END)';
$this->Qca->virtualFields['production'] = 'Sum(qca_end - qca_start)';
$this->Qca->virtualFields['idle'] = 'Sum(Qca.qca_durend)';
My find(), works fine:
$hoursvalues = $this->Qca->find('all', array('conditions' => $conditions,
'fields' => array('Qca.dir_id', 'Qca.name', 'Sum(CASE WHEN Qca.qca_tipcode = 1 THEN 1 END) AS Qca__comps', 'Sum(qca_end - qca_start) as Qca__production', 'Sum(Qca.qca_durend) as Qca__idle'),
'group' => array('Qca.dir_id')
)
);
and then:
$this->paginate('Qca' );
$this->set('hoursvalues', $hoursvalues);
What extra settings does $this->paginate('Qca' ); needs? Please note I have all data I need via find().
What is it I'm missing that sorting does not work for either concrete or virtual fields?
Thansk a lot!
Carlos
Part of your problem is the following:
$this->paginate('Qca');
$this->set('hoursvalues', $hoursvalues);
$this->paginate() returns an array with sorted values. What you need to do is specify your extra settings in $this->paginate array and then
$this->set('hoursvalues', $this->paginate('Qca'));
For your other fields, making them virtualFields will make them easier to work with.

Should I use more find in this case?

i have two controllers
Sections_controller.php
Articles_controller.php
Section model hasmany Article...
i want to fetch articles In the form of blocks like all news sites..every block have section name with links to the articles within this section..so i use this code......
The First block
$block1=$this->Article->find('all',
array(
'limit' => 4, // just fetch 4 articles
'order' => array('Article.created'=>'DESC'),
'conditions' => array('Section_id' => 87)
)
);
// set the section for the view
$this->set(compact('block1'));
The second block
$block2=$this->Article->find('all',
array(
'limit' => 4, // just fetch 4 articles
'order' => array('Article.created'=>'DESC'),
'conditions' => array('Section_id' => 88)
)
);
// set the section for the view
$this->set(compact('block2'));
and etc....
anyone have the best method in this task without Repetition find code..
notice..i cant pass $id in the function because articles must display when request site index example( www.newssite.com)
Any find(s) should be done in the Model, not the controller - this follows the MVC structure as well as the "fat models, skinny controllers" mantra, which helps keep with the MVC idea.
This is not only the way it "should" be done, but it will also allow you to have the code in just one place:
//in the Article model
function getArticlesBySection($id) {
$articles = $this->find('all', array(
'limit' => 4,
'order' => array('Article.created'=>'DESC'),
'conditions' => array('Section_id' => $id)
));
return $articles;
}
//in the Articles controller
$block1 = $this->Article->getArticlesBySection('87');
$block2 = $this->Article->getArticlesBySection('88');
$this->set(compact('block1', 'block2'));
The above should work just fine for what you want to do, but there is always a lot you can do to improve upon it - like setting it up to be a lot more flexible by accepting an array of options:
//in the Article model
function getArticles($id, $opts = null) {
$params = array();
//limit
$params['limit'] = 100; //catchall if no limit is passed
if(!empty($opts['limit'])) $params['limit'] = $opts['limit'];
//order
$params['order'] = array('Article.created'=>'DESC');
if(!empty($opts['order'])) $params['order'] = $opts['order'];
//conditions
$params['conditions'] = array();
if(!empty($opts['sections'])) array_push($params['conditions'], array('Section_id'=>$opts['sections']));
$articles = $this->find('all', $params);
return $articles;
}
//in the Articles controller
$opts = array('limit'=>4, 'sections'=>array('87'));
$block1 = $this->Article->getArticles($opts);
$opts['sections'] = array('88');
$block2 = $this->Article->getArticles($opts);
I'm sure there are things that can be done to make this more lean...etc, but it's how I like to write it for ease of use and readability, and at least gives you a start on how to think of model methods, and the ability to use and reuse them for different purposes.
You can accomplish this with a straight mysql query, but I'm not sure how you would fit it into a cake model->find function. You can do something like this:
$articles = $this->Article->query("SELECT * FROM articles a WHERE (SELECT COUNT(*) FROM articles b WHERE a.Section_id = b.Section_id AND a.created < b.created) < 4 ORDER BY a.Section_id, a.created DESC");
/* if you look at the results, you should have the 4 latest articles per section.
Now you can loop through and set up an array to filter them by section. Modify to fit your needs */
foreach($articles as $article) {
$results[$article['Article']['Section_id']][] = $article;
}
$this->set('results',$results);

Drupal 7, insert user_profile_form into a page

I tried to insert user_profile_form into a page. the code is rather simple:
global $user;
module_load_include('inc', 'user', 'user.pages');
print drupal_render(drupal_get_form('user_profile_form', $user, 'account'));
everything works fine except when uploading images, it shows:
"Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'user_profile_form' was given in drupal_retrieve_form()"
Any ideas, thx a lot
you need to use form_load_include() instead of module_load_include to load/build a form, and you have to pass arguments inside $form_state.
the code you are looking for looks like:
$form_id = 'user_profile_form';
$form_state = array();
//passing arguments to form.
$form_state['build_info']['args'] = array($account, 'account');
form_load_include($form_state, 'inc', 'user', 'user.pages');
$form = drupal_build_form($form_id, $form_state);
To prevent the notice or warning:
Trying to get property of non-object in user_account_form() (line 1019 of /var/www/modules/user/user.module)
Change the last two lines to:
<?php
$output_form = drupal_get_form($form_id);
print render($output_form);
?>

Resources