cakephp pass foreign key to add method in controller - cakephp

i have two tables persons and numbers. persons has a hasMany association with numbers, while numbers has a belongsTo association with persons. i also have a view method in the persons controller which displays the information about that person (including all the numbers associated to him). i also want to add an link to add a new number to that person. i was successful in passing the primary key of the person to the add method, but cant include it to the form in the add's view. i tried placing it in $this->data and adding a hidden field to the view, and using set() and assigning it as a default value to a hidden field in the view, but to no avail. any thoughts are appreciated.

I prefer passing the person ID in the GET request itself.
In your numbers controller.
function add($personId = null) {
//code
$this->set('personId', $personId);
}
In your view, modify your form helper to post to a different URL with the person ID on the end
echo $this->Form->create('Number', array('url' => '/admin/number/add' . $personId));
Now when the form posts the data, the $personId is passed to the method so you don't need to access it via the data variable.

Related

CakePHP How can I save many records in a link table at once for user - map - attributes?

I have three tables/models:
User(id)
Map(id, user_id, attribute_id)
Attribute (id, name)
Map belongsTo the others, the others hasMany Map.
I'd like the user (via user controller and user view) to create many links to associations at once. How can I do this, assuming that the user forms (add/edit) have 10 attribute fields, all linking to the same table?
I'd need to save up to 10 records in Map in one go. To start with, I'm unsure what the field should be - create('Attribute.name')? Also, cake outputs the same input name to each input as they point to the same field - what's the best way to fix this?
I have already read the relevant documentation, but didn't get much from it.
Thanks!
You can use saveAssociated() method. Prepare an input array according to the instructions given in the link and directly use $this->User->saveAssociated($your_array, array('deep' => true));

checkboxes for table records

I want to let user to publish or unpublish some articles at once. So, in articles list view, I want to put a checkbox beside each row (article) and send these checkboxes to controller.
I tried to use $this->Form->input('status') after each row in my loop, but it created same checkbox for each article. (inputs name and id are the same)
How to create an array of checkboxes or something like this? And how to check them in controller?
Note: Each article has a status field, which is a tinyint 1 character field. (so Cake can understand it's a checkbox)
You will need to specify an 'index' for each input.
In stead of this:
$this->Form->input('Article.status');
Use this:
$this->Form->input('Article.0.id');
$this->Form->input('Article.0.status');
$this->Form->input('Article.1.id');
$this->Form->input('Article.1.status');
// ....
$this->Form->input('Article.xxx.id');
$this->Form->input('Article.xxx.status');
It's the 'index' is just a counter to make sure that 'unique' inputs are generated. However, it is important that each row contains an input for the id of that row; CakePHP will need that to determin which record it should update the status for.
Further reading
Documentation on the naming of fields/inputs can be found here:
FormHelper field naming conventions
CakePHP - Create a form which edits multiple rows of the same model

How to Build one form with input of many views

I have 3 tables/models:
Customer
Address
City
Each one have their views,but I need to add Address and City to the Customer view and there will be all the inputs from each view and it will result in only one Form. I read some stuff about include these views in Customer's view using elements.
What is the best way for doing that?
If I'm reading you right, you have 3 tables, customer, address & city, and want to read them all when you have a user view?
Sounds like you want to set up A CakePHP association, likely a hasOne relationship.
Edit: Here's how you'd use an element to accomplish this:
// Form
<?php echo $this->element('cityselect'); ?>
// app/View/Elements/cityselect.ctp
<?php $cities = $this->requestAction('/cities/index'); ?>
// Use $cities to populate element form
You'll probably want to write an action for your cities/address controllers that returns key-pairs to easily populate your select field.

Cakephp:How to get id of the first form when saving my last of three forms

High guys,I have three models that i am using in Cakephp,I create 3 views for my models which are linked by the second model.The problem is how can i retrieve the id of the first model when i want to do a final save?
Under the assumption that you're saving the three models in three separate page requests, it should suffice to simply store the saved model data in your session. That way you can read it in the 2nd and 3rd page request and include the necessary data in your forms and processing.
If you're doing it all in the same request, then Moyed's idea works, you can get the ID of a saved row through the model:
$this->Model->save($data);
$saved_row_id = $this->Model->id;
You need to know the id of first model.
If you use the the save() method you can get the id like so:
$this->Model->save($data);
$id = $this->Model->id;
OR
Cake's model class has a function that gets the last inserted id:
$this->Model->getLastInsertID()
You just need to replace Model with the name of first model

cakephp habtm relationships with input rather than select box?

I'm creating a form in cakephp that has a typical HABTM relationship. Let's say that I'm developing an order form with a coupon code. From a logic point of view, I need to accept a text-entry for the coupon code, so the incoming data would not be the proper primary key, but rather a different field.
I would then need to validate that data, and retrieve the proper primary key id, and then update the orders_coupons table with that coupon_id, and order_id.
When using a select box the value would always be the proper coupon_id, but where is the appropriate location for me to put the logic to handle this? Should I modify the data using beforeSave?
your question isn't very clear b/c it sounds like you can both specify the coupon via a select box or in just a free form text box.
My inclination is to add a new method to the Model that would update the record with the "human readable key". So the function would first read the coupon_id from the DB, and then do the update.
As you said, you'll just have to look up the coupon id...
// assuming $data looks like:
// array('Order' => array(...), 'Coupon' => array('code' => ...))
$coupon_id = $this->Order->Coupon->field('id', array('code' => $data['Coupon']['code']));
if (!$coupon_id) {
// user put in a non-existing coupon code, punish him!
} else {
$data['Order']['coupon_id'] = $coupon_id;
$this->Order->save($data);
}
If I get this correct, this is pretty much the same as tagging?! (There is a text input box for habtm items and the string is submitted to the controller without the ids).
If so, I would recommend to split the processing. Submit the data to the controller and then pass the coupon-string to a proper function in the coupon-model, that gets the coupon-ids (saves new items) and returns them back to the controller to save the complete habtm-data.

Resources