Wrong controller is called from Form. Cakephp - cakephp

Have a form.
$this->Form->create(array('controller'=>'bookings','action'=>'book'));
But for some reason, instead of BookingsController it is searching RoomsController for
the specified action.
Please help. Using Cakephp 2.3

TLDR:
echo $this->Form->create(
'Booking', //notice this - the first item
array(
'url' => array(
'controller'=>'bookings',
'action'=>'book'
)
)
);
Explanation:
Probably a good idea to look at the documentation instead of just guessing at it's syntax:
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
:)

Related

Only change the extension of an url, keep query-string

I have a search form, that uses get-parameters to determine what is searched and I want to have the search results as rss-feed.
So I'm using the route rss-extension to have those available and want to generate a link to the rss feed for that search/query.
$this->Html->link(__("Search results as rss"), ['ext' => 'rss']); leaves out all the params and I haven't found a way to extract the get-params in a sane way.
Is there some shortcut to just use the actual current url and change the extension?
The best way I found so far was:
$this->Html->link(__("Search results"), ['ext' => 'rss', '?' => $this->passedArgs]);
Is there a better way?
Yeah, you might want to use
'?' => $this->request->query
Then it only uses the query strings - which is what you want after all.
Below is the code I use, dont know if $this->link work the same way:
$url = $this->params['url'];
echo $this->Form->create(null, array(
'url' => array(
'action' => 'yourAction',
'?' => $url,
'ext' => 'rss'
)
));
echo $this->Form->end('Search Results');

Cakephp change the order of virtualFieldId in find/paginate

I have a model in which I have defined a virtualfield, virtualfield01. And when I call a find function like this.
$this->myModel->find('all', array(
'fields' => array(
'field01',
'virtualfield01',
'field02',
'field03')));
the result always gives me.
myModel=>array(
'field01' => 'value01'
'field02' => 'value02'
'field03' => 'value03'
'virtualfield01' => 'virtualvalue01')
the virtualfield is always output as the last field of the result.
How can I make the order exactly the same to that I make in the find function???
Why wouldn't you just do something like the following in your view, just as #mark mentioned?
$this->Html->tableCells(array(
$var['Model']['field01'],
$var['Model']['virtualfield01'],
$var['Model']['field02'],
$var['Model']['field03'],
));
There's no 're-arrangement action', you're just telling the data how to display, which is why it is a 'view'.
Simple is that as mark Mentioned. Order doesn't matter.

how to change PaginatorHelper direction

how to change PaginatorHelper direction? it's generated links sort asc, and i want to change in to desc.
i wrote that code at my .ctp file, but no changes..:
<?php $this->Paginator->options(array('direction' => 'desc')) ?>
how to change that direction? can I change it in controller? or i should change at view?
my helpers:
public $helpers = array ('Html', 'Form', 'Paginator');
thanks.
Setting defaults for pagination is outlines in the documentation. You can also pass params to the paginate() call in your controller:
$this->paginate = array(
'conditions' => array('Recipe.title LIKE' => 'a%'),
'limit' => 10,
'order' => 'Recipe.created'
);
I know this is a late response, but I wanted to answer the question in case someone comes here looking for the correct answer as there has been no answers that are correct.
To change the default of the pagination direction in the paginator, there are two ways you can make this happen.
The Helper
If you are using the PaginatorHelper, you can set the default when you create the link in the view:
echo $this->Paginator->sort('Link Name', 'Model.columnName', array('direction' => 'desc')) ;
This will print the link that will sort the column based on the direction you identify in the options array as indicated above. If the options array is left out, it will default to 'asc'.
The Component
If you want to set the default direction on the PaginatorComponent, you do so like this:
$this->Paginator->settings = array(
'direction' => 'desc',
'sort' => 'Model.column',
);
Keep in mind this does two things. It will automatically sort your data by the Model.column identified in the sort option and it will do it in the direction specified.
NOTE: You cannot simply add the direction. It requires BOTH to be set in order to work.

$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.

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