Symfony 2 collection with image - editing - forms

I have little problem with my collection form type.
I have relation 1 to many ( product and productImages )
I think that the problem is in my productImages Entity ( if it is possible to solve )
My productImages:
class produktImage
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #Assert\File(maxSize="6000000")
*/
public $file;
/**
* #ORM\ManyToOne(targetEntity="product", inversedBy="tags")
*/
protected $product;
and the most importatnt method:
public function upload()
{
$this->name = trim(date("dmyGis").rand(1,99999999).'_'.$this->file->getClientOriginalName());
$this->file->move($this->getUploadRootDir(), $this->name);
$move = new resizeImg(1280, 3000, $this->name, $this->getUploadRootDir(), null);
$move->setThumbial(550, 2000, $this->getUploadRootDir().'../small/', null);
$move->setThumbial(200, 2000, $this->getUploadRootDir().'../thumb/', null);
$this->file = null;
}
Form productImage has file field only.
My product entity ( the most importatnt methods )
public function addPhotosum($photos)
{
$photos->setProdukt($this);
$photos->upload($this);
$this->photos->add($photos);
return $this;
}
public function removePhotosum($photos)
{
$this->photos->removeElement($photos);
$photos->removeImg();
}
Ok, but where is my problem.
When I try to add or remove file everything is ok.
If I try to edit file nothing happen. Name and file don't change.
I think it is because Product don't see changes in name ( name is stored in db only ) but I don't know how can I tell him "change name and file when file is diffrent".
Can I use preUpdate or something else?
Someone had similar problem?

Just call the upload() method in your edit action of your controller ..
example :
public function editAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('YourBundle:YourEntity')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Your entity.');
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$entity->upload()
$entity->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('your_destination_route', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
);
}
I hope it will work for you ..

Related

symfony3 image is not uploaded

In AppBundle\Etity\Image I have:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="images")
* #ORM\HasLifecycleCallbacks
*/
class Image
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
* #Assert\NotBlank
*/
private $name;
/**
* #ORM\Column(type="string", length=255, nullable=true)
* #Assert\NotBlank
*/
private $path;
/**
* #Assert\Image(maxSize="10M", mimeTypes="image/jpeg", minWidth = 600, minHeight = 400)
* #Assert\NotBlank
*/
private $file;
/**
* #ORM\Column(type="string", length=255)
* #Assert\NotBlank
*/
private $alt;
private $temp;
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #return mixed
*/
public function getName()
{
return $this->name;
}
/**
* #param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* #return mixed
*/
public function getPath()
{
return $this->path;
}
/**
* #param mixed $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* #return mixed
*/
public function getAlt()
{
return $this->alt;
}
/**
* #param mixed $alt
*/
public function setAlt($alt)
{
$this->alt = $alt;
}
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path;
}
public function getUploadRootDir()
{
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
public function getUploadDir()
{
return 'images/full';
}
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->path)) {
// store the old name to delete after the update
$this->temp = $this->path;
$this->path = null;
} else {
$this->path = 'initial';
}
}
/**
* #return mixed
*/
public function getFile()
{
return $this->file;
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename.'.'.$this->getFile()->guessExtension();
}
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->getFile()->move($this->getUploadRootDir(), $this->path);
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir().'/'.$this->temp);
// clear the temp image path
$this->temp = null;
}
$this->file = null;
}
/**
* #ORM\PostRemove()
*/
public function removeUpload()
{
$file = $this->getAbsolutePath();
if ($file) {
unlink($file);
}
}
}
Which is used in AppBundle\Entity\Post.php like this:
/**
* #ORM\ManyToOne(targetEntity="Image", cascade="all")
* #ORM\JoinColumn(name="image_id", referencedColumnName="id")
*/
private $teaserImage;
In AppBundle\Form\Type\PostType.php I have this:
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use AppBundle\Entity\Post;
/**
* Defines the form used to create and manipulate blog posts.
*/
class PostType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', null, array('label' => 'Title'))
->add('summary', null, array('label' => 'Summary'))
->add('teaserImage', 'AppBundle\Form\Type\ImageType', array('label' => 'Image'))
->add('content', null, array(
'attr' => array('rows' => 20),
'label' => 'Content',
))
;
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Post',
));
}
}
For some reason the images are not uploaded to the specified directory (or anywhere else) and I am not sure what I did wrong. I would be grateful for any insights.
Thank you.
The issue is related to
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
In this case I changed the previous line to:
return __DIR__ . '/../../../web/' . $this->getUploadDir();
That is because my entity is located in src/AppBundle/Entity and to go to root directory it needs to hop 3 directories back.
Also it is a bad idea to hard-code paths in entities. I modified my example accordingly.

FOSUser + FOSRest - Update User

I'm using Fosuser and Fosrest for a webservice.
I created GetUser to get List user and the registration to register an user and it work perfectly !
But, now I try to make the update user but i got a problem !
I copy the function from Fosuser controller
In My controller :
public function updatePostAction(Request $request,$id)
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** #var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** #var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isValid()) {
/** #var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
return $this->render('FOSUserBundle:Profile:edit.html.twig', array(
'form' => $form->createView()
));
}
it doesn't work ! anyone can help me ?
When i try to post :
{
"fos_user_profile_form": {
"username":"jacky"
}
}
it returns : https://gist.github.com/gaticho/34ae4fff5492fdecd48f
The database doesn't change :-(
My User entity :
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
private $dateCreated;
/**
* #var DateTime
*
* #ORM\Column(name="date_modified", type="datetime", nullable=true)
*/
private $dateModified;
/**
* #var DateTime
*
* #ORM\Column(name="date_deleted", type="datetime", nullable=true)
*/
private $dateDeleted;
public function __construct()
{
parent::__construct();
}
}`
Thanks !

The class 'Symfony\\Component\\HttpFoundation\\File\\UploadedFile' was not found in the chain configured namespaces during Rest API Unit Test

I'm writing a REST API client and I am trying to unit test the user creation process which let a user to upload an image.
I am using Symfony 2.7, Doctrine Extension Bundle (Uploadable extension) and FOS Rest Bundle
The unit tests are working well excepted when I try to upload a file, it triggers me the following error when I error_log the 500 HTTP Reponse :
The class 'Symfony\\Component\\HttpFoundation\\File\\UploadedFile' was not found in the chain configured namespaces
Please find the relevant code :
UsersControllerTest.php
<?php
namespace Acme\Bundle\UserBundle\Tests\Controller;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class UsersControllerTest extends WebTestCase
{
public function testAvatar(){
$client = static::createClient();
$shortImage = tempnam(sys_get_temp_dir(), 'upl');
imagepng(imagecreatetruecolor(10, 10), $shortImage);
$file = new UploadedFile(
$shortImage,
basename($shortImage),
MimeTypeGuesser::getInstance()->guess($shortImage),
filesize($shortImage)
);
$crawler = $client->request(
"POST",
"/api/users",
array(
"user_registration" => array(
"firstName" => "test",
"lastName" => "test"
),
),
array(
'user_registration'=>array('avatar'=>$file)
),
array(
'Content-Type' => 'multipart/formdata'
)
);
error_log($client->getResponse()); //Here I see the Uploadable class namespace error
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
UserRegistrationType.php
<?php
namespace Acme\Bundle\UserBundle\Form\Type;
use Acme\Bundle\UserBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
class UserRegistrationType extends AbstractType{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\Bundle\UserBundle\Entity\User',
'cascade_validation' => true,
'csrf_protection' => false
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstName', 'text');
$builder->add('lastName', 'text');
$builder->add('avatar', 'file', array(
'required' => false
));
}
public function getParent()
{
return 'form';
}
public function getName()
{
return 'user_registration';
}
}
UsersController.php
<?php
namespace Acme\Bundle\UserBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\View;
use Acme\Bundle\UserBundle\Entity\User;
use Acme\Bundle\UserBundle\Entity\Avatar;
use Acme\Bundle\UserBundle\Form\Type\UserRegistrationType;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use JMS\SecurityExtraBundle\Annotation as JMSSecurity;
class UsersController extends FOSRestController
{
/**
* #View(serializerGroups={"Registration"})
*/
public function postUsersAction(Request $request){
$user = new User();
$form = $this->createForm(new UserRegistrationType(), $user);
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
if(null !== $user->getAvatar()){
$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
$uploadableManager->markEntityToUpload($user, $user->getAvatar());
}
$em->flush();
return $user;
}
else {
$validator = $this->get('validator');
$errors = $validator->validate($user, array('Default','Registration'));
$view = $this->view($errors, 400);
return $this->handleView($view);
}
}
}
Avatar.php
<?php
namespace Acme\Bundle\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Avatar
*
* #ORM\Table("avatar")
* #ORM\Entity
* #Gedmo\Uploadable(pathMethod="getPath", callback="postUploadAction", filenameGenerator="SHA1", allowOverwrite=true, appendNumber=true)
*/
class Avatar
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="path", type="string")
* #Gedmo\UploadableFilePath
*/
private $path;
/**
* #ORM\Column(name="name", type="string")
* #Gedmo\UploadableFileName
*/
private $name;
/**
* #ORM\Column(name="mime_type", type="string")
* #Gedmo\UploadableFileMimeType
*/
private $mimeType;
/**
* #ORM\Column(name="size", type="decimal")
* #Gedmo\UploadableFileSize
*/
private $size;
public function postUploadAction(array $info)
{
// Do some stuff with the file..
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function getPath(){
return __DIR__.'../../web/avatars/';
}
/**
* Set path
*
* #param string $path
* #return Image
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Set mimeType
*
* #param string $mimeType
* #return Image
*/
public function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
return $this;
}
/**
* Get mimeType
*
* #return string
*/
public function getMimeType()
{
return $this->mimeType;
}
/**
* Set size
*
* #param string $size
* #return Image
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
/**
* Get size
*
* #return string
*/
public function getSize()
{
return $this->size;
}
}
User.php
class User{
...
/**
* Avatar
*
* #ORM\OneToOne(targetEntity="Acme\Bundle\UserBundle\Entity\Avatar", cascade={"all"})
* #ORM\JoinColumn(nullable=true)
* #Assert\Valid
*/
private $avatar;
public function getAvatar(){
return $this->avatar;
}
public function setAvatar($avatar = null){
$this->avatar = $avatar;
return $this;
}
}
Error stack (JSON)
For full stack please see here : http://pastebin.com/sgPE4Uh1
{
"error":{
"code":500,
"message":"Internal Server Error",
"exception":[
{
"message":"The class 'Symfony\\Component\\HttpFoundation\\File\\UploadedFile' was not found in the chain configured namespaces Acme\\Bundle\\UserBundle\\Entity",
"class":"Doctrine\\Common\\Persistence\\Mapping\\MappingException",
"trace":[
{
"namespace":"",
"short_class":"",
"class":"",
"type":"",
"function":"",
"file":"D:\\xampp\\htdocs\\RestApi\\vendor\\doctrine\\common\\lib\\Doctrine\\Common\\Persistence\\Mapping\\MappingException.php",
"line":37,
"args":[
]
}]
}
}
}
I really don't know the origin of the error, On my config.yml the doctrine mapping is set to auto
Thank you in advance for any help
EDIT 1
I have completely refactored the code with this scenario :
The upload request is now independant, it has it own isolated request with a Content-Type set to multipart/form-data
This time it would not have any problem, but..the same error is still here :
{
code: 500
message: "The class 'Symfony\Component\HttpFoundation\File\UploadedFile' was not found in the chain configured namespaces Sowq\Bundle\UserBundle\Entity"
}
I am wondering if that's not a bug or a compatibility issue between Symfony 2.7 and the doctrine Uploadable extension, any idea?
I think the problem is that you or (uploadableManager extension - I don't know this) try set an object file (of type "UploadedFile Class")
$Avatar is a file object if you want save to DataBase, you need a datatype blob.
Usually, before save the file on File System and then save $Avatar on DB, where $Avatar is just file path or file name o link to File System resource
see https://github.com/Atlantic18/DoctrineExtensions/issues/1353

Symfony2 - Entities not linking via ManyToMany

I have 2 entities, Reply and Post.
These are linked as ManyToMany with Post being the owner.
I have created a form for Reply to add new replies to the post, but the for some reason the replies are not showing up in the for loop in Twig.
In the database the new replies are listed and saved yet it's not displaying?
I've setup fixtures for linking replies to posts and it displays just fine in the for loop, just not for the new replies created in the form?
What am I missing here?
ReplyForm
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('author')
->add('body')
->add('post', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\Reply'
));
}
public function getName()
{
return 'acme_demobundle_reply';
}
Twig
{% for reply in post.replies %}
<hr>
<p><small>Reply from <em>{{ reply.author.name }}</em> on {{ reply.createdAt|date }}</small></p>
<p>{{ reply.body }}</p>
{% endfor %}
Controller
public function createReplyAction(Request $request, $slug)
{
$post = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post')
->findOneBy(array(
'slug' => $slug
));
if (null == $post) {
throw $this->createNotFoundException('Post was not found');
}
$reply = new Reply();
$reply->addPost($post);
$form = $this->createForm(new ReplyType(), $reply);
$form->handleRequest($request);
if ($form->isValid()) {
$this->getDoctrine()->getManager()->persist($reply);
$this->getDoctrine()->getManager()->flush();
return $this->redirect($this->generateUrl('acme_core_post_show', array(
'slug' => $slug
)));
}
return array(
'post' => $post,
'form' => $form->createView()
);
}
Reply entity
/**
* #ORM\ManyToMany(targetEntity="Post", mappedBy="replies")
*/
protected $post;
/**
* Constructor
*/
public function __construct()
{
$this->post = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add post
*
* #param \Acme\DemoBundle\Entity\Post $post
* #return Reply
*/
public function addPost(\Acme\DemoBundle\Entity\Post $post)
{
$this->post[] = $post;
return $this;
}
/**
* Remove post
*
* #param \Acme\DemoBundle\Entity\Post $post
*/
public function removePost(\Acme\DemoBundle\Entity\Post $post)
{
$this->post->removeElement($post);
}
/**
* Get post
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPost()
{
return $this->post;
}
Post entity
/**
* #return Array Collection
*
* #ORM\ManyToMany(targetEntity="Reply", inversedBy="post")
* #JoinTable(name="posts_replies",
* joinColumns={#JoinColumn(name="post_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="reply_id", referencedColumnName="id")}
* )
*/
protected $replies;
/**
* Constructor
*/
public function __construct()
{
$this->replies = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add replies
*
* #param \Acme\DemoBundle\Entity\Reply $replies
* #return Post
*/
public function addReply(\Acme\DemoBundle\Entity\Reply $replies)
{
$replies->addPost($this);
$this->replies[] = $replies;
return $this;
}
/**
* Remove replies
*
* #param \Acme\DemoBundle\Entity\Reply $replies
*/
public function removeReply(\Acme\DemoBundle\Entity\Reply $replies)
{
$this->replies->removeElement($replies);
}
/**
* Get replies
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getReplies()
{
return $this->replies;
}
Remove the following:
$replies->addPost($this); from Post entity
and add in
$post->addReply($this); under addPost for Reply entity.
Shouldn't you construct your $replies property as an ArrayCollection in your Post Entity?
public function __construct()
{
$this->replies = new \Doctrine\Common\Collections\ArrayCollection();
}
the entity Post's annotation is wrong, try this one:
/**
* #ORM\ManyToMany(targetEntity="Reply", inversedBy="post")
* #ORM\JoinTable(name="posts_replies",
* joinColumns={#ORM\JoinColumn(name="post_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="reply_id", referencedColumnName="id")}
* )
*/
private $replies;

Doctrine ODM / MongoDB: How to query for references within embedded documents?

I'm new to Doctrine ODM and i'm totally stuck with a simple query :(
Let me start with the document structure:
Array
(
[_id] => 4ee1e4527f749c9411000012
[voteList] => Array
(
[_id] => 4ee1e4527f749c9411000013
[votes] => Array
(
... stripped ...
)
[latest] => Array
(
[_id] => 4ee1e4527f749c9411000014
[rating] => 1
[voter] => Array
(
[$ref] => Voter
[$id] => 4ee1e4527f749c941100000f
[$db] => x_test
)
)
)
... stripped ...
)
This document is called Voting.
My Question is, how to find Voting-documents by a particular voter (which is stored in voteList.latest.voter).
I tried it like this:
$builder
->field('voteList.latest.voter')->references($voter)
->getQuery()
->execute();
And this way also:
$result = $builder
->field('voteList.latest.voter.$id')->equals(new \MongoId($voter->getId()))
->getQuery()
->execute();
Both are leading to this exception:
Doctrine\ODM\MongoDB\MongoDBException: No mapping found for field 'voteList.latest.voter' in class 'App\BaseBundle\Document\Voting'.
Am i building the query incorrectly or might something be wrong with my document classes?
Thanks for reading, any advices appreciated.
EDIT: Documents attached
/**
* #ODM\Document(repositoryClass="App\BaseBundle\Document\VotingRepository")
*/
class Voting
{
/**
* #ODM\Id
* #var int
*/
protected $id;
/**
* #ODM\EmbedOne(targetDocument="App\BaseBundle\Document\VoteList")
* #var VoteList
*/
protected $voteList;
public function __construct()
{
if ($this->voteList === null) {
$this->voteList = new VoteList();
}
}
/**
* #return string
*/
public function getId()
{
return $this->id;
}
/**
* #return VoteList
*/
public function getVoteList()
{
return $this->voteList;
}
}
;
/**
* #ODM\EmbeddedDocument
*/
class VoteList implements \Countable, \ArrayAccess, \IteratorAggregate
{
/**
* #ODM\Id
*/
protected $id;
/**
* #ODM\EmbedMany(targetDocument="App\BaseBundle\Document\Vote")
* #var Vote[]
*/
protected $votes = array();
/**
* #ODM\EmbedOne(targetDocument="App\BaseBundle\Document\Vote")
* #var Vote
*/
protected $latest;
public function getId()
{
return $this->id;
}
/**
* #return Vote
*/
public function getLatest()
{
return $this->latest;
}
}
/**
* #ODM\EmbeddedDocument
*/
class Vote
{
/**
* #ODM\Id
*/
protected $id;
/**
* #ODM\ReferenceOne(targetDocument="App\BaseBundle\Document\Voter")
* #var Voter
*/
public $voter;
public function getId()
{
return $this->id;
}
public function getVoter()
{
return $this->voter;
}
public function setVoter(Voter $voter)
{
$this->voter = $voter;
}
}
Figured out it's not working due to a doctrine-odm bug.
https://github.com/doctrine/mongodb-odm/pull/207