如何在cakephp 3中复制条目

时间:2018-06-13 08:06:39

标签: cakephp duplicates cakephp-3.0 belongs-to

您好我在cakephp中需要重复的条目,我不知道如何。

我尝试获取实体,克隆实体并删除id。当它保存时,它会为我节省一个与他的关系有关的新实体,但不是全部。

    $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)

通过她的广告系列为我节省了一个新的模拟。问题是这些活动没有'Cadenas','Actions'或'Semanas'(All are belongsTo association)。我的$ 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"
}

模拟表:

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'
    ]);
}

广告系列表:

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表:

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'
    ]);
}

1 个答案:

答案 0 :(得分:1)

我必须在create newEntity上声明相关模型:

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

它成功地运作。