CakePHP re-baking concerns - cakephp

I'm new to CakePHP. I added some columns to my table abc. Is it possible not to re-bake my model Abc and its corresponding contoller and views? I already made a lot of changes and I think baking it again will overwrite my existing files. Thanks.

Baking is only needed when you want to generate automatic code. If you have already modified your Models/Controllers/Views and have added new columns in your database, then you will have to edit manually your Models/Controllers/Views to make relevant modifications.
You will probably want to edit Models to add validation rules for the new columns, to edit Views to add new inputs for these columuns. Controllers will probably remain unchanged.

Related

Cakedc users plugin new table entry

I am using Cakedc Users plugin and I want to add a small entry to the Users table which is "Balance" (integer)
I read the extending part in the documentation and I honestly got dizzy from all the modifications that I have to do
I don't want to rewrite the whole thing just for a small entry, Is there anyway I can add it to the table with minumum modification or rename another entry that I don't need, like "tos_date" or something
Well, let me try to help about how extending the model should be:
Modify the users table (via migrations or manually) and add the columns you want.
Copy the template files with the forms, from the plugin itself to your app under the folder src/Template/Plugin/CakeDC/Users/name_of_the_controller/name_of_the_action, then modify the forms to add a new control for your custom column
You're done
Thanks!

Add content of form to 2 different tables cakephp

I am building a form with Cakephp 2.x and I want to submit some of the data that is received when submitting it to table A and the other data to table B. Does anyone know how to achieve this in cakephp?
I believe you need to look at Cakephp's model documentation. Looking at it now it seems that saveMany could help you with that. http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array
Yes. You should do what you want follow below way:
1. Create method receiveData() in FooController.
2. Create view file receive_data.ctp in folder foo.
$this->Foo->create();
$this->Foo->input('....', array(....));
// other input.
$this->Foo->end();
3. In FooController\receiveData(), When submit, you have data array $myDataArray like this: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html#adding-posts
(Notice: It is Foo controller).
4. Before you save data from submitted data, you maybe need manipulation with submitted data array.
5.
You must call Model Bar inside FooController by:
$uses = array('Bar');
You will save to other table by:
$this->Bar->save($myDataArray);
(Notice: It is Bar Model).
Do Table A and Table B represent two different Models?
if yes saveAssociated might help you.
http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto
If you want to save the same kind of model n times then saveMany is what you want to do.

Assigning field values before saving on different models

I continue building my CakePHP application. Now I've created all the database schema in MySQL, run some "cake bake all" and I have lots of models, views and controllers, and I'm gonna personalize them.
In many of my models I have this fields:
company_id
created_by
modified_by
As you can understand, the first field is the id of the owner of the "row", created_by is the id of who created the row and modified_by the latest person who updated it.
I know I can create a beforeSave filter in the model and update all the data (I suppose that I can know if I'm creating or updating a row, isn't it?), but just now I have 15 different models and I hope the app will grow more, so it's a lot of repetitive code to write. And, it breaks the DRY principle.
And my question is: Is there any "elegant" way to solve this problem? Maybe creating a class extending AppModel with a beforeSave filter for updating the fields, and that all my models inherit from the new Model class instead of AppModel?
Thanks for your help!
Make it a behaviour and load it for models that need that functionality.
See http://book.cakephp.org/2.0/en/models/behaviors.html#creating-behaviors
I think the most appropriate way is to create Behaviors.
You can set up the beforeSave callback in the behavior like what you have in your model.
Here is the link to the documentation http://book.cakephp.org/2.0/en/models/behaviors.html#behavior-callbacks
You can also check as example dereuromark's WhoDidItBehavior.

cakephp AVOIDING repetitive .ctp files

Many models in my app are similar, and I've automated the creation of each CTP for the standard CRUD for each. In other words, the ctp files themselves for each model are identical. I pass the fields used to create the form as an array to a helper. I find though I'm just creating the same files over and over in separate view directories. Is there a way I can refer to say 1 add.ctp for each of the model controllers? I hope my question is clear enough. Thanks.
$this->render('/controller/view');
You can render any view from any controller, so if you want to create one "index" view and its generic enough that you are just passing $data in, you could render the same view each time.
You could take it one step further and create that view in your elements folder to completely detach it from your controllers.
http://book.cakephp.org/view/980/render
https://github.com/infinitas/infinitas/blob/beta/app_controller.php#L389

Can not Bake table model, controller and view

I developed small CakePHP application, and now I want to add one more table (in fact, model/controller/view) into system, named notes. I had already created a table of course.
But when I run command cake bake model, I do not get table Notes on the list. I can add it manually, but after that I get some errors when running cake bake controller and cake bake view.
Can you give me some clue why I have those problems, and how to add that new model?
I would also check your app/config/database.php to ensure that you are using the correct database configuration. You may well have added the table to a different database perhaps and the bake is picking up the other database. Also, and this may be obvious, but check you are in the right project, it's easy to be in a different folder and not realise, especially if you have lots of projects.
I'm not aware of a limit on the bake listing. I would check your database to make sure the table exists and has some columns. You can always open up the console bake script and check for a limit and increase it if needs be.
I found solution!
I had to delete all from cache directory, /app/tmp/cache/models
Now it works!
:-)
When you say added it manually, do you mean added the note.php model? If not, you may want to try that. Verify that the model name is correct for the following:
file name: note.php
class name: class Note extends AppModel
table name: notes
Also, be sure the notes table has the id column and it is set to primary key.
If this does not push you in the right direction, please post your notes table schema here. Also, have you had success in baking other things in your app? Have you upgraded anything?
Please change the following farameters to bake:
For Controller:
/cake/console/libs/tasks/controller.php
function listAll($useDbConfig = 'default') {
change to :
function listAll($useDbConfig = 'YOUR DB CONFIG NAME') {
NOW DO CAKE BAKE for CONTROLLER! ENJOY!

Resources