upload not running with VichUploaderBundle - doctrine-orm

I would like to use a VichUploaderBundle for upload files in my symfony 3.2.3 project (PHP 5.6). I try lot of thing but nothing upload run. But the persistance layer run perfeclty with my database.
config.yml
knp_gaufrette:
stream_wrapper: ~
adapters:
fileupload_adapter:
local:
directory: %kernel.root_dir%/../web/
create: true
filesystems:
fileupload_fs:
adapter: fileupload_adapter
# VichUploaderBundle Configuration
vich_uploader:
db_driver: orm
twig: true
storage: gaufrette
mappings:
tgmedia_file:
uri_prefix: web
upload_destination: fileupload_fs
namer: vich_uploader.namer_origname
Entity
namespace MediaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use TweedeGolf\MediaBundle\Model\AbstractFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* #ORM\Entity
* #Vich\Uploadable
* #ORM\HasLifecycleCallbacks
* #ORM\Table
*/
class FileUpload
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* #Vich\UploadableField(mapping="tgmedia_file", fileNameProperty="imageName")
*
* #var File
*/
protected $imageFile;
/**
* #ORM\Column(type="string", length=255)
*
* #var string
*/
private $imageName;
/**
* #ORM\Column(type="datetime")
*
* #var \DateTime
*/
protected $updatedAt;
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #return File|null
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* #param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*
* #return Product
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
/**
* #param string $imageName
*
* #return FileUpload
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
return $this;
}
/**
* #return string|null
*/
public function getImageName()
{
return $this->imageName;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
*
* #return FileUpload
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
}
The formType
<?php
namespace MediaBundle\Form;
use MediaBundle\Entity\File;
use MediaBundle\Entity\FileUpload;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Vich\UploaderBundle\Form\Type\VichFileType;
use Vich\UploaderBundle\Form\Type\VichImageType;
class FileUploadType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('imageName')
->add('imageFile', VichImageType::class, [
'required' => false,
'allow_delete' => true,
'download_link' => true,
'mapped' => false,
'data_class' => null
])
->add('submit', SubmitType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => FileUpload::class,
'csrf_protection' => false,
));
}
}
Controller
<?php
namespace MediaBundle\Controller;
use MediaBundle\Entity\File;
use MediaBundle\Entity\FileUpload;
use MediaBundle\Entity\Product;
use MediaBundle\Form\FileType;
use MediaBundle\Form\FileUploadType;
use MediaBundle\Form\ProductType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class MainController extends Controller
{
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$fileUploaded = new FileUpload();
$form = $this->createForm(FileUploadType::class, $fileUploaded, array(
'action' => $this->generateUrl('media_main')
));
$form->handleRequest($request);
if ($form->isSubmitted())
{
$fileUploaded->setUpdatedAt(new \DateTime());
$fileUploaded->setImageFile($form->get('imageFile')->getData());
$em->persist($fileUploaded);
$em->flush();
}
return $this->render('MediaBundle:main:index.html.twig', array(
'form' => $form->createView()
));
}
}
Where is my error ? What wrong ?

uri_prefix: /project/web/fileupload_fs
upload_destination: '%kernel.root_dir%/../web/fileupload_fs'
uri_prefix begins from www folder.
and change controller like that
if ($form->isSubmitted() && $form->isValid())
{
$em->persist($fileUploaded);
$em->flush();
}

Related

[PHPUnit], [Symfony]: test that Entity was saved in DB

I have problem with my test. I learn how to write phpunit test and how i can mock object, services etc.. I have this method on my ProductService:
<?php
namespace App\Service;
use App\Entity\Product;
use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ProductService
{
/**
* #var ProductRepository
*/
private $productRepository;
/**
* #var EntityManager
*/
private $entityManager;
/**
* #var ValidatorInterface
*/
private $validator;
/**
* ProductService constructor.
* #param ProductRepository $productRepository
* #param EntityManagerInterface $entityManager
* #param ValidatorInterface $validator
*/
public function __construct(ProductRepository $productRepository, EntityManagerInterface $entityManager, ValidatorInterface $validator)
{
$this->productRepository = $productRepository;
$this->entityManager = $entityManager;
$this->validator = $validator;
}
/**
* #param $productData
* #return Product|string
*/
public function createProduct($productData)
{
$name = $productData['name'];
$quantity = $productData['quantity'];
$sku = $productData['sku'];
$product = new Product();
$product->setName($name);
$product->setQuantity($quantity);
$product->setProductSerial($sku);
$errors = $this->validator->validate($product);
if (count($errors) > 0) {
$errorsString = (string)$errors;
return $errorsString;
}
try {
$this->entityManager->persist($product);
$this->entityManager->flush();
return $product;
} catch (\Exception $ex) {
return $ex->getMessage();
}
}
}
and i write this test:
<?php
namespace App\Tests\Service;
use App\Entity\Product;
use App\Repository\ProductRepository;
use App\Service\ProductService;
use Doctrine\Common\Persistence\ObjectRepository;
use PHPUnit\Framework\TestCase;
class ProductServiceTest extends TestCase
{
/**
* Create product test
*/
public function testCreateProduct()
{
$product = new Product();
$product->setName('tester');
$product->setQuantity(2);
$product->setProductSerial('Examplecode');
$productService = $this->createMock(ProductService::class);
$productService->method('createProduct')->will($this->returnSelf());
$this->assertSame($productService, $productService->createProduct($product));
}
}
When i run phpunit test, then i always have success but my database is empty. How can I be sure that the test works correctly? What is worth fixing and what is not? I wanted to make the launch of tests result in, for example, adding records to the test database, but I have no idea how to do it and how to properly mock it. I using phpunit + Symfony 4.
I used to write tests, but those that asked the endpoint API, and here I want to test services and repositories without endpoints.
I would like to learn how to test and mock websites, repositories, various classes etc.
When i apply answer then i have:
PHPUnit 7.5.17 by Sebastian Bergmann and contributors.
Testing Project Test Suite
?[31;1mE?[0m 1 / 1 (100%)
Time: 542 ms, Memory: 10.00 MB
There was 1 error:
1) App\Tests\Service\ProductServiceTest::testCreateProduct
Doctrine\Common\Persistence\Mapping\MappingException: The class 'App\Repository\ProductRepository' was not found in the chain configured namespaces App\Entity, Gesdinet\JWTRefreshTokenBundle\Entity
D:\warehouse-management-api\vendor\doctrine\persistence\lib\Doctrine\Common\Persistence\Mapping\MappingException.php:22
D:\warehouse-management-api\vendor\doctrine\persistence\lib\Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain.php:87
D:\warehouse-management-api\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php:151
D:\warehouse-management-api\vendor\doctrine\persistence\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php:304
D:\warehouse-management-api\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php:78
D:\warehouse-management-api\vendor\doctrine\persistence\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php:183
D:\warehouse-management-api\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php:283
D:\warehouse-management-api\vendor\doctrine\doctrine-bundle\Repository\ContainerRepositoryFactory.php:44
D:\warehouse-management-api\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php:713
D:\warehouse-management-api\vendor\doctrine\persistence\lib\Doctrine\Common\Persistence\AbstractManagerRegistry.php:215
D:\warehouse-management-api\tests\Service\ProductServiceTest.php:28
?[37;41mERRORS!?[0m
?[37;41mTests: 1?[0m?[37;41m, Assertions: 0?[0m?[37;41m, Errors: 1?[0m?[37;41m.?[0m
My Product entity
<?php
namespace App\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="App\Repository\ProductRepository")
*/
class Product
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
* #Assert\NotBlank()
*/
private $name;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank()
*/
private $quantity;
/**
* #Gedmo\Mapping\Annotation\Timestampable(on="create")
* #ORM\Column(type="datetime")
*/
private $createdAt;
/**
* #Gedmo\Mapping\Annotation\Timestampable(on="update")
* #ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
*/
private $product_serial;
public function __construct() {
$this->setCreatedAt(new \DateTime());
$this->setUpdatedAt();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(int $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(): self
{
$this->updatedAt = new \DateTime();
return $this;
}
public function getProductSerial(): ?string
{
return $this->product_serial;
}
public function setProductSerial(string $product_serial): self
{
$this->product_serial = $product_serial;
return $this;
}
}
ProductRepository
<?php
namespace App\Repository;
use App\Entity\Product;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
class ProductRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Product::class);
}
}
doctrine.yaml
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
First of all, when you mock a method the original method doesn't exist any more, in this test. In your case you substitute ProductService::createProduct with something like this:
// This is your mock
class ProductService
{
// ...
public function createProduct($productData)
{
return $this;
}
}
Your test doesn't check anything.
If you want to test the real functionality then
namespace App\Tests\Service;
use App\Repository\ProductRepository;
use App\Service\ProductService;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ProductServiceTest extends KernelTestCase
{
/**
* Create product test
*/
public function testCreateProduct(): void
{
// We load the kernel here (and $container)
self::bootKernel();
$productData = [
'name' => 'foo',
'quantity' => 1,
'sku' => 'bar',
];
$productRepository = static::$container->get('doctrine')->getRepository(ProductRepository::class);
$entityManager = static::$container->get('doctrine')->getManager();
// Here we mock the validator.
$validator = $this->getMockBuilder(ValidatorInterface::class)
->disableOriginalConstructor()
->setMethods(['validate'])
->getMock();
$validator->expects($this->once())
->method('validate')
->willReturn(null);
$productService = new ProductService($productRepository, $entityManager, $validator);
$productFromMethod = $productService->createProduct($productData);
// Here is you assertions:
$this->assertSame($productData['name'], $productFromMethod->getName());
$this->assertSame($productData['quantity'], $productFromMethod->getQuantity());
$this->assertSame($productData['sku'], $productFromMethod->getSku());
$productFromDB = $productRepository->findOneBy(['name' => $productData['name']]);
// Here we test that product in DB and returned product are same
$this->assertSame($productFromDB, $productFromMethod);
}
}

Doctrine 2 ZF3 self referencing entity is not updating all fields of parent

I have a self referencing entity with parent and children. The strange thing is, when I add the form elements (DoctrineModule ObjectSelect) for parent and children to the form, some other fields of the parent entity doesn't update, when I persist the entity. The child entities are updating fine.
In the update query of the parent aren't these fields which I want to update. It's like doctrine doesn't recognize the changes for the parent (owning side) anymore. Before the persist I get the right entity with the actual changes from the form, but than doctrine doesn't update the changed fields in the query.
When I delete the form elements for parent and children in the form, everything works fine and the parent entity update/persist all fields.
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(type="text")
*/
private $teaser;
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(type="text")
*/
private $description;
/**
* One Category has Many Categories.
* #var Collection
* #ORM\OneToMany(targetEntity="Rental\Entity\Rental", mappedBy="parent")
*/
private $children;
/**
* Many Categories have One Category.
* #ORM\ManyToOne(targetEntity="Rental\Entity\Rental", inversedBy="children")
* #ORM\JoinColumn(name="parent_id", referencedColumnName="houses_id", nullable=true, onDelete="SET NULL")
*/
private $parent;
public function __construct()
{
$this->children = new ArrayCollection();
}
/**
* Get teaser
*
* #return string
*/
public function getTeaser()
{
return $this->teaser;
}
/**
* Set teaser
*
* #param string $teaser
*/
public function setTeaser($teaser)
{
$this->teaser = $teaser;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set description
*
* #param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Add $child
* #param Collection $children
*/
public function addChildren(Collection $children)
{
foreach ($children as $child) {
$this->addChild($child);
}
}
/**
* #param Rental $child
* #return void
*/
public function addChild(Rental $child)
{
if ($this->children->contains($child)) {
return;
}
$child->setParent($this);
$this->children[] = $child;
}
/**
* Remove children
* #param Rental $children
*/
public function removeChildren(Collection $children)
{
foreach ($children as $child) {
$this->removeChild($child);
}
}
/**
* Remove child.
*
* #param \Rental\Entity\Rental $child
*
* #return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeChild(Rental $child)
{
return $this->children->removeElement($child);
}
/**
* Get children.
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
/**
* Set parent.
*
* #param \Rental\Entity\Rental|null $parent
*/
public function setParent(Rental $parent = null)
{
$this->parent = $parent;
}
/**
* Get parent.
*
* #return \Rental\Entity\Rental|null
*/
public function getParent()
{
return $this->parent;
}
Answer based (and might still change) based on discussion in comments with OP under question.
Simple use case: self-referencing Doctrine Entity does not correctly update parent/child related Entity object properties on on-save action.
OP provided Entity.
I'm assuming you have a Form setup for this Entity. I'm also assuming it's correctly setup, as you did not provide it after being asked (because, rightly so, it would mean a lot of code). However, assuming stuff with code makes for a lot of mistakes, which is why I mention it here.
As such, I'm assuming the handling of your Form in the Controller might be faulty. To check, please use the following simplified addAction function and give it a shot (Factory code below).
/**
* #var RentalForm
*/
protected $form;
/**
* #var ObjectManager
*/
protected $objectManager;
public function __construct(ObjectManager $objectManager, RentalForm $form)
{
$this->form = $form;
$this->objectManager = $objectManager;
}
public function addAction()
{
/** #var RentalForm $form */
$form = $this->getForm();
/** #var Request $request */
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$entity = $form->getObject();
$this->getObjectManager()->persist($entity);
try {
$this->getObjectManager()->flush();
} catch (\Exception $e) {
$message = sprintf(
'Was unable to save the data. Saving threw an error. <br />Code: %s. <br />Message: %s',
$e->getCode(),
$e->getMessage()
);
$this->flashMessenger()->addErrorMessage($message);
return [
'form' => $form,
'validationMessages' => $form->getMessages() ?: '',
];
}
$this->flashMessenger()->addSuccessMessage(
$this->getTranslator()->translate('Successfully created object.')
);
// TODO replace vars with your own: return $this->redirect()->route($route, $routeParams);
}
$this->flashMessenger()->addWarningMessage(
'Your form contains errors. Please correct them and try again.'
);
}
return [
'form' => $form,
'validationMessages' => $form->getMessages() ?: '',
];
}
Factory for class with the above, modify as needed for you situation
class RentalControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/** #var ObjectManager $objectManager */
$objectManager = $container->get(EntityManager::class);
/** #var FormElementManagerV3Polyfill $formElementManager */
$formElementManager = $container->get('FormElementManager');
/** #var RentalForm $form */
$form = $formElementManager->get(RentalForm::class);
return new RentalController($objectManager, $form);
}
}

Doctrine2 ORM OneToMany or ManyToOne not working

I would like to have the relationship between 2 tables, with OneToMany and ManyToOne relationships.
Survey
id
title
Questions:
id
survey_id
So i am looking to list the surveys and their respective questions, so how can i achieve that.
Here is my code,
Survey Entity:
<?php
namespace Survey\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Library\Entity\BaseEntity;
/**
* Description of Survey
*
* #author Mubarak
*/
/**
* #ORM\Entity
* #ORM\Table(name="surveys")
*/
class Survey extends BaseEntity{
public function __construct() {
$this->questions = new ArrayCollection();
}
/**
* #ORM\Column(name="title", type="string")
* #var string
*/
protected $title;
/**
* #ORM\Column(name="description", type="string")
* #var string
*/
protected $description;
/**
* #ORM\OneToMany(targetEntity="Survey\Entity\Questions", mappedBy="surveys")
*/
private $questions;
public function getTitle() {
return $this->title;
}
public function setTitle($title) {
$this->title = $title;
}
public function getDescription() {
return $this->description;
}
public function setDescription($description) {
$this->description = $description;
return $this;
}
public function getQuestions() {
return $this->questions;
}
public function setQuestions(ArrayCollection $questions) {
$this->questions = $questions;
}
public function __toString() {
return __CLASS__ . ": [id: {$this->id}, name: {$this->name}]";
}
}
Below is the Questions Entity:
<?php
namespace Survey\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Library\Entity\BaseEntity;
/**
* Description of Survey Questions
*
* #author Mubarak
*/
/**
* #ORM\Entity
* #ORM\Table(name="survey_questions")
*/
class Questions extends BaseEntity{
/**
* #ORM\Column(name="question", type="string")
* #var string
*/
protected $question;
/**
* #ORM\ManyToOne(targetEntity="Survey\Entity\Survey", inversedBy="questions")
* #ORM\JoinColumn(name="survey_id", referencedColumnName="id")
*/
private $surveys;
public function getQuestion() {
return $this->question;
}
public function setQuestion($question) {
$this->question = $question;
}
public function getSurveys() {
return $this->surveys;
}
public function setSurveys(ArrayCollection $surveys) {
$this->surveys = $surveys;
}
public function __toString() {
return __CLASS__ . ": [id: {$this->id}, name: {$this->name}]";
}
}
What is the mistake i am doing here, this is my code to get the surveys and questions:
$surveysInterface = $this->surveyService->getAllSurveys();
foreach($surveysInterface as $survey){
$surveysArray[] = array(
'id' => $survey->getId(),
'title' => $survey->getTitle(),
'description' => $survey->getDescription(),
'isActive' => $survey->getActive(),
'questions' => array(
'question' => $survey->getQuestions()->getQuestion()
)
);
}
'questions' => array(
'question' => $survey->getQuestions()->getQuestion()
)
This part look wrong since getQuestions() function return array of Questions entities. Suggest to change it to
'questions' => $survey->getQuestions()

symfony2 : unable to save parent id in embedded object

I'm trying tu use my first embedded form (SF2.1 + Doctrine2)
the entities :
<?php
namespace Chris\BabelBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
*/
class Needs
{
/**
* #ORM\GeneratedValue
* #ORM\Id
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Projects")
* #Assert\NotBlank()
*/
private $relatedproject;
/**
* #ORM\ManyToOne(targetEntity="Activities")
* #Assert\NotBlank()
*/
private $relatedactivity;
/**
* #ORM\ManyToOne(targetEntity="Jobs")
* #Assert\NotBlank()
*/
private $relatedjob;
/**
* #ORM\Column(type="string", length=255)
* #Assert\NotBlank()
*/
private $firstname;
/**
* #ORM\Column(type="string", length=255)
* #Assert\NotBlank()
*/
private $lastname;
/**
* #ORM\ManyToOne(targetEntity="Experiences")
* #Assert\NotBlank()
*/
private $relatedexperience;
/**
* #ORM\ManyToOne(targetEntity="Plants")
* #Assert\NotBlank()
*/
private $maintrainingplant;
/**
* #ORM\Column(type="date")
* #Assert\NotBlank()
*/
private $trainingneedstart;
/**
* #ORM\Column(type="date")
* #Assert\NotBlank()
*/
private $readytowork;
/**
* #var ArrayCollection $trainings
*
* #ORM\OneToMany(targetEntity="Trainings", mappedBy="training", cascade={"persist", "remove", "merge"})
*/
private $trainings;
/**
* Constructor
*/
public function __construct()
{
$this->trainings = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstname
*
* #param string $firstname
* #return Needs
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* #return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* #param string $lastname
* #return Needs
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* #return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set trainingneedstart
*
* #param \DateTime $trainingneedstart
* #return Needs
*/
public function setTrainingneedstart($trainingneedstart)
{
$this->trainingneedstart = $trainingneedstart;
return $this;
}
/**
* Get trainingneedstart
*
* #return \DateTime
*/
public function getTrainingneedstart()
{
return $this->trainingneedstart;
}
/**
* Set readytowork
*
* #param \DateTime $readytowork
* #return Needs
*/
public function setReadytowork($readytowork)
{
$this->readytowork = $readytowork;
return $this;
}
/**
* Get readytowork
*
* #return \DateTime
*/
public function getReadytowork()
{
return $this->readytowork;
}
/**
* Set relatedproject
*
* #param \Chris\BabelBundle\Entity\Projects $relatedproject
* #return Needs
*/
public function setRelatedproject(\Chris\BabelBundle\Entity\Projects $relatedproject = null)
{
$this->relatedproject = $relatedproject;
return $this;
}
/**
* Get relatedproject
*
* #return \Chris\BabelBundle\Entity\Projects
*/
public function getRelatedproject()
{
return $this->relatedproject;
}
/**
* Set relatedactivity
*
* #param \Chris\BabelBundle\Entity\Activities $relatedactivity
* #return Needs
*/
public function setRelatedactivity(\Chris\BabelBundle\Entity\Activities $relatedactivity = null)
{
$this->relatedactivity = $relatedactivity;
return $this;
}
/**
* Get relatedactivity
*
* #return \Chris\BabelBundle\Entity\Activities
*/
public function getRelatedactivity()
{
return $this->relatedactivity;
}
/**
* Set relatedjob
*
* #param \Chris\BabelBundle\Entity\Jobs $relatedjob
* #return Needs
*/
public function setRelatedjob(\Chris\BabelBundle\Entity\Jobs $relatedjob = null)
{
$this->relatedjob = $relatedjob;
return $this;
}
/**
* Get relatedjob
*
* #return \Chris\BabelBundle\Entity\Jobs
*/
public function getRelatedjob()
{
return $this->relatedjob;
}
/**
* Set relatedexperience
*
* #param \Chris\BabelBundle\Entity\Experiences $relatedexperience
* #return Needs
*/
public function setRelatedexperience(\Chris\BabelBundle\Entity\Experiences $relatedexperience = null)
{
$this->relatedexperience = $relatedexperience;
return $this;
}
/**
* Get relatedexperience
*
* #return \Chris\BabelBundle\Entity\Experiences
*/
public function getRelatedexperience()
{
return $this->relatedexperience;
}
/**
* Set maintrainingplant
*
* #param \Chris\BabelBundle\Entity\Plants $maintrainingplant
* #return Needs
*/
public function setMaintrainingplant(\Chris\BabelBundle\Entity\Plants $maintrainingplant = null)
{
$this->maintrainingplant = $maintrainingplant;
return $this;
}
/**
* Get maintrainingplant
*
* #return \Chris\BabelBundle\Entity\Plants
*/
public function getMaintrainingplant()
{
return $this->maintrainingplant;
}
/**
* Add trainings
*
* #param \Chris\BabelBundle\Entity\Trainings $trainings
* #return Needs
*/
public function addTraining(\Chris\BabelBundle\Entity\Trainings $trainings)
{
$this->trainings[] = $trainings;
return $this;
}
/**
* Remove trainings
*
* #param \Chris\BabelBundle\Entity\Trainings $trainings
*/
public function removeTraining(\Chris\BabelBundle\Entity\Trainings $trainings)
{
$this->trainings->removeElement($trainings);
}
/**
* Get trainings
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTrainings()
{
return $this->trainings;
}
public function __toString() {return $this->getFirstname();}
}
<?php
namespace Chris\BabelBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
*/
class Trainings
{
/**
* #ORM\GeneratedValue
* #ORM\Id
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Plants")
* #Assert\NotBlank()
*/
private $trainingplant;
/**
* #ORM\Column(type="date")
* #Assert\NotBlank()
*/
private $trainingstart;
/**
* #ORM\Column(type="date")
* #Assert\NotBlank()
*/
private $trainingend;
/**
* #var Needs $trainings
*
* #ORM\ManyToOne(targetEntity="Needs", inversedBy="trainings", cascade={"persist", "remove", "merge"})
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="Needs_id", referencedColumnName="id")
* })
*/
private $training;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set trainingstart
*
* #param \DateTime $trainingstart
* #return Trainings
*/
public function setTrainingstart($trainingstart)
{
$this->trainingstart = $trainingstart;
return $this;
}
/**
* Get trainingstart
*
* #return \DateTime
*/
public function getTrainingstart()
{
return $this->trainingstart;
}
/**
* Set trainingend
*
* #param \DateTime $trainingend
* #return Trainings
*/
public function setTrainingend($trainingend)
{
$this->trainingend = $trainingend;
return $this;
}
/**
* Get trainingend
*
* #return \DateTime
*/
public function getTrainingend()
{
return $this->trainingend;
}
/**
* Set trainingplant
*
* #param \Chris\BabelBundle\Entity\Plants $trainingplant
* #return Trainings
*/
public function setTrainingplant(\Chris\BabelBundle\Entity\Plants $trainingplant = null)
{
$this->trainingplant = $trainingplant;
return $this;
}
/**
* Get trainingplant
*
* #return \Chris\BabelBundle\Entity\Plants
*/
public function getTrainingplant()
{
return $this->trainingplant;
}
/**
* Set training
*
* #param \Chris\BabelBundle\Entity\Needs $training
* #return Trainings
*/
public function setTraining(\Chris\BabelBundle\Entity\Needs $training = null)
{
$this->training = $training;
return $this;
}
/**
* Get training
*
* #return \Chris\BabelBundle\Entity\Needs
*/
public function getTraining()
{
return $this->training;
}
}
The 2 Controllers :
<?php
namespace Chris\BabelBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Chris\BabelBundle\Entity\Needs;
use Chris\BabelBundle\Form\NeedsType;
/**
* Needs controller.
*
*/
class NeedsController extends Controller
{
/**
* Lists all Needs entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('ChrisBabelBundle:Needs')->findAll();
return $this->render('ChrisBabelBundle:Needs:index.html.twig', array(
'entities' => $entities,
));
}
/**
* Finds and displays a Needs entity.
*
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChrisBabelBundle:Needs')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Needs entity.');
}
return $this->render('ChrisBabelBundle:Needs:show.html.twig', array(
'entity' => $entity, ));
}
/**
* Displays a form to create a new Needs entity.
*
*/
public function newAction()
{
$entity = new Needs();
$form = $this->createForm(new NeedsType(), $entity);
return $this->render('ChrisBabelBundle:Needs:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Creates a new Needs entity.
*
*/
public function createAction(Request $request)
{
$entity = new Needs();
$form = $this->createForm(new NeedsType(), $entity);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('needs_show', array('id' => $entity->getId())));
}
return $this->render('ChrisBabelBundle:Needs:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Displays a form to edit an existing Needs entity.
*
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChrisBabelBundle:Needs')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Needs entity.');
}
$editForm = $this->createForm(new NeedsType(), $entity);
return $this->render('ChrisBabelBundle:Needs:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
));
}
/**
* Edits an existing Needs entity.
*
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChrisBabelBundle:Needs')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Needs entity.');
}
$editForm = $this->createForm(new NeedsType(), $entity);
$editForm->bind($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('needs_edit', array('id' => $id)));
}
return $this->render('ChrisBabelBundle:Needs:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
));
}
/**
* Deletes a Needs entity.
*
*/
public function deleteAction($id)
{
$em = $this->container->get('doctrine')->getEntityManager();
$metier = $em->find('ChrisBabelBundle:Needs', $id);
if (!$metier)
{
throw new NotFoundHttpException("Need not found");
}
$em->remove($metier); $em->flush();
return $this->redirect($this->generateUrl('needs'));
}
}
<?php
namespace Chris\BabelBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Chris\BabelBundle\Entity\Trainings;
use Chris\BabelBundle\Form\TrainingsType;
/**
* Trainings controller.
*
*/
class TrainingsController extends Controller
{
/**
* Lists all Trainings entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('ChrisBabelBundle:Trainings')->findAll();
return $this->render('ChrisBabelBundle:Trainings:index.html.twig', array(
'entities' => $entities,
));
}
/**
* Finds and displays a Trainings entity.
*
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChrisBabelBundle:Trainings')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Trainings entity.');
}
$deleteForm = $this->createDeleteForm($id);
return $this->render('ChrisBabelBundle:Trainings:show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(), ));
}
/**
* Displays a form to create a new Trainings entity.
*
*/
public function newAction()
{
$entity = new Trainings();
$form = $this->createForm(new TrainingsType(), $entity);
return $this->render('ChrisBabelBundle:Trainings:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Creates a new Trainings entity.
*
*/
public function createAction(Request $request)
{
$entity = new Trainings();
$form = $this->createForm(new TrainingsType(), $entity);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('trainings_show', array('id' => $entity->getId())));
}
return $this->render('ChrisBabelBundle:Trainings:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Displays a form to edit an existing Trainings entity.
*
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChrisBabelBundle:Trainings')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Trainings entity.');
}
$editForm = $this->createForm(new TrainingsType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render('ChrisBabelBundle:Trainings:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Edits an existing Trainings entity.
*
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChrisBabelBundle:Trainings')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Trainings entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createForm(new TrainingsType(), $entity);
$editForm->bind($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('trainings_edit', array('id' => $id)));
}
return $this->render('ChrisBabelBundle:Trainings:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a Trainings entity.
*
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChrisBabelBundle:Trainings')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Trainings entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('trainings'));
}
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm()
;
}
}
and finally the forms :
<?php
namespace Chris\BabelBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class NeedsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname')
->add('lastname')
->add('trainingneedstart')
->add('readytowork')
->add('relatedproject')
->add('relatedactivity')
->add('relatedjob')
->add('relatedexperience')
->add('maintrainingplant')
// Ajout du formulaire imbriqué des trainings en collection
->add('trainings', 'collection', array('type' => new TrainingsType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Chris\BabelBundle\Entity\Needs'
));
}
public function getName()
{
return 'chris_babelbundle_needstype';
}
}
<?php
namespace Chris\BabelBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TrainingsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('trainingstart')
->add('trainingend')
->add('trainingplant')
->add('training')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Chris\BabelBundle\Entity\Trainings'
));
}
public function getName()
{
return 'chris_babelbundle_trainingstype';
}
}
most of this is working except that the trainings saved do not contain the needs ID in the DB so, when editing a need I cant retrieve the several trainings.
Finally I found the solution with the help of French IRC :)
I only had to modify the Needs entity like this :
/**
* Add trainings
*
* #param \Chris\BabelBundle\Entity\Trainings $trainings
* #return Needs
*/
public function addTraining(\Chris\BabelBundle\Entity\Trainings $trainings)
{
$this->trainings[] = $trainings;
$trainings->setTraining($this);
return $this;
}

