8

I am using cakephp v3.3.3

After using cake bake to auto-generate code, I have this php file BallsTable.php which has the initialize() function below;

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('balls');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->belongsTo('Baskets', [
        'foreignKey' => 'basket_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('BallRecords', [
        'foreignKey' => 'ball_id',
    ]);
}

However, the above initialize() function does not allow me to do cascading delete.

I need to manually modify initialize() like below to enable cascading delete. But this is cumbersome as the file gets overwritten every time with a new cake bake.

    $this->hasMany('BallRecords', [
        'foreignKey' => 'ball_id',
        //http://book.cakephp.org/3.0/en/orm/deleting-data.html
        //manually add the 2 lines below to enable cascading delete
        'dependent' => true,
        'cascadeCallbacks' => true,
    ]);

Is there some way to configure cake bake to auto-generate code that manually enable cascading delete?

EDIT: A bounty was added to invite answers that configure bake templates.

  • 2
    Please see [Customizing the Bake Templates](http://book.cakephp.org/3.0/en/bake/development.html#customizing-the-bake-templates) in the manual. You can make them do anything that you want! – Greg Schmidt Sep 08 '16 at 18:58

1 Answers1

0

Unfortunately... No...

From Cake Back Model generator you can configure association with Cascading option.

You can check with help of cake bake model option by typing command bin/cake bake model --help

$ bin/cake bake model --help

Welcome to CakePHP v3.3.3 Console
---------------------------------------------------------------
App : src
Path: C:\wamp\www\student_logic\src\
PHP : 5.5.12
---------------------------------------------------------------
Bake table and entity classes.

Usage:
cake bake.bake model [subcommand] [options] [<name>]

Subcommands:

all  Bake all model files with associations and validation.

To see help on a subcommand use `cake bake.bake model [subcommand] --help`

Options:

--help, -h             Display this help.
--verbose, -v          Enable verbose output.
--quiet, -q            Enable quiet output.
--plugin, -p           Plugin to bake into.
--force, -f            Force overwriting existing files without
                       prompting.
--connection, -c       The datasource connection to get data from.
                       (default: default)
--theme, -t            The theme to use when baking code.
                       (choices: Bake|Migrations)
--table                The table name to use if you have
                       non-conventional table names.
--no-entity            Disable generating an entity class.
--no-table             Disable generating a table class.
--no-validation        Disable generating validation rules.
--no-rules             Disable generating a rules checker.
--no-associations      Disable generating associations.
--no-fields            Disable generating accessible fields in the
                       entity.
--fields               A comma separated list of fields to make
                       accessible.
--no-hidden            Disable generating hidden fields in the entity.
--hidden               A comma separated list of fields to hide.
--primary-key          The primary key if you would like to manually set
                       one. Can be a comma separated list if you are
                       using a composite primary key.
--display-field        The displayField if you would like to choose one.
--no-test              Do not generate a test case skeleton.
--no-fixture           Do not generate a test fixture skeleton.

Arguments:

name  Name of the model to bake. Can use Plugin.name to bake plugin
      models. (optional)

Omitting all arguments and options will list the table names you can
generate models for
Community
  • 1
  • 1
Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
  • Sorry for not being clear in my question. The problem is not with my model php file. I would like to configure cake bake such that the auto-generated php code by cake bake enables cascading delete by default. –  Sep 08 '16 at 06:47
  • Thanks for the answer. Bad news but it's the truth. Hope the cakephp developers do something about it. Sigh ... –  Sep 08 '16 at 07:00
  • 1
    I dont know, who had down voted this answer.. I am very surprised.. if this option available in cake back let me know by comment.!!! – Haresh Vidja Sep 08 '16 at 07:50
  • I upvoted and selected your answer as the answer. If there is good news, I would love to know too. –  Sep 08 '16 at 07:52
  • 1
    @user91579631, I know you have up voted.. but others some person down voting without any comment or reason. – Haresh Vidja Sep 08 '16 at 07:53
  • Haresh Vidja, I have experienced that several times on Stack Overflow. Downvote without explanation. But most members don't do that. So, I am still very happy with the SO community. It will be nice if those who downvote can come out with better answers. –  Sep 08 '16 at 07:55
  • @user91579631, just I want to know you have unmark this answer from right? – Haresh Vidja Sep 13 '16 at 13:05
  • Yes, sorry about that. I set a bounty to attract answers that use bake templates to provide the solution. Your upvote remains. –  Sep 13 '16 at 13:45
  • 1
    @user91579631, ahh its okey :) – Haresh Vidja Sep 13 '16 at 14:10
  • @user91579631, thanks for your bounty, please mark as correct again if you want. :) – Haresh Vidja Sep 19 '16 at 09:37