# Saving Data associated has Many

**URL:** <https://discourse.cakephp.org/t/saving-data-associated-has-many/12406>\
**Category:** Need Help\
**Created:** [February 2, 2025, 10:26pm UTC](https://discourse.cakephp.org/t/saving-data-associated-has-many/12406 "2025-02-02T22:26:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![efjot](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@efjot](https://discourse.cakephp.org/u/efjot)\
**Post date:** [February 2, 2025, 10:26pm UTC](https://discourse.cakephp.org/t/saving-data-associated-has-many/12406/1 "2025-02-02T22:26:53Z")

</div>

Hi I have 2 table, a Ticket table and a comments table.  
In ticketsTable.php

```auto
 $this->hasMany('Comments', [
            'foreignKey' => 'ticket_id'
        ]);

```

and in commentsTable.php

```auto
$this->belongsTo('Tickets', [
            'foreignKey' => 'comment_id',
        ]);
    }

```

When I read the data, all is fine, e.g in edit function.  
I can run with foreach over all comments and show them.  
$ticket = $this-\>Tickets-\>get($id, contain: [‘Comments’]);

```auto
foreach($comments as $entity)
 {
      echo h($entity->created->nice());
       echo $this->Text->autoParagraph(h($entity->comment));

 }

```

But how to add a new Comment in a form and save it?

---

<div class="post-metadata">

**Author:** ![Rods](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@Rods](https://discourse.cakephp.org/u/Rods)\
**Post date:** [February 3, 2025, 12:20am UTC](https://discourse.cakephp.org/t/saving-data-associated-has-many/12406/2 "2025-02-03T00:20:48Z")

</div>

Thats Another form for comments to create you just make sure that you select the ticket id from cooment form