UniqueEntity using Doctrine 2

I am using Zend Framework 2 and I have created a User Entity. Now I am trying to make the username field unique. However the following error is coming up.
[Semantical Error] The annotation "#UniqueEntity" in class User\Entity\User was never imported. Did you maybe forget to add a "use" statement for this annotation?
I have added this code for uniqueness check
#UniqueEntity("email")
I can see that it is a method used in Symfony. How can I use it for Zend Framework 2?
This is the Entity I am using
<?php
namespace User\Entity;
use Doctrine\ORM\Mapping as ORM,
Zend\Form\Annotation;
/**
* A user entity.
*
* #ORM\Entity
* #ORM\Table(name="users")
* #UniqueEntity("email")
* #property int $id
* #property string $username
* #property string $email
* #property string $password
*
* #Annotation\Name("User")
*/
class User {
/**
* #ORM\Id
* #ORM\Column(type="integer");
* #ORM\GeneratedValue(strategy="AUTO")
*
* #Annotation\Required(false)
*/
protected $id;
/**
* #ORM\Column(type="string")
*
* #Annotation\Attributes({"type":"text"})
* #Annotation\Options({"label":"Username:"})
* #Annotation\Filter({"name":"StringTrim"})
* #Annotation\Filter({"name":"StripTags"})
*/
protected $username;
/**
* #ORM\Column(type="string")
*
* #Annotation\Attributes({"type":"text" })
* #Annotation\Options({"label":"Email:"})
* #Annotation\Filter({"name":"StringTrim"})
* #Annotation\Filter({"name":"StripTags"})
*/
protected $email;
/**
* #ORM\Column(type="string")
*
* #Annotation\Attributes({"type":"text" })
* #Annotation\Options({"label":"Password:"})
* #Annotation\Filter({"name":"StringTrim"})
* #Annotation\Filter({"name":"StripTags"})
*/
protected $password;
public function __get($property) {
return $this->$property;
}
/**
* Magic setter to save protected properties.
*
* #param string $property
* #param mixed $value
*/
public function __set($property, $value) {
$this->$property = $value;
}
public function getArrayCopy() {
return array(
'username' => $this->username,
'email' => $this->email,
'surname' => $this->surname,
'first_name' => $this->first_name,
'company' => $this->company,
'postcode' => $this->postcode,
);
}
public function populate($data) {
$this->username = isset($data['username']) ? $data['username'] : $this->username;
}
public function setDate($property, $value){
$this->$property = new \DateTime($value);
}
}
#UniqueEntity is a particular extension of the symfony validation component (written as annotation here). What you are looking for is probably the validators you can find in DoctrineModule: https://github.com/doctrine/DoctrineModule/blob/master/docs/validator.md
Support for it as annotation is not yet built-in.