Cakephp how does saveall work




















If you think back to our add action, the Account model had no data and therefore no id and we were just creating one and only account, therefore Account. In the case of edit we already know the id of the Account model, therefore the field names must match the actual id from the database. And yes, cake is smart enough to make those inputs as hidden fields.

I hope that now you can see how saveAll can make your life so much easier with so little effort. Skip to content Search for:. Home August 1 Practical use of saveAll part 1, working with multiple models. August 1, vladko. You can also use this format to save several records and their HABTM associations with saveAll , using an array like the following:. Passing the above array to saveAll will create the contained tags, each associated with their respective recipes.

Another example that is helpful is when you need to save many Tags to a Post. In this example, you can see the Recipe. Other ways we might want to present our associated data can include a select drop down list. The data can be pulled from the model using the find 'list' method and assigned to a view variable of the model name. For example, a Recipe can have multiple Tags assigned to it.

In this case, the data is pulled out of the model the same way, but the form input is declared slightly different. The tag name is defined using the ModelName convention:.

Using the preceding code, a multiple select drop down is created, allowing for multiple choices to automatically be saved to the existing Recipe being added or saved to the database. Normally HABTM is used to bring 2 models together but it can also be used with only 1 model, though it requires some extra attention. The key is in the model setup the className. By setting the className to the models name and use the alias as key we avoid those issues.

Creating form elements and saving the data works the same as before but you use the alias instead. For example if you have a Club that has 10 Children associated.

You then update the Club with 2 children. The Club will only have 2 Children, not Also note that if you want to add more fields to the join when it was created or meta information this is possible with HABTM join tables, but it is important to understand that you have an easy option.

HasAndBelongsToMany between two models is in reality shorthand for three models associated through both a hasMany and a belongsTo association. These two examples are almost the exact same. They use the same amount of named fields in the database and the same amount of models.

When your join table contains extra fields besides two foreign keys, you can prevent losing the extra field values by setting 'unique' array key to 'keepExisting'. Additionally, if you used bake in order to create the models, this is set automatically. You can create your database tables as you normally would.

Table names are by convention lowercase and pluralized with multi-word table names separated by underscores. For example, a Model name of Ingredient expects the table name ingredients. New issue. Jump to bottom. Labels defect.

Milestone 2. Linked pull requests. Copy link. It seems that this has happened all the way back since the 1. Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment. Ian Hoar Ian Hoar 1, 19 19 silver badges 36 36 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Dave Dave I've tried it with request and read over that doc several times. The results are the same with or without the request part.

I should also add that if I use save instead of saveAll it will save the title, but not the Url. SaveAll saves nothing. No need to echo here. Just debug. Sign up or log in Sign up using Google.



0コメント

  • 1000 / 1000