Created
January 30, 2013 20:06
-
-
Save biozshock/4676380 to your computer and use it in GitHub Desktop.
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 and use | |
class DefaultController extends Controller | |
{ | |
public function saveAction($listId, $id) | |
{ | |
$list = $this->getDataManager()->getRepository('BiozshockListsBundle:MyList')->find($listId); | |
//FIXME: replace with filter. | |
// workaround because $this->getDataManager()->getRepository('BiozshockListsBundle:MyList') | |
// ->findOneBy(array('id' => $listId, 'item.id' => $id)) not working.. :( | |
$item = null; | |
/** @var Item $oneItem */ | |
foreach ($list->getItems() as $oneItem) { | |
if ($oneItem->getId() == $id) { | |
$item = $oneItem; | |
break; | |
} | |
} | |
if (null === $item) { | |
$item = new Item(); | |
} | |
$new = null === $item->getId() ? true : $list->getItems()->indexOf($item); | |
// standard symfony form | |
$form = $this->createForm(new ListType(), $item); | |
$form->bind($this->getRequest()); | |
if ($form->isValid()) { | |
$storage = $this->getDataManager(); | |
if ($new === true) { | |
$list->addItem($item); | |
} else { | |
// try 1 not working | |
$list[$new] = $item; | |
// try 2 not working either | |
$list->getItems()->remove($new); | |
$list->addItem($item); | |
} | |
$storage->persist($item); | |
$storage->persist($list); | |
$storage->flush(); | |
// redirect | |
} | |
// if errors... | |
} | |
} |
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 Biozshock\ListsBundle\Document; | |
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB, | |
Doctrine\Common\Collections\Collection, | |
Doctrine\Common\Collections\ArrayCollection | |
; | |
/** | |
* Item entity | |
* | |
* @MongoDB\EmbeddedDocument | |
* | |
*/ | |
class Item | |
{ | |
/** | |
* @MongoDB\Id | |
*/ | |
protected $id; | |
/** | |
* @var string | |
* @MongoDB\Field(type="string") | |
*/ | |
protected $name; | |
/** | |
* @var string | |
* @MongoDB\Field(type="string") | |
*/ | |
protected $comment; | |
// getters and setters generated by symfony console. | |
} |
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 Biozshock\ListsBundle\Document; | |
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB, | |
Doctrine\Common\Collections\Collection, | |
Doctrine\Common\Collections\ArrayCollection | |
; | |
/** | |
* Items entity | |
* | |
* @MongoDB\Document(repositoryClass="Biozshock\ListsBundle\Repository\ListRepository") | |
* | |
*/ | |
class MyList | |
{ | |
/** | |
* @MongoDB\Id | |
*/ | |
protected $id; | |
/** | |
* @var \Doctrine\Common\Collections\Collection | |
* @MongoDB\EmbedMany(targetDocument="Biozshock\ListsBundle\Document\Item") | |
*/ | |
protected $items; | |
public function __construct() | |
{ | |
$this->items = new \Doctrine\Common\Collections\ArrayCollection(); | |
} | |
/** | |
* Add item | |
* | |
* @param \Biozshock\ListsBundle\Document\Item $item | |
*/ | |
public function addItems(\Biozshock\ListsBundle\Document\Item $item) | |
{ | |
$this->items[] = $item; | |
} | |
/** | |
* Get items | |
* | |
* @return \Doctrine\Common\Collections\Collection $items | |
*/ | |
public function getItems() | |
{ | |
return $this->items; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment