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.
Related
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));
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
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.
My Self Joined Categories table is as follows:
id, name, description, parent_id
I used Cake Bake to generate the Model, Controller and Views. Model
has the $belongsTo and $hasMany association set up. In add() of the
controller,
$parentCategories = $this->Category->ParentCategory->find('list');
$this->set(compact('parentCategories'));
is present. In the add view, the cake bake generated form is:
<?php
echo $this->Form->input('name');
echo $this->Form->input('description');
echo $this->Form->input('isincome');
echo $this->Form->input('parent_id');
?>
When I run in browser, the parent_id field is getting a drop down, but
it is not being filled with any data. I used
<?debug($parentCategories);?>
in the add view, and it happily outputs
Array
(
[1] => Entertainment
[2] => Groceries
)
But this array is not being used for filling that drop down by the
Form helper. What should I do? Is this a bug with Cake's Form helper in 1.3? It never occurred in 1.2...
When adding an input for field_id, the form helper looks for a variable called $fields. I.e., the name without _id and pluralized. $parentCategories does not fit that description, so it's not used. $parents would be.
Second, $this->Category->ParentCategory is the same as $this->Category. Both reference the Category model. No need to go through ParentCategory.
Third, it's not usually a good idea to join a Tree model to itself. You'll understand why when you start to query with higher recursive settings. You should instead make it a proper Tree and use the TreeBehavior methods to query it.
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.