Created
July 28, 2012 16:25
-
-
Save gzankevich/3193910 to your computer and use it in GitHub Desktop.
Issue #4124
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* @ORM\Entity | |
* @ORM\Table(name="folder") | |
*/ | |
class Folder | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(type="string", length=100) | |
*/ | |
protected $name; | |
/** | |
* @ORM\OneToMany(targetEntity="File", mappedBy="folder", cascade={"persist", "delete"}) | |
*/ | |
protected $files; | |
public function __construct() | |
{ | |
$this->files = new \Doctrine\Common\Collections\ArrayCollection(); | |
} | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set name | |
* | |
* @param string $name | |
*/ | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
/** | |
* Get name | |
* | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* Add files | |
* | |
* @param Acme\DemoBundle\Entity\File $files | |
*/ | |
public function addFile(\Acme\DemoBundle\Entity\File $files) | |
{ | |
$this->files[] = $files; | |
} | |
/** | |
* Get files | |
* | |
* @return Doctrine\Common\Collections\Collection | |
*/ | |
public function getFiles() | |
{ | |
return $this->files; | |
} | |
public function setFiles($files) | |
{ | |
$this->files = $files; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* @ORM\Entity | |
* @ORM\Table(name="file") | |
*/ | |
class File | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(type="string", length=100) | |
*/ | |
protected $name; | |
/** | |
* @ORM\ManyToOne(targetEntity="Folder") | |
* @ORM\JoinColumn(name="folder_id", referencedColumnName="id") | |
*/ | |
protected $folder; | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set name | |
* | |
* @param string $name | |
*/ | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
/** | |
* Get name | |
* | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* Set folder | |
* | |
* @param Acme\DemoBundle\Entity\Folder $folder | |
*/ | |
public function setFolder(\Acme\DemoBundle\Entity\Folder $folder) | |
{ | |
$this->folder = $folder; | |
} | |
/** | |
* Get folder | |
* | |
* @return Acme\DemoBundle\Entity\Folder | |
*/ | |
public function getFolder() | |
{ | |
return $this->folder; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilder; | |
class FileType extends AbstractType | |
{ | |
public function buildForm(FormBuilder $builder, array $options) | |
{ | |
$builder->add('name'); | |
} | |
public function getName() | |
{ | |
return 'folder'; | |
} | |
public function getDefaultOptions(array $options) | |
{ | |
return array( | |
'data_class' => 'Acme\DemoBundle\Entity\File', | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilder; | |
class FolderType extends AbstractType | |
{ | |
public function buildForm(FormBuilder $builder, array $options) | |
{ | |
$builder->add('name'); | |
$builder->add('files', 'collection', array( | |
'type' => new FileType(), | |
'allow_add' => true, | |
'allow_delete' => true, | |
'by_reference' => false, | |
)); | |
} | |
public function getName() | |
{ | |
return 'folder'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Tests\Controller; | |
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
class TestControllerTest extends WebTestCase | |
{ | |
public function testIndex() | |
{ | |
$client = static::createClient(); | |
$crawler = $client->request('GET', '/test'); | |
$form = $crawler->selectButton('Save')->form(); | |
$form['folder[name]'] = 'test'; | |
$form['folder[files][0][name]'] = 'test'; | |
// submit the form | |
$crawler = $client->submit($form); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="{{ asset('bundles/acmedemo/js/jquery-1.7.2.js') }}"></script> | |
<script type="text/javascript"> | |
$(function() { | |
$('#addFile').click(function() { | |
$('#folders').append($('#folder_files').attr('data-prototype').replace(/\$\$name\$\$/g, $('#folder_files').children().length)); | |
return false; | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<form method="post" action="{{ path('test') }}"> | |
<div id="folders"> | |
{{ form_row(form) }} | |
</div> | |
<input type="submit" value="Save" /> | |
</form> | |
<a href="#" id="addFile">Add File</a> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
phpunit -c app ] 6:13 PM | |
PHPUnit 3.6.11 by Sebastian Bergmann. | |
Configuration read from /home/george/development/symfonyTesting/app/phpunit.xml.dist | |
E | |
Time: 0 seconds, Memory: 17.25Mb | |
There was 1 error: | |
1) Acme\DemoBundle\Tests\Controller\DemoControllerTest::testIndex | |
InvalidArgumentException: The form field "folder[files][0][name]" does not exist | |
/home/george/development/symfonyTesting/vendor/symfony/src/Symfony/Component/DomCrawler/Form.php:364 | |
/home/george/development/symfonyTesting/src/Acme/DemoBundle/Tests/Controller/TestControllerTest.php:18 | |
FAILURES! | |
Tests: 1, Assertions: 0, Errors: 1. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Acme\DemoBundle\Form\FolderType; | |
use Acme\DemoBundle\Entity\Folder; | |
// these import the "@Route" and "@Template" annotations | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |
class TestController extends Controller | |
{ | |
/** | |
* @Route("/test", name="test") | |
* @Template() | |
*/ | |
public function indexAction() | |
{ | |
$folder = $this->getDoctrine()->getRepository('AcmeDemoBundle:Folder')->findOneById(1); | |
$folder = new Folder(); | |
$form = $this->get('form.factory')->create(new FolderType(), $folder); | |
$request = $this->get('request'); | |
if ('POST' == $request->getMethod()) { | |
$form->bindRequest($request); | |
if ($form->isValid()) { | |
foreach($folder->getFiles() as $f) { | |
$f->setFolder($folder); | |
} | |
$this->getDoctrine()->getEntityManager()->persist($folder); | |
$this->getDoctrine()->getEntityManager()->flush(); | |
echo 'SAVED'; | |
} | |
} | |
return array('form' => $form->createView()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment