---
title: "mehrere Datensätze einer foreach-Schleife speichern"
url: "https://www.typo3.net/d/123222-mehrere-datensaetze-einer-foreach-schleife-speichern"
source: "typo3.net"
tags: ["Extensions","Extbase","Frage"]
created: "2016-10-06"
last_reply: "2016-10-26"
replies: 4
solved: false
page: 1
pages: 1
lang: "de"
---

# mehrere Datensätze einer foreach-Schleife speichern

Extensions › Extbase · 4 Antworten · gestartet 2016-10-06

## Eröffnungsbeitrag

**moep0r** · 2016-10-06 · [Beitrag #1](https://www.typo3.net/d/123222-mehrere-datensaetze-einer-foreach-schleife-speichern/1)

Hallo
Ich bin ziemlich neu bei typo3 und stehe gerade total auf dem Schlauch.
Ich möchte mehrere Einträge aus einem Formular aus einer foreach-Schleife speichern.

Formular:

```html
<h1>{headline}</h1>

<f:render partial="Error" arguments="{object:'timeofday'}" /> 
	
<h3>{survey.title}</h3>

<f:form action="{action}" object="{timeofday}" name="timeofday" arguments="{survey:survey, appiontment:appiontment}" additionalAttributes="{role:'form'}">
	
<f:for each="{survey.appiontments}" as="appiontments">
	<li class="list-group-item">
		<label>{appiontments.appiontmentdate}</label>
		<f:form.textfield property="timevalue" type="time" class="form-control" id="{appiontments}" />
	</li>
	
</f:for>

<f:form.submit value="{submitmessage}" class="btn btn-primary" />
	
</f:form>
```

Mein aktueller Controller speichert jetzt natürlich nur den letzten Wert.
Ich komme aber einfach nicht drauf wie ich jetzt weiter verfahre.

```php
/**
 * @param \Schmidtch\Survey\Domain\Model\Survey $survey
 * @param \Schmidtch\Survey\Domain\Model\Appiontment $appiontment
 * @param \Schmidtch\Survey\Domain\Model\Timeofday $timeofday
 */
public function addAction(
	\Schmidtch\Survey\Domain\Model\Survey $survey,
	\Schmidtch\Survey\Domain\Model\Appiontment $appiontment,
	\Schmidtch\Survey\Domain\Model\Timeofday $timeofday) 
{		
	$appiontment->addTimeofday($timeofday);		
	$this->objectManager->get('Schmidtch\\Survey\\Domain\\Repository\\AppiontmentRepository')->update($appiontment);
	$this->redirect('show','Survey',Null,array('survey'=>$survey));
}
```

## Antworten

### [#2](https://www.typo3.net/d/123222-mehrere-datensaetze-einer-foreach-schleife-speichern/2) · moep0r · 2016-10-06

```html
<h1>{headline}</h1>

<f:render partial="Error" arguments="{object:'timeofday'}" /> 
	
<h3>{survey.title}</h3>

<f:form action="{action}" object="{timeofday}" name="timeofday" arguments="{survey:survey, appiontment:appiontment}" additionalAttributes="{role:'form'}">
	
<f:for each="{survey.appiontments}" as="appiontments">
	<li class="list-group-item">
		<label>{appiontments.appiontmentdate}</label>
		<f:form.textfield property="timevalue" type="time" class="form-control" id="{appiontments}" />
	</li>
	
</f:for>

<f:form.submit value="{submitmessage}" class="btn btn-primary" />
	
</f:form>
```

```php
<?php

/**
	 * @param \Schmidtch\Survey\Domain\Model\Survey $survey
	 * @param \Schmidtch\Survey\Domain\Model\Appiontment $appiontment
	 * @param \Schmidtch\Survey\Domain\Model\Timeofday $timeofday
	 */
	public function addAction(
		\Schmidtch\Survey\Domain\Model\Survey $survey,
		\Schmidtch\Survey\Domain\Model\Appiontment $appiontment,
		\Schmidtch\Survey\Domain\Model\Timeofday $timeofday) 
	{		
		$appiontment->addTimeofday($timeofday);		
		$this->objectManager->get('Schmidtch\\Survey\\Domain\\Repository\\AppiontmentRepository')->update($appiontment);
		$this->redirect('show','Survey',Null,array('survey'=>$survey));
	}
```

### [#3](https://www.typo3.net/d/123222-mehrere-datensaetze-einer-foreach-schleife-speichern/3) · moep0r · 2016-10-12

Ich habe jetzt mal etwas rumprobiert.
Mein Template sieht jetzt so aus:

```php
<f:layout name="Default" />

<f:section name="content">

	<h1>Termin wählen</h1>

	<f:render partial="Error" arguments="{object:'timeofday'}" /> 
		
	<h3>{survey.title}</h3>

	<f:form action="add" object="{timeofday}" method="post" name="timeofday" arguments="{survey:survey, appiontment:appiontment}" additionalAttributes="{role:'form'}">
		
		<f:for each="{survey.appiontments}" as="appiontments" >
			<li class="list-group-item">
				<label>{appiontments.appiontmentdate}</label>
				<f:form.textfield type="time" class="form-control" name="timeofdays[timevalue1][]" value="00:00" />
				<f:form.textfield type="time" class="form-control" name="timeofdays[timevalue2][]" value="00:00" />
				<f:form.textfield type="time" class="form-control" name="timeofdays[timevalue3][]" value="00:00" />
				<f:form.hidden class="form-control" name="timeofdays[appiontmentID][]" value="{appiontments.uid}" />
			</li>
			
		</f:for>

		<f:form.submit value="Umfrage erstellen" class="btn btn-primary" />
		
	</f:form>

</f:section>
```

Jetzt habe ich das Problem, dass ich einfach die Werte nicht zum Controller bekomme

```php
/**
	 * @param \Schmidtch\Survey\Domain\Model\Survey $survey
	 * @param \Schmidtch\Survey\Domain\Model\Appiontment $appiontment
	 * @param \Schmidtch\Survey\Domain\Model\TimeOfDay $timeofday
	 */
	public function addAction(
		\Schmidtch\Survey\Domain\Model\Survey $survey)
	{	

		$wertArray = $_POST["tx_survey_surveylisting"];
		$timeofdays = $wertArray["timeofdays"];

		
		foreach ($timeofdays["appiontmentId"] as $key => $appiontment) {			
			
			$this->timeofdayRepository->add(new \Schmidtch\Survey\Domain\Model\TimeOfDay($appiontment,$timeofdays['timevalue1'][$key]));		
			
			
		}
		$persistenceManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager');
		$persistenceManager->persistAll();
		
		$this->redirect('show', 'Survey', NULL, array('survey' => $survey));
	}
```

Hab jetzt schon einiges rumprobiert, aber das Array ist immer leer.

```php
Wenn ich im Template den name so setzte 
name="tx_survey_surveylisting[timeofdays][timevalue1][]"
bekomme ich im Quelltext dann 
name="tx_survey_surveylisting[tx_survey_surveylisting][timeofdays][timevalue1][]"
darum hab ich ihn auf 
name="timeofdays[timevalue1][]"
```

Hat vielleicht irgendjemand eine Idee? :)

### [#4](https://www.typo3.net/d/123222-mehrere-datensaetze-einer-foreach-schleife-speichern/4) · froemken · 2016-10-25

Dafür, dass Du neu bei TYPO3 bist, hast Du Dir mit dieser Aufgabe direkt die Königsdisziplin rausgesucht. Selbst ich brauche dafür solche ein Formular ein paar Stunden. Ich kann Dir sagen, dass es geht, aber es kann an allen Ecken und Kanten knallen.

Vielleicht hilft Dir schon dieser Ansatz hier:

<https://github.com/jweiland-net/events2/blob/master/Resources/Private/Partials/Event/FormFields.html#L228>

Schau auch mal hier rein:
<http://www.typo3lexikon.de/typo3-tutorials/core/systemextensions/extbase/property-mapper.html>

Bitte installier Dir xdebug für PHP, damit Du weißt, was, wann, wo passiert. Bei meinem ersten Formular dieser Art habe ich 3 Tage gebraucht. Also nicht demotivieren lassen. Stück für Stück durchackern und viel dabei lernen ;-)

Stefan

### [#5](https://www.typo3.net/d/123222-mehrere-datensaetze-einer-foreach-schleife-speichern/5) · moep0r · 2016-10-26

Danke für deine Antwort :)
Das geschilderte Problem bin ich mit einem Click-Handler "umgangen", jedoch bin ich nicht umhin gekommen mehrere Objekte aus einem Formular zu speichern.
Ich habe sie jetzt "per Hand" in die Datenbank geschrieben. Auf jedenfall werde ich mich weiter in Typo3 einarbeiten.

Template:

```php
<f:if condition="{survey.appiontments}" >
	<f:form action="add" object="{poll}" name="poll" arguments="{survey:survey, timeofday:timeofday, subscriber:subscriber}">
		<f:form.hidden name="polls[subscriber][]" value="{subscriber.uid}" /> 
		<ul class="list-group">
			<f:for each="{survey.appiontments}" as="appiontment" iteration="iterator" >
				<f:form.hidden name="polls[appiontment][]" value="{iterator.index}" /> 
				<li class="list-group-item">
					<table class="table table-bordered table-sm">									
						<thead>					
							<tr>
								<th rowspan="2"></th>														
								<th colspan="{appiontment.timeOfDay -> f:count()}"><f:format.date format="d.m.Y">{appiontment.appiontmentdate}</f:format.date></th>											
							</tr>
							<tr>						
								<f:for each="{appiontment.timeOfDay}" as="timeofday" >
									<th>{timeofday.timevalue} Uhr</th>
								</f:for>
							</tr>
						</thead>
			
						<tbody>		
							<tr>
								<th width="150px">{subscriber.nameSubscriber}</th>								
									<f:for each="{appiontment.timeOfDay}" as="timeofday" iteration="iteratorb" >
										<td>
											<f:form.checkbox name="polls[poll][{iterator.index}][{iteratorb.index}]" class="checkbox" checked="true" value="1" />
											<f:form.hidden name="polls[timeofday][{iterator.index}][{iteratorb.index}]" value="{timeofday.uid}" /> 
										</td>										
									</f:for>									
							</tr>
						</tbody>						
					</table>
				</li>
			</f:for>
			<br />
			<f:form.submit value="Abschicken" class="btn btn-primary" />
		</ul>			
	</f:form>
</f:if>
```

Controller:

```php
public function addAction (
	\Schmidtch\Survey\Domain\Model\Survey $survey,
	\Schmidtch\Survey\Domain\Model\Subscriber $subscriber,
	\Schmidtch\Survey\Domain\Model\Poll $poll = NULL)
{
	$polls = $_POST['tx_survey_surveylisting']['polls'];
	$pollArray = $polls['poll'];
	$timeofdayArray = $polls['timeofday'];
	$uid = $polls['subscriber'][0];
	$appiontmentArray = $polls['appiontment'];
	
	$resultPoll = $this->pollRepository->addPoll($uid,$pollArray,$timeofdayArray,$appiontmentArray);
	$resultTimeofday = $this->timeofdayRepository->updatePoll($appiontmentArray,$timeofdayArray);		
	$countSubscriber = $this->pollRepository->countSubscriber($uid);
	$resultSubscriber = $this->subscriberRepository->updatePoll($uid,$countSubscriber); 
	$this->redirect('showAfter', 'Subscriber', NULL, array('subscriber' => $subscriber,'survey' => $survey));		
}
```

Repository:

```php
public function countSubscriber($uid)
{		
	$sql = "SELECT * FROM tx_survey_domain_model_poll WHERE subscriber='".$uid."'";
	$result = $GLOBALS['TYPO3_DB']->sql_query($sql);
	$num_rows = $result->num_rows;
	return $num_rows;
}

public function addPoll($uid,$pollArray,$timeofdayArray,$appiontmentArray)
{		
	for($a=0;$a<sizeof($appiontmentArray);$a++) {
	
		for($b=0;$b<sizeof($timeofdayArray[$a]);$b++) {

			if($pollArray[$a][$b] != NULL) {
				$sql = "INSERT INTO tx_survey_domain_model_poll (pid, subscriber,timeofday,poll,tstamp,crdate)
					VALUES (70,'".$uid."','".$timeofdayArray[$a][$b]."','".$pollArray[$a][$b]."',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);";
				$resultInsert = $GLOBALS['TYPO3_DB']->sql_query($sql);
	
			}
			else {
				$sql = "INSERT INTO tx_survey_domain_model_poll (pid, subscriber,timeofday,poll,tstamp,crdate)
					VALUES (70,'".$uid."','".$timeofdayArray[$a][$b]."',0,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);";
				$resultInsert = $GLOBALS['TYPO3_DB']->sql_query($sql);
			}
		}
	}		
	return $resultInsert;    
}
```

```php
public function updatePoll($appiontmentArray,$timeofdayArray)
{
	for($a=0;$a<sizeof($appiontmentArray);$a++) {
	
		for($b=0;$b<sizeof($timeofdayArray[$a]);$b++) {

			$sql = "SELECT * FROM tx_survey_domain_model_poll WHERE timeofday='".$timeofdayArray[$a][$b]."'";
			$result = $GLOBALS['TYPO3_DB']->sql_query($sql);
			$num_rows = $result->num_rows;				
			
			$sql = "UPDATE tx_survey_domain_model_timeofday
				SET timecheck='".$num_rows."'
				WHERE uid='".$timeofdayArray[$a][$b]."'";
			$resultInsert = $GLOBALS['TYPO3_DB']->sql_query($sql);				
		}
	}		
	return $resultInsert;
}

public function countTimeofday($timeofday)
{
	$query = $this->createQuery();
	$sql = "SELECT * FROM tx_survey_domain_model_poll WHERE timeofday='".$timeofday."'";
	$query->statement( $sql );
	$result = $query->count();
	return $result;
}
```

```php
public function updatePoll($uid,$countSubscriber)
{		
	$sql = "UPDATE tx_survey_domain_model_subscriber
		SET subcheck='".$countSubscriber."'
		WHERE uid='".$uid."'";
	$resultInsert = $GLOBALS['TYPO3_DB']->sql_query($sql);
			
	return $resultInsert;    
}
```

---

Quelle: „mehrere Datensätze einer foreach-Schleife speichern“, typo3.net, https://www.typo3.net/d/123222-mehrere-datensaetze-einer-foreach-schleife-speichern
Beiträge von Mitgliedern des Forums. Bitte mit Link auf die Diskussion zitieren.
