Converting named parameters to GET parameters for pagination at CakePHP 2.2 - cakephp

I am trying to convert named parameters to GET parameters for pagination at CakePHP 2.2 following the instructions given in the documentation but it is not working.
Instead of this:
http://localhost/cake/posts/yourPosts/page:2?url=posts%2FyourPosts
I want this:
http://localhost/cake/posts/yourPosts/?page=2&url=posts%2FyourPosts
The thing is, when i submit a form using GET method, i don't want to retain the current page, and currently, it is doing it by default because it is not a normal param but a named param.
(?url=posts%2FyourPosts is added automatically with the GET method)
I have tried to to this in my view but it stills passing the parameter as a named one:
$this->Paginator->options(array('convertKeys' => array('page')));
echo $this->Paginator->next(' >', array('class' => 'button next right'), null, array('class' => 'next button'));
What am I doing wrong?

you should fix the issue at its source, not cloaking it:
http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#pagination-with-get-parameters
public $paginate = array(
'paramType' => 'querystring'
);
in your controller

Related

Option usage configureListFields in SonataAdminBundle

unfortunately documentation doesn't cover how to use options available in configureListFields ListMapper when you add fields to the list.
This is my basic code
$listMapper
->add('myField', null, array(
'label' => LabelHelper::LABEL_MY_FIELD,
'code' => // what should I put here ... $this->methodName() is not working
))
I want to use 'code' option (docs - section 7.2.1), because I would like to customize just one filed final display. I don't want to rewrite the row template.
As stated in the code section I've tried simple method that returns string, but nothing happened in the list view (I've cleared cache etc.).
Answer is simple. You just put method name without brackets
$listMapper
->add('myField', null, array(
'label' => LabelHelper::LABEL_MY_FIELD,
'code' => 'methodName'
))
Method should be stored in corresponding Entity class

Cakephp 2.5 Postlink hidding the model atribute name at the url

I am using a cakephp 2.5 postlink method inside a view file :
$tableRow['Model.modelatribute'] = $this->Form->postLink(
$data['Vehicle']['plate'],
array('controller'=>'somecontroller',
'action' => 'somemethod',
'Model.modelatribute' => base64_encode($data['Vehicle']['plate'])
),
array('confirm' => 'Look at vehicle '.$data['Vehicle']['plate'])
);
I would like not to show the model atribute name on the url bar. After clicking on the link and being redirected, the url shows :
somemethod/Model.modelatribute:vSpEeTIweQ%3D%3D
Can i hide the model atribute name, using postlink method o cakephp 2.5 ?
ThankĀ“s in advance
If you simply want to pass the value of Model.modelatribute as a parameter, just leave off Model.modelatribute in your routing array. If you want to pass the value without occouring in the url you can use the data option of postLink.
echo $this->Form->postLink(
$data['Vehicle']['plate'],
array(
'controller'=>'somecontroller',
'action' => 'somemethod',
base64_encode($data['Vehicle']['plate']) // Routing array without modelname
),
array(
'confirm' => 'Look at vehicle '.$data['Vehicle']['plate'],
'data' => array(
// Data option of postLink method
'Model.modelatribute' => base64_encode($data['Vehicle']['plate'])
)
)
);
Well, you put in there. If you don't want it to be there simply don't put it in the URL. You can alternatively put it into the body of the POST request.
If you're concerned that people can use the model name for something and you want to hide it for that reason, then it is a very bad approach called security through obscurity. Instead you want to ensure that you validate that only values you want are passed and processed. So check your incoming data, always, from everywhere.

$this->find not working in cakePHP

I have a model with the name Deal class Deal extends AppModel
Now in my controller I call a method in the Deal model called getDeal()
$dealInfo = Deal::getDeal($dealID);
I want the info returned to me but the var_dump displays blank
function getDeal($dealID){
$deal = $this->Deal->find('first', array(
'conditions' =>
'Deal.id' =>$dealID
) ,
'fields' => array(
'Deal.id'
) ,
'recursive' => 1,
));
}
This is the first time I'm working in cakePHP, so this question might sound a bit dumb
When you're just using an id as your find condition you can use CakePHP's dynamic finder methods.
Dynamic finders work like this.
$this->Model->findByModelFieldName();
In your example it would be
$this->findById($dealId, array(
'fields' => array('Deal.id'),
'recursive' => 1
));
I don't know if I'm just being mental, but is this simply because you're not returning anything from getDeal()?
Try adding return $deal; and see if that helps. Your question doesn't state exactly where you're doing the var_dump so I might be well off.
Edit:
As per the discussion with you and 8vius, we've established that this isn't right, and you simply need to change $this->Deal->find() to $this->find() because its being run from the model.
Check your function declaration, $deal_id does not exist and as you can see from the parameter you pass to the function which should me $deal_id and not dealID. So you have a misdeclared function calling find with a variable that does not exist.

How to pass data in url using cakephp?

I wanted to now if there is a way to pass a simple string inside url and cakephp some how put it inside one of the inputs of my form without any code writing on view side?
I tried calling this->set("bla","bla"); the field name is bla
but nothing changed in view
As I understood the question you want to have something like this:
in the url you have something like:
http://yourserver.com/controller/action?search=string
and you want to put this "string" in the search field, right?
Then let's imagine that your field's name is data[search_field]. (I am skipping the model, because for my example it's not needed, but it's possible the name to be data[Model][search_field]).
Then in your controller's action you have to do following:
$this->data['search_string'] = $this->params['url']['search'];
You can pass values in the url by using html helper. Try:
echo $this->Html->link('View Some Page', array(
'controller' => 'yourController',
'action' => 'view',
1, // id
'?' => array('yourField' => 'valueToPass'))
);

CakePHP AutoComplete Question

I am working on a book review application and I am using autoComplete to search for titles in when creating a review. The review model has an associated book_id field and the relationship is setup as the review hasOne book and a book hasMany reviews.
I am trying to pass the Book.id (into the book_id field), but I want to display Book.name for the user to select from. With the default setup (accomplished via CakePHP's tutorial), I can only pass Book.name. Is it possible to display the name and pass the id?
Also, I am passing it via the following code in the create() action of the review controller:
$this->data['Review']['book_id'] = $this->data['Book']['id'];
Is that the proper way to do it in CakePHP? I know in Ruby on Rails, it is automatic, but I can't seem to make it work automagically in CakePHP. Finally, I am not using the generator because it is not available in my shared hosting environment... so if this is the wrong way, what do I need other than associates in my models to make it happen automatically?
Thanks for the help and I promise this is my question for awhile...
UPDATE- I tried the following, but it is not working. Any ideas why?
function autoComplete() {
$this->set('books', $this->Book->find('all', array(
'conditions' => array(
'Book.name LIKE' => $this->data['Book']['name'].'%'
),
'fields' => array('id','name')
)));
$this->layout = 'ajax';
}
The problem is that when I use the code above in the controller, the form submits, but it doesn't save the record... No errors are also thrown, which is weird.
UPDATE2:
I have determine that the reason this isn't working is because the array types are different and you can't change the array type with the autoComplete helper. As a workaround, I tried the follow, but it isn't working. Can anyone offer guidance why?
function create() {
if($this->Review->create($this->data) && $this->Review->validates()) {
$this->data['Review']['user_id'] = $this->Session->read('Auth.User.id');
$this->Book->find('first', array('fields' => array('Book.id'), 'conditions' => array('Book.name' => $this->data['Book']['name'])));
$this->data['Review']['book_id'] = $this->Book->id;
$this->Review->save($this->data);
$this->redirect(array('action' => 'index'));
} else {
$errors = $this->Review->invalidFields();
}
}
FINAL UPDATE:
Ok, I found that the helper only takes the find(all) type or array and that the "id" field wasn't passing because it only applied to the autoComplete's LI list being generated. So, I used the observeField to obtain the information and then do a database lookup and tried to create a hidden field on the fly with the ID, but that didn't work. Finally, the observeField would only take the characters that I put in instead of what I clicked, due to an apparent Scriptaculous limitation. So, I ended up going to a dropdown box solution for now and may eventually look into something else. Thanks for all of the help anyway!
First of all, $this->data will only contain ['Book']['id'] if the field exists in the form (even if it's hidden).
To select something by name and return the id, use the list variant of the find method, viz:
$selectList = $this->Book->find('list', array(
'fields' => array(
'id',
'name'
)));
$this->set('selectList', $selectList);
In the view, you can now use $selectList for the options in the select element:
echo $form->input('Book.id', array('type' => 'hidden'));
echo $form->input('template_id', array(
'options' => $selectList,
'type' => 'select'
));

Resources