Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- model FATHER
- ...
- /**
- * A Father
- *
- * @FLOW3\Entity
- */
- class Father {
- /**
- * The name
- * @var string
- */
- protected $name;
- /**
- * The sons
- * @var \Doctrine\Common\Collections\Collection<\Mario\Basic\Domain\Model\Son>
- * @ORM\OneToMany(mappedBy="father",cascade={"persist"})
- */
- protected $sons;
- [getters and setters]
- /**
- * Adds a son to this father
- *
- * @param \Mario\Basic\Domain\Model\Son $son
- * @return void
- */
- public function addSon(\Mario\Basic\Domain\Model\Son $son) {
- $son->setFather($this);
- $this->sons->add($son);
- }
- model SON
- ...
- /**
- * A Son
- *
- * @FLOW3\Entity
- */
- class Son {
- /**
- * The father
- * @var \Mario\Basic\Domain\Model\Father
- * @ORM\ManyToOne(inversedBy="sons")
- */
- protected $father;
- /**
- * The name
- * @var string
- */
- protected $name;
- [getters and setters]
- ****** template and layout for FATHER *********
- default.html
- <!DOCTYPE html>
- <html>
- <head>
- <script language="javascript">
- lastField = 0;
- function addInput2() {
- if (lastField <= 9) {
- var node = document.getElementById('sonInput'+lastField);
- var newNodeLI = document.createElement('li');
- newNodeLI.innerHTML = 'add a new son: ';
- var newNode = document.createElement('input');
- lastField += 1;
- newNode.setAttribute("id", "sonInput"+lastField);
- newNode.setAttribute("type", "text");
- newNode.setAttribute("value", "name of the new son "+lastField);
- newNode.setAttribute("name", "newFather[0][sons]["+lastField+"][name]");
- newNodeLI.appendChild(newNode);
- node.parentNode.insertBefore(newNodeLI, node.nextSibling);
- } else {
- var node = document.getElementById('sonInput'+lastField);
- var newNodeLI = document.createElement('li');
- newNodeLI.innerHTML = 'Only 10 input fields allowed.';
- node.parentNode.insertBefore(newNodeLI, node.nextSibling);
- }
- } </script>
- <meta charset="utf-8">
- <title><f:render section="Title" /></title>
- <f:base />
- </head>
- <body>
- <f:flashMessages class="flashmessages" />
- <h1><f:render section="Title" /></h1>
- <f:render section="Content" />
- </body>
- </html>
- New.html (input fields for a father and as many sons as a client wishes. See JS above)
- <f:layout name="Default" />
- <f:section name="Title">New father</f:section>
- <f:section name="Content">
- <p>Just fill out the following form to create a new father:</p>
- <f:form action="create" name="newFather">
- <label for="name">Name</label>
- <f:form.textfield property="0.name" />
- <ul><li>
- <label for="nameson">Name of the son</label>
- <f:form.textfield property="0.sons.0.name" id="sonInput0" />
- <input type="button" onclick="addInput2()" name="add" value="Add input field" />
- </li>
- </f:form>
- </f:section>
- Echo.html
- .... snip ....
- Edit.html
- <f:layout name="Default" />
- <f:section name="Title">Edit father "{father.name}"</f:section>
- <f:section name="Content">
- <f:for each="{objArr}" as="son"> {son.flow3_persistence_identifier} {son.name} {son.father.name} <br> </f:for>
- <hr>
- <f:form action="update" object="{father}" name="father" >
- <ol><li>
- <label for="name">Name</label>
- <f:form.textfield property="name" id="name" value="{father.name}"/>
- </li>
- <f:if condition="{father.sons}">
- <f:then>
- <f:for each="{objArr}" as="son" iteration="i"> {son.flow3_persistence_identifier}
- <li><label for="nameson">Son: {son.name} </label>
- <f:form.textfield property="sons.{i.index}.__identity" value="{son.flow3_persistence_identifier}" type="hidden"/>
- <f:form.textfield property="sons.{i.index}.name" value="{son.name}" /></li>
- </f:for></f:then>
- <f:else><td><p>{father.name} is without sons.</p></td></f:else>
- </f:if></li>
- <li><f:form.submit value="Update"/></li>
- </ol></f:form></f:section>
- fatherController Echo, New, Edit, Create, Update actions
- class FatherController extends ActionController {
- /**
- * @FLOW3\Inject
- * @var \Mario\Basic\Domain\Repository\FatherRepository
- */
- protected $fatherRepository;
- /**
- * @FLOW3\Inject
- * @var \Mario\Basic\Domain\Repository\SonRepository
- */
- protected $sonRepository;
- /**
- * @var \Mario\Basic\Domain\Model\Father
- */
- protected $father;
- [index, show]
- /**
- * Echo content of New form
- *
- * @param array<\Mario\Basic\Domain\Model\Father> $newFather
- * @return void
- */
- public function echoAction(array $newFather) {
- $this->view->assign('newFather', $newFather);
- }
- /**
- * Shows a form for creating a new father object
- * @param array<\Test\Basic\Domain\Model\Father> $newFather
- *
- * @return void
- */
- public function newAction() {
- }
- /**
- * Adds the given new array of father objects to the father repository
- *
- * @param array<\Mario\Basic\Domain\Model\Father> $newFather An array of new fathers to add
- * @return void
- */
- public function createAction(array $newFather) {
- $propertyMapper = new \TYPO3\FLOW3\Property\PropertyMapper();
- foreach($newFather as $fatherArr){
- $fthArray = array();
- $sonArray = array();
- foreach($fatherArr as $k => $v){
- if(!($k == 'sons')){$fthArray[$k]=$v;
- }elseif(($k == 'sons')){$sonArray['sons']=$v;}}
- $fatherObj = $propertyMapper->convert($fthArray, '\Mario\Basic\Domain\Model\Father');
- $sons = new \Doctrine\Common\Collections\ArrayCollection();
- $fatherObj->setSons($sons);
- $this->fatherRepository->add($fatherObj);
- foreach($sonArray as $v){
- foreach($v as $sonMain){
- $sonObj = $propertyMapper->convert($sonMain,'\Mario\Basic\Domain\Model\Son');
- $sonObj->setFather($fatherObj);
- $this->sonRepository->add($sonObj);
- $fatherObj->addSon($sonObj);
- $this->fatherRepository->update($fatherObj);
- }}
- }
- $this->addFlashMessage('Created a new father and his sons');
- $this->redirect('index');
- }
- /**
- * Shows a form for editing an existing father object
- *
- * @param \Mario\Basic\Domain\Model\Father $father The father to edit
- * @return void
- */
- public function editAction(\Mario\Basic\Domain\Model\Father $father) {
- $this->view->assign('father', $father);
- $objArr=array();
- foreach($father->getSons() as $son){
- $son->flow3_persistence_identifier=$this->persistenceManager->getIdentifierByObject($son);
- $objArr[]=$son;}
- $this->view->assign('objArr',$objArr);
- }
- /**
- * Updates the given father object
- *
- * @param \Mario\Basic\Domain\Model\Father $father The father to update
- * @return void
- */
- public function updateAction(Father $father) {
- $this->fatherRepository->update($father);
- foreach($father->getSons() as $son){
- $this->sonRepository->update($son);
- }
- $this->addFlashMessage('Updated the father.');
- $this->redirect('index');
- }
Advertisement
Add Comment
Please, Sign In to add comment