2

Hi I need duplicate entries in cakephp and I don't know how.

I tried getting the entity, clonning the entity, and removing id. When it saves, it saves me a new entity with his relationships, but not all.

    $simulation = $this->Simulations->get($id_simulation, ['contain' => ['Campaigns' =>['Semanas' => ['SemanasPond'], 'Actions', 'Cadenas']]]);
    $new_simulation = clone $simulation; // Create a new record
    $new_simulation->id = null;
    $new_simulation->created = null;
    $new_simulation->modified = null;
    $new_simulation_object = $this->Simulations->newEntity($new_simulation->toArray());
    $this->Simulations->save($new_simulation_object)

It saves me a new Simulation with her campaigns. The problem is these campaigns not has 'Cadenas', 'Actions' or 'Semanas' (All are belongsTo association). My $new_simulation_object:

Simulation {#484 ▼
  +"name": "Copia 6 Simulación base(Genérico Adultos (12-06-2018 16:50:29))"
  +"orden": 1
  +"periodo_id": 1
  +"pondera_id": 1
  +"tiposimulation_id": 2
  +"proyecto_id": 212
  +"campaigns_number": null
  +"campaign_count": 3
  +"created_userid": 2
  +"created": null
  +"modified_userid": null
  +"modified": null
  +"campaigns": array:3 [▼
  0 => Campaign {#289 ▼
      +"name": "Nueva Campaña"
      +"orden": 0
      +"simulation_id": 25022
      +"ambito_id": 100
      +"creatividad": 3.0
      +"created_userid": 2
      +"created": FrozenTime @1528822301 {#284 ▶}
      +"modified_userid": null
      +"modified": FrozenTime @1528822301 {#285 ▶}
      +"cadenas": array:1 [▼
          0 => array:17 [▼
                  "id" => 10
                  "parent_id" => null
                  "lft" => 31
                  "rght" => 32
                  "name" => "Onda Madrid "
                  "id_nivel" => 1
                  "ambito_id" => 212
                  "cadena_tipo_id" => 1
                  "visible" => true
                  "orden" => 10
                  "pondera" => true
                  "logo" => "NULL"
                  "created_userid" => 1
                  "created" => FrozenTime @1526428800 {#498 ▶}
                  "modified_userid" => 1
                  "modified" => FrozenTime @1528874497 {#499 ▶}
          "_joinData" => array:7 [▼
        "id" => 20
        "campaign_id" => 204
        "cadena_id" => 10
        "created_userid" => null
        "created" => FrozenTime @1528822389 {#483 ▶}
        "modified_userid" => null
        "modified" => FrozenTime @1528822389 {#476 ▶}
      ]
    ]
  ]
          +"actions": array:3 [▶]
          +"semanas": array:3 [▶]
          +"[new]": true
          +"[accessible]": array:15 [▶]
          +"[dirty]": array:12 [▶]
          +"[original]": []
          +"[virtual]": []
          +"[errors]": []
          +"[invalid]": []
          +"[repository]": "Campaigns"
    }
    1 => Campaign {#509 ▶}
    2 => Campaign {#474 ▶}
  ]
      +"[new]": true
      +"[accessible]": array:17 [▶]
      +"[dirty]": array:13 [▶]
      +"[original]": []
      +"[virtual]": []
      +"[errors]": []
      +"[invalid]": []
      +"[repository]": "Simulations"
}

Simulations Table:

class SimulationsTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('simulations');
    $this->setDisplayField('name');
    $this->setPrimaryKey('id');

    $this->addBehavior('Timestamp');
    $this->addBehavior('CounterCache', [
        'Proyectos' => ['simulation_count']
    ]);

    $this->belongsTo('Proyectos', [
        'foreignKey' => 'proyecto_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('Campaigns', [
        'foreignKey' => 'simulation_id'
    ]);
}

Campaigns Table:

class CampaignsTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('campaigns');
    $this->setDisplayField('name');
    $this->setPrimaryKey('id');

    $this->addBehavior('Timestamp');
    $this->addBehavior('CounterCache', [
        'Simulations' => ['campaign_count']
    ]);

    $this->belongsTo('Simulations', [
        'foreignKey' => 'simulation_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Ambitos', [
        'foreignKey' => 'ambito_id'
    ]);
    $this->hasMany('Semanas', [
        'foreignKey' => 'campaign_id'
    ]);
    $this->hasMany('SemanasPond', [
        'foreignKey' => 'campaign_id'
    ]);
    $this->belongsToMany('Actions', [
        'foreignKey' => 'campaign_id',
        'targetForeignKey' => 'action_id',
        'joinTable' => 'campaigns_actions'
    ]);
    $this->belongsToMany('Cadenas', [
        'foreignKey' => 'campaign_id',
        'targetForeignKey' => 'cadena_id',
        'joinTable' => 'campaigns_cadenas'
    ]);
}

Cadenas Table:

class CadenasTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('cadenas');
    $this->setDisplayField('name');
    $this->setPrimaryKey('id');

    $this->addBehavior('Timestamp');
    $this->addBehavior('Tree');

    $this->belongsTo('ParentCadenas', [
        'className' => 'Cadenas',
        'foreignKey' => 'parent_id'
    ]);
    $this->hasMany('ChildCadenas', [
        'className' => 'Cadenas',
        'foreignKey' => 'parent_id'
    ]);
    $this->belongsToMany('Campaigns', [
        'foreignKey' => 'cadena_id',
        'targetForeignKey' => 'campaign_id',
        'joinTable' => 'campaigns_cadenas'
    ]);
    $this->belongsToMany('Descuentos', [
        'foreignKey' => 'cadena_id',
        'targetForeignKey' => 'descuento_id',
        'joinTable' => 'descuentos_cadenas'
    ]);
}
Sergioh Lonet
  • 831
  • 1
  • 7
  • 9
  • Looks like **https://stackoverflow.com/questions/42020863/cakephp-3-x-saving-nested-deep-association**. Whenever adding variable dumps, please make sure that you show all relevant data, `cadenas` most likely is just an array of arrays as mentioned in the linked questions answer. – ndm Jun 13 '18 at 08:59
  • please edit your question adding the relationships between your tables – arilia Jun 13 '18 at 09:05

1 Answers1

2

I must declare associated models on create newEntity:

$new_simulation_object = $this->Simulations->newEntity($new_simulation->toArray(), ['associated' => ['Campaigns' =>['associated' =>['Cadenas', 'Actions', 'Semanas' => ['associated' => ['SemanasPond', 'CampaignsInsertions']]]]]]);

It works succesfully.

Sergioh Lonet
  • 831
  • 1
  • 7
  • 9