CakePHP controller name in behavior - cakephp

I created relation Post hasMany Photos, Photos actsAs ImageBehavior.
How put to $data['Photo']['model'] = 'Post'? Automated?

I'm not sure what you are asking about but when you have a form, you can simply add to your $this->data['Photo']['model'] any value you want with hidden fields.
// photo/add.ctp:
$this->Form->create('Photo');
$this->Form->input('model', array('type' => 'hidden', 'value' =>'yourvalue'));
$this->Form->end('Submit');
Update
You can set this value after the form was submitted, so even if someone will replace the hidden field value you can just check it.
function add(){
if(!empty($this->data['Photo']['model']){
$this->data['Photo']['model'] = "yourvalue";
$this->Photo->save($this->data));
}
else
rest of the code...
}
rest of the code

Related

hard-code Form input field in cakephp so user can't edit it?

I have following form field in a registration form in cakephp. I want to make it 'hard-coded', so user can't edit it
echo $form->input('name', array('label' => __('Name *', true)));
Then don't add it to the form.
Those fields should be added in the controller (or even beforeValidate/beforeSave model layer) then right before saving:
if ($this->request->is('post')) {
$this->User->create();
// add the content before passing it on to the model
$this->request->data['User']['status'] = 1;
if ($this->User->save($this->request->data)) {
...
}
}
See "default values - hidden" here.
You can set the readonly property:
echo $form->input('name', array('label' => __('Name *', true), 'readonly' => true));
However, this only affects the UI, and so you still have to apply mark's answer to ensure the value doesn't get changed by the user.
Two options:
hard code the value before the save
use white list
If you want the field to be a read-only, from the moment it is set. use white list. this way - it doesn't matter if the user will submit the field or not. cake won't save it.
$white_list = array('title', 'category');
$this->Model->save($data,$validate,$white_list);
The other solution is as mark coded it:
$this->request->data['User']['status'] = 1;
if ($this->User->save($this->request->data)) {
...
}
Any solution should mix a UI indication that the field will not be changed. tho a good UX will not allow it in the first-place.

how to post values in cakephp 2.x and show the filtered records in another page

I have designed page in cakephp contains form which input filed name 'id' and submit button. I want to display the data filtered by the 'id' in view page. Please give me an example with code for this.
So you wanna search into a database for this ID and return back all stored data to the view?
First of all,you have to retrieve your data. See here: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
Secondly, you have to use
$this->set('var_name', $var_name_containing_found_data);
and finally you can handle your data into the view by manipulating as you wish the $var_name variable.
Put this action in your controller:
public function your_action($id = null) {//your action
if ($this->request->is('post')) {
$search = $this->YourModel->find('all', array('conditions' => array('id' => $this->request->data['id'])));//assuming id is submitted like you said $_POST['id']
$this->set('search', $search);
}
}
You can access the search variable in your view (your_view.ctp) like this $this->search
assuming your cakephp version is 2.x

CakePHP hasMany checkbox

I have two tables: Ingredients and Customers. The relationship between them is that Customers hasMany Ingredients. By default when doing the cakebake using the console, the only way to change them is by assigning an ingredient to the customer in the Ingredients page. However, I want to have in Customers page a checkbox list of Ingredients that can be assigned. Is it possible to do this? If yes, how?
edit:
What I have done until now is that I add this code to my add.ctp:
echo $this->Form->input('Ingredient',
array('label'=>'',
'type'=>'select',
'multiple'=>'checkbox',
'options'=>$ingredients));
However, it gives me "Undefined variable: ingredients" error when I tried to open the add view.
You want and need a HABTM relationship. Different customers can access and use the same ingredients. Look at the docs here yours would be very similar to different Posts using same Tags.
If you are getting this error:
"Undefined variable: ingredients"
It sounds like you haven't declared this variable in your controller, and set it so that the view can use it. Without knowing your code, you would probably need do do something like this (please note I am guessing what your application structure looks like and have not tested this code).
CustomersController.php
// The controller action for your view
public function view() {
// Get the ID and name of all your ingredients
$ingredients = $this->Ingredient->find('all', array(
'fields' => array('id', 'name'),
'order' => 'name',
'recursive' => -1
));
// We will use this array to store all the HTML select options
$ingredientOptions = array();
// Loop through all the ingredients and add them to the select
// options in a format that is suitable for CakePHP to use in
// the view to build your HTML select menu.
foreach ($ingredients as $i) {
$ingredient = $i['Ingredient'];
$ingredientOptions[$ingredient['id']] = $ingredient['name'];
}
// Make the variable available to the view
$this->set('ingredients', $ingredientOptions);
}

how to add hidden value from registeration from in db field in cakephp

I have one registration form. I want to pass hidden value in that form. I have created one field in database 'lang'. In registration form I did like this: echo $form->hidden('lang', array('value' => '1')); But its not saving the value in db. Sorry I have no experience in cakephp, so please if anybody help me in this with all processes. Thanks
You should not just pass hidden stuff through the form just for the heck of it.
If you can, add those values prior to actually saving.
See http://www.dereuromark.de/2010/06/23/working-with-forms/ for details
Basically, you do:
if ($this->request->is('post') || $this->request->is('put')) {
$this->Post->create();
// add the content before passing it on to the model
$this->request->data['Post']['lang'] = '1';
if ($this->Post->save($this->request->data)) {
...
}
}

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