Pagination error when using with element - cakephp

I habve a view from table which is working fine with pagination (Paginator->sort()) when I am using it as a view. But when i changed is as a element, it will throw errors:
Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE\cake\libs\view\helpers\paginator.php, line 194]<br>
Warning (2): array_merge() [function.array-merge]: Argument #2 is not an array [CORE\cake\libs\view\helpers\paginator.php, line 194]<br>
Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE\cake\libs\view\helpers\paginator.php, line 378]<br>
Warning (2): array_merge() [function.array-merge]: Argument #2 is not an array [CORE\cake\libs\view\helpers\paginator.php, line 378]<br>
The code is exacty same as before, but I changed of course some of the code as follows:
Model:Added next statement
var $helpers = array('Paginator');
Controller: changed the paginate clause to return values to element
return $collaborations = $this->paginate('Collaboration');
Element: added request action into start of the element and the other line is how I call the parination sort
<?php $collaborations = $this->requestAction('/collaborations/calendar'); ?>
<?php echo $this->Paginator->sort('Pvm','Collaboration.start_date'); ?>
Why can't my pagination sort functionality work? Do I have declare something else? I did some Googling and I figured out that paginator might have problem to see/find datamodel (to be declared somewhere) or it would need some parameters to be assigned into it?
Thanks in advance :)

Check cakephp book: http://book.cakephp.org/1.3/en/view/1231/Pagination
You have to set up the paginate variable in your controller and then use the method paginate to populate the data in the view.
Helpers should be configured in the controller not in the model.
Example, your controller class should look like:
class ColaborationController extends AppController {
var $paginate = array( 'limit'=>25, 'order'=>array('Colaboration.start_date'=>'asc'));
function some_action() {
$this->set('collaborations', $this->paginate('Collaboration'));
}
}
And your view:
<?php echo $this->Paginator->sort('Pvm','collaboration.start_date'); ?>

I got bored trying to resolve this problem. I changed the whole application logic around and now it works fine. No problems with paginations in elements (no pagination elements at all) and I don't have to use requestAction method - I read that it is not good for performance and should be avoided. Thanks all for your help :)

Related

Cakephp 3 dynamically build contain

How can I dynamically build the contain in the new cakephp 3 query builder. This is what I have now:
$query = $dbTable->find()
->select($contain['select']['fields'])
->contain(function($q) use($array,$contain){
$new = [];
foreach($array as $v){
if(isset($contain['contains'][$v])){
$fields = $contain['contains'][$v];
$new[$v] = $q->select($fields);
}
}
return $new;
});
But I am getting several errors with this:
Warning (2): Illegal offset type in isset or empty [CORE\src\ORM\EagerLoader.php, line 198]
Warning (2): strpos() expects parameter 1 to be string, object given [CORE\src\ORM\EagerLoader.php, line 203]
Warning (2): Illegal offset type [CORE\src\ORM\EagerLoader.php, line 223]
Warning (2): Illegal offset type [CORE\src\ORM\EagerLoader.php, line 224]
As already mentioned by Lorenzo, that's not how it works, contain() doesn't accept callables, just look at the docs:
http://api.cakephp.org/3.0/class-Cake.ORM.Query.html#_contain
Also your code would invoke select() multiple times on one and the same query, that wouldn't work anyways.
However, looking at your code it seems that you could simply make use of the fields option, ie build a simple array to pass to contain(). This is shown in the docs, the example however will trigger an error as it seems to be necessary to explicitly set the foreign key field too:
$query->contain([
'Articles' => [
'fields' => ['foreign_key_column_name', 'title']
]
]);
You cannot use contain with a closure inside. If you believe this is a good idea (I think it could be) then open a enhancement request on github.
To get all fields in the table Articles do this in your controller.
$query = $this->modelName->find('all')
->contain('Articles');
If you want a expecify field use this:
$query = $this->modelName->find('list', [
'keyField' => 'id',
'valueField' => 'author.name'
])->contain(['Articles']);
To more informations about: https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html

cakephp Paginator -> sort - model option

I am having some issues sorting the table data by a field of a different model.
From here: http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html
It says I can add in the 'model' option but when I try:
echo $this->Paginator->sort('unit', 'Unit', array('model' => 'Unit'));
I get this error:
Warning (2): array_filter() expects parameter 1 to be array, null given [CORE/Cake/View/Helper/PaginatorHelper.php, line 395]
Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE/Cake/View/Helper/PaginatorHelper.php, line 395]
Any idea what is going on here? The Main / default model is Card and I need to order by the Unit model for one of the column headings.
Thanks
If you are showing records in a list out of some tables, then you can use it via:
<?php echo $this->Paginator->sort('Unit.unit', 'Unit');
It will perfectly work without passing third argument model option.
Just a reminder for newer Versions: Associated models are not automatically loaded in CakePHP3s Paginator. Make sure you include the 'sortWhitelist' Option, see https://book.cakephp.org/3.0/en/controllers/components/pagination.html#control-which-fields-used-for-ordering
Please try below code
echo $this->Paginator->sort('Unit.unit', 'Unit', array('model' => 'Unit'));
Let me know if any.

CakePHP 2.0: Preparing data from a different model by the AppController for a Helper?

I've made a helper, that should output some data from my database in the layout, so this data have to be available everywhere. Now I tried to load and set a variable by the AppController, but it seems I can not use the usual find()-method. Here's my error:
Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\propfe\Controller\AppController.php on line 46
That's how I tried to load the Model, once by the $uses-variable and once by the App::import()-Command:
var $uses = 'Surgeryhour';
App::import('Model','Surgeryhour');
And here's the line 46 of the error:
$this->set('data', $this->Surgeryhour->find(null, '1'));
Any ideas how I can get all this to work?
Try load model like this:
$this->loadModel('Surgeryhour');
$this->set('data', $this->Surgeryhour->find(null, '1'));
Don't need variable $uses.
never use App::import for models
always:
$this->Surgeryhour = ClassRegistry::init('Surgeryhour');

Cakephp 2.0: Htmlhelper throwing array_merge() error when using Html->script()

I am using the following code to implement some jquery-ui functionality:
<?php $this->Html->script(array(
'jquery-ui-1.8.16.custom.min.js',
'jquery-1.6.2.min.js'
),
null,
array(
'inline'=>'false',
'once'=>'true'
)
);
?>
<script>
jQuery(document).ready(function(){
$('.cv .collapsable').click(function() {
$(this).next().toggle('slow');
return false;
}).next().hide();
});
</script>
The target elements/classes are a element and nested elements. The jquery functionality is working, however, at the expense of php array_merge() errors like so:
Warning (2): array_merge() [function.array-merge]: Argument #2 is not an array [CORE/Cake/View/Helper/HtmlHelper.php, line 478]
Warning (2): array_merge() [function.array-merge]: Argument #2 is not an array [CORE/Cake/View/Helper/HtmlHelper.php, line 478]
Warning (2): array_merge() [function.array-merge]: Argument #2 is not an array [CORE/Cake/View/Helper/HtmlHelper.php, line 478]
Am I doing something wrong? Is this a bug? I've tried just adding the <script> tags to the default.ctp layout, but couldn't get the URLs to work out either. I've done this with cakephp 1.3 so I am a little boggled at the problem I'm having now. Any help would be much appreciated.
UPDATE
So this is my complete code:
The PHP:
<?php $this->Html->script(
array(
'jquery-1.6.2.min'
),
null,
array(
'inline'=>'false',
)
); ?>
The jquery:
<script>
jQuery(document).ready(function() {
$('div.collapsable').click(function() {
$(this).next().slideToggle("slow");
return false;
});
});
</script>
I have removed the jquery-ui script to simplify things—when I do, I am only throwing two "array_merge" errors, now. Still, if I remove 'null'—which shouldn't even exist since the function only takes two parameters—I lose all functionality (the errors also go away).
But things get wierder!
I took a look into cake->core->lib->helper->htmlhelper and examined the function; it's got a line that checks to see if the $options array is a bool. If I instead use this code:
<?php $this->Html->script(
array(
'jquery-1.6.2.min'
),
false
); ?>
I lose the capacity to specify either inline = true or once = true however I at least gain full functionality without the array_merge() errors. Is this a bug in 2.0?
Per the documentation page, the $this->Html->script() only accepts 2 parameters. Remove the null and you should be fine.
Also, you shouldn't include the .js at the end of your script file names.
Your code should look like this:
<?php
$this->Html->script(
array(
'jquery-ui-1.8.16.custom.min',
'jquery-1.6.2.min'
),
array(
'inline'=>'false',
'once'=>'true'
)
);
?>

cakephp invalidate element array

I am using cakephp. I have a form with an element array.
For ex:-
<textarea name="data[User][0][description]>
<textarea name="data[User][1][description]>
From the controller, I need to invalidate (manually) the array field if it is empty and need to show errors to the respective field.
What is the correct syntax for invalidating the field if it an element array ?
I know, the following will work for single element . How it will be for an element array ?
$this->User->invalidate("description");
Unfortunately you cannot invalidate the field with that function.
But what invalidate() does?
function invalidate($field, $value = true) {
if (!is_array($this->validationErrors)) {
$this->validationErrors = array();
}
$this->validationErrors[$field] = $value;
}
It just set validationErrors of the model.
So, you can do following in your Controller (but I also appeal you to move that validation in the Model):
$this->User->validationErrors[1]['description'] = 'Your error message';
The following code will invalidate the second description in the list.
HTH
You can type in view:
<?php
echo $this->Form->error("User.1.description");
?>
Thanks Nik,
your answer helped me, but halfway, because my problem was with a compound field by other subfields.
account_number {
bank_code,
bank_office,
check_digit,
account
}
In this case if we need put in validation error in one subfield, this is the solution:
$this->Model->validationErrors['account_number']['bank_code'][0] = 'Your message error';
I hope this help someone.
Regards.
I had similar problem, since it was for admin panel I showed error message on the first level of field i.e. for this portion only.
If you are validation on controller, just create an array of error with field name and error message, set it in controller and display message if in_array($field, $withErrorArray) in view.

Resources