Hi I try to solve this problem now for days. But I don't get it so I hope you can help me.
I have the entity Worker and it's in a many to many relation ship with the Task entity.
Now I don't want my user to do all input at one time. At first the user should enter his details and if they are valid he can choose his tasks.
So I added three methods to the standard generated code and change the url path in the createAction().
But if I press the submit button in my AddTaskType form there is nothing persisted to the database.
I hope you have suggestions for this problem.
//Worker Entity
namespace DLRG\HelferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* #ORM\Entity
* #ORM\Table(name="Workers")
*/
class Worker {
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
*/
protected $name;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
*/
protected $lastName;
/**
* #ORM\Column(type="string")
*/
protected $tel;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
* #Assert\Email()
*/
protected $email;
/**
* #ORM\Column(type="string")
*/
protected $organisation;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank()
* #Assert\Range(min=6, minMessage="Du musst mindestens 6 Jahre Alt sein um bei uns helfen zu können.")
*/
protected $age;
/**
* #ORM\ManyToMany(targetEntity="Task", inversedBy="workers")
* #ORM\JoinTable(name="workers_tasks")
*/
protected $tasks;
/**
* #ORM\ManyToMany(targetEntity="Time", inversedBy="workers")
* #ORM\JoinTable(name="workers_times")
*/
protected $times;
/**
* Constructor
*/
public function __construct() {
$this->tasks = new \Doctrine\Common\Collections\ArrayCollection ();
$this->times = new \Doctrine\Common\Collections\ArrayCollection ();
}
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Worker
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName() {
return $this->name;
}
/**
* Set lastName
*
* #param string $lastName
* #return Worker
*/
public function setLastName($lastName) {
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName() {
return $this->lastName;
}
/**
* Set tel
*
* #param string $tel
* #return Worker
*/
public function setTel($tel) {
$this->tel = $tel;
return $this;
}
/**
* Get tel
*
* #return string
*/
public function getTel() {
return $this->tel;
}
/**
* Set email
*
* #param string $email
* #return Worker
*/
public function setEmail($email) {
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail() {
return $this->email;
}
/**
* Set organisation
*
* #param string $organisation
* #return Worker
*/
public function setOrganisation($organisation) {
$this->organisation = $organisation;
return $this;
}
/**
* Get organisation
*
* #return string
*/
public function getOrganisation() {
return $this->organisation;
}
/**
* Set age
*
* #param integer $age
* #return Worker
*/
public function setAge($age) {
$this->age = $age;
return $this;
}
/**
* Get age
*
* #return integer
*/
public function getAge() {
return $this->age;
}
/**
* Add tasks
*
* #param \DLRG\HelferBundle\Entity\Task $tasks
* #return Worker
*/
public function addTask(\DLRG\HelferBundle\Entity\Task $task) {
$task->addWorker ( $this );
$this->tasks [] = $task;
return $this;
}
/**
* Remove tasks
*
* #param \DLRG\HelferBundle\Entity\Task $tasks
*/
public function removeTask(\DLRG\HelferBundle\Entity\Task $tasks) {
$this->tasks->removeElement ( $tasks );
}
/**
* Get tasks
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTasks() {
return $this->tasks;
}
/**
* Add times
*
* #param \DLRG\HelferBundle\Entity\Time $times
* #return Worker
*/
public function addTime(\DLRG\HelferBundle\Entity\Time $time) {
$time->addWorker ( $this );
$this->times [] = $time;
return $this;
}
/**
* Remove times
*
* #param \DLRG\HelferBundle\Entity\Time $times
*/
public function removeTime(\DLRG\HelferBundle\Entity\Time $times) {
$this->times->removeElement ( $times );
}
/**
* Get times
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTimes() {
return $this->times;
}
}
//TaskEntity
namespace DLRG\HelferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="Tasks")
*/
class Task {
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $name;
/**
* #ORM\Column(type="string")
*/
protected $description;
/**
* #ORM\ManyToMany(targetEntity="Time", mappedBy="tasks")
*/
protected $times;
/**
* #ORM\ManyToMany(targetEntity="Worker", mappedBy="tasks")
*/
protected $workers;
public function __toString() {
return $this->name;
}
/**
* Constructor
*/
public function __construct() {
$this->times = new \Doctrine\Common\Collections\ArrayCollection ();
$this->workers = new \Doctrine\Common\Collections\ArrayCollection ();
}
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Task
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName() {
return $this->name;
}
/**
* Set description
*
* #param string $description
* #return Task
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription() {
return $this->description;
}
/**
* Add times
*
* #param \DLRG\HelferBundle\Entity\Time $times
* #return Task
*/
public function addTime(\DLRG\HelferBundle\Entity\Time $time) {
$this->times [] = $time;
return $this;
}
/**
* Remove times
*
* #param \DLRG\HelferBundle\Entity\Time $times
*/
public function removeTime(\DLRG\HelferBundle\Entity\Time $times) {
$this->times->removeElement ( $times );
}
/**
* Get times
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTimes() {
return $this->times;
}
/**
* Add workers
*
* #param \DLRG\HelferBundle\Entity\Worker $workers
* #return Task
*/
public function addWorker(\DLRG\HelferBundle\Entity\Worker $worker) {
$this->workers [] = $worker;
return $this;
}
/**
* Remove workers
*
* #param \DLRG\HelferBundle\Entity\Worker $workers
*/
public function removeWorker(\DLRG\HelferBundle\Entity\Worker $workers) {
$this->workers->removeElement ( $workers );
}
/**
* Get workers
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getWorkers() {
return $this->workers;
}
}
The added methods
//WorkerController.php
/**
* Add task to a Worker
*
* #Route("/", name="task_add")
* #Method("POST")
* #Template("DLRGHelferBundle:Task:add.html.twig")
*/
public function addTask(Request $request, $id) {
$em = $this->getDoctrine ()->getManager ();
$entity = $em->getRepository ( 'DLRGHelferBundle:Worker' )->find ( $id );
if (! $entity) {
throw $this->createNotFoundException ( 'Unable to find Worker entity.' );
}
$form = $this->createAddTaskForm ( $entity );
$form->handleRequest ( $request );
if ($form->isValid ()) {
$formData = $form->get ( 'tasks' )->getData ();
foreach ( $formData as $task ) {
$entity->addTask ( $task );
}
$em->persist ( $entity );
$em->flush ();
return $this->redirect ( $this->generateUrl ( 'worker_show', array (
'id' => $entity->getId ()
) ) );
}
return array (
'entity' => $entity,
'form' => $form->createView ()
);
}
/**
* Creates a form to add a Task to Worker entity.
*
* #param Worker $entity
* The entity
*
* #return \Symfony\Component\Form\Form The form
*/
private function createAddTaskForm(Worker $entity) {
$form = $this->createForm ( new AddTaskType (), $entity, array (
'action' => $this->generateUrl ( 'task_add', array (
'id' => $entity->getId ()
) ),
'method' => 'POST'
) );
$form->add ( 'submit', 'submit', array (
'label' => 'Zeiten'
) );
return $form;
}
/**
* Displays a form to create a new Worker entity.
*
* #Route("/new/{id}/addTask", name="worker_task")
* #Method("GET")
* #Template()
*/
public function addAction($id) {
$em = $this->getDoctrine ()->getManager ();
$entity = $em->getRepository ( 'DLRGHelferBundle:Worker' )->find ( $id );
if (! $entity) {
throw $this->createNotFoundException ( 'Unable to find Worker entity.' );
}
$form = $this->createAddTaskForm ( $entity );
return array (
'entity' => $entity,
'form' => $form->createView ()
);
}
My AddTaskType looks like this:
namespace DLRG\HelferBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use DLRG\HelferBundle\Controller\HelferController;
class AddTaskType extends AbstractType {
/**
*
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add ( 'tasks', 'entity', array (
'class' => 'DLRGHelferBundle:Task',
'property' => 'name',
'multiple' => true
) );
}
/**
*
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults ( array (
'data_class' => 'DLRG\HelferBundle\Entity\Worker'
) );
}
/**
*
* #return string
*/
public function getName() {
return 'dlrg_helferbundle_addTask';
}
}
Related
i have added a new field/column to my "listing" entity, which is a boolean named "promoted" which is like one already present in the db "certified", it is created to behave exactly the same, however, it is'nt working..
The mariadb db updated correctly with the new row containing a boolean, but when i try to use it in a partial query or to update the field from 0 to 1 with a form it dont update, like it's unmapped, here's my code (not all just the important parts are it is 3 functions with 1000~+ lines: BaseListing.php
/**
* Listing
*
* #CocoricoAssert\ListingConstraint()
*
* #ORM\MappedSuperclass
*/
abstract class BaseListing
{
protected $price = 0;
/**
*
* #ORM\Column(name="certified", type="boolean", nullable=true)
*
* #var boolean
*/
protected $certified;
/**
*
* #ORM\Column(name="min_duration", type="smallint", nullable=true)
*
* #var integer
*/
/**
*
* #ORM\Column(name="promoted", type="boolean", nullable=true)
*
* #var boolean
*/
protected $promoted;
/**
* #return boolean
*/
public function isCertified()
{
return $this->certified;
}
/**
* #param boolean $certified
*/
public function setCertified($certified)
{
$this->certified = $certified;
}
/**
* #return boolean
*/
public function isPromoted()
{
return $this->promoted;
}
/**
* #param boolean $promoted
*/
public function setPromoted($promoted)
{
$this->promoted = $promoted;
}
}
here's AdminListing.php to generate the form for the admin panel to pass certified on / off and as i wish promoted on / off (cuted because it's a too large file):
class ListingAdmin extends AbstractAdmin
{
/** #inheritdoc */
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('admin.listing.title')
->add(
'status',
ChoiceType::class,
array(
'choices' => array_flip(Listing::$statusValues),
'placeholder' => 'admin.listing.status.label',
'translation_domain' => 'cocorico_listing',
'label' => 'admin.listing.status.label',
)
)
->add(
'adminNotation',
ChoiceType::class,
array(
'choices' => array_combine(
range(0, 10, 0.5),
array_map(
function ($num) {
return number_format($num, 1);
},
range(0, 10, 0.5)
)
),
'placeholder' => 'admin.listing.admin_notation.label',
'label' => 'admin.listing.admin_notation.label',
'required' => false,
)
)
->add(
'certified',
null,
array(
'label' => 'admin.listing.certified.label',
)
)
->add(
'promoted',
CheckboxType::class,
array(
'label' => 'admin.listing.promoted.label',
'required' => false,
)
)
This is not working because i have a query in ListingRepository that use "certified":
public function getFindSelectPart(QueryBuilder $queryBuilder)
{
$queryBuilder
->select("partial l.{id, price, averageRating, certified, createdAt, commentCount}")
->addSelect("partial t.{id, locale, slug, title, description}")
->addSelect("partial llcat.{id, listing, category}")
->addSelect("partial ca.{id, lft, lvl, rgt, root}")
->addSelect("partial cat.{id, locale, name}")
->addSelect("partial i.{id, name}")
->addSelect("partial u.{id, firstName}")
->addSelect("partial ln.{id, city, route, country}")
->addSelect("partial co.{id, lat, lng}")
->addSelect("partial ui.{id, name}")
->addSelect("'' AS DUMMY");//To maintain fields on same array level when extra fields are added
return $queryBuilder;
}
And when i put promoted just behind the certified in the partial query it says:
[Semantical Error] line 0, col 88 near partial.l Error: There is no mapped field named 'promoted' on class Cocorico\ListingBundle\Entity\Listing
besides i have done exactly the same setup as the certified value, i have cleared cache and updated DB scheme.
Here's my Entity\Listing.php :
?php
namespace Cocorico\ListingBundle\Entity;
use Cocorico\BookingBundle\Entity\Booking;
use Cocorico\ListingBundle\Model\BaseListing;
use Cocorico\ListingBundle\Model\ListingOptionInterface;
use Cocorico\ListingCategoryBundle\Entity\ListingListingCategory;
use Cocorico\ListingCharacteristicBundle\Entity\ListingListingCharacteristic;
use Cocorico\ListingDiscountBundle\Entity\ListingDiscount;
use Cocorico\ListingImageBundle\Entity\ListingImage;
use Cocorico\ListingLocationBundle\Entity\ListingLocation;
use Cocorico\MessageBundle\Entity\Thread;
use Cocorico\UserBundle\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Listing
*
* #ORM\Entity(repositoryClass="Cocorico\ListingBundle\Repository\ListingRepository")
*
* #ORM\Table(name="listing",indexes={
* #ORM\Index(name="created_at_l_idx", columns={"created_at"}),
* #ORM\Index(name="status_l_idx", columns={"status"}),
* #ORM\Index(name="price_idx", columns={"price"}),
* #ORM\Index(name="type_idx", columns={"type"}),
* #ORM\Index(name="min_duration_idx", columns={"min_duration"}),
* #ORM\Index(name="max_duration_idx", columns={"max_duration"}),
* #ORM\Index(name="average_rating_idx", columns={"average_rating"}),
* #ORM\Index(name="admin_notation_idx", columns={"admin_notation"}),
* #ORM\Index(name="platform_notation_idx", columns={"platform_notation"})
* })
*/
class Listing extends BaseListing
{
use ORMBehaviors\Timestampable\Timestampable;
use ORMBehaviors\Translatable\Translatable;
use \Cocorico\ListingSearchAdvancedBundle\Model\ListingSearchableTrait;
// use \Cocorico\ListingCategoryFieldBundle\Model\ListingCategoryFieldableTrait;
// use \Cocorico\DeliveryBundle\Model\ListingDeliverableTrait;
// use \Cocorico\ListingDepositBundle\Model\ListingDepositableTrait;
// use \Cocorico\ListingSessionBundle\Model\ListingSessionableTrait;
// use \Cocorico\ServiceBundle\Model\ListingTrait;
// use \Cocorico\ListingVideoBundle\Model\ListingVideoTrait;
// use \Cocorico\CarrierBundle\Model\ListingCarrierableTrait;
/**
* #ORM\Id
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\GeneratedValue(strategy="CUSTOM")
* #ORM\CustomIdGenerator(class="Cocorico\CoreBundle\Model\CustomIdGenerator")
*
* #var integer
*/
protected $id;
/**
* #Assert\NotBlank(message="assert.not_blank")
*
* #ORM\ManyToOne(targetEntity="Cocorico\UserBundle\Entity\User", inversedBy="listings", cascade={"persist"})
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*
* #var User
*/
protected $user;
/**
* #ORM\OneToOne(targetEntity="Cocorico\ListingLocationBundle\Entity\ListingLocation", inversedBy="listing", cas\
cade={"persist", "remove"}, orphanRemoval=true)
* #ORM\JoinColumn(name="location_id", referencedColumnName="id", onDelete="CASCADE")
*
* #var ListingLocation
**/
protected $location;
/**
* #ORM\OneToMany(targetEntity="Cocorico\ListingCategoryBundle\Entity\ListingListingCategory", mappedBy="listing\
", cascade={"persist", "remove"}, orphanRemoval=true)//, fetch="EAGER"
*
*/
protected $listingListingCategories;
/**
* For Asserts #see \Cocorico\ListingBundle\Validator\Constraints\ListingValidator
*
* #ORM\OneToMany(targetEntity="Cocorico\ListingImageBundle\Entity\ListingImage", mappedBy="listing", cascade={"\
persist", "remove"}, orphanRemoval=true)
* #ORM\OrderBy({"position" = "asc"})
*/
protected $images;
/**
* #ORM\OneToMany(targetEntity="Cocorico\ListingCharacteristicBundle\Entity\ListingListingCharacteristic", mappe\
dBy="listing", cascade={"persist", "remove"}, orphanRemoval=true) //, fetch="EAGER"
*
*/
protected $listingListingCharacteristics;
/**
*
* #ORM\OneToMany(targetEntity="Cocorico\ListingDiscountBundle\Entity\ListingDiscount", mappedBy="listing", casc\
ade={"persist", "remove"}, orphanRemoval=true)
* #ORM\OrderBy({"fromQuantity" = "asc"})
*/
protected $discounts;
/**
* #ORM\OneToMany(targetEntity="Cocorico\BookingBundle\Entity\Booking", mappedBy="listing", cascade={"persist", \
"remove"}, orphanRemoval=true)
* #ORM\OrderBy({"createdAt" = "desc"})
*/
protected $bookings;
/**
* #ORM\OneToMany(targetEntity="Cocorico\MessageBundle\Entity\Thread", mappedBy="listing", cascade={"remove"}, o\
rphanRemoval=true)
* #ORM\OrderBy({"createdAt" = "desc"})
*/
protected $threads;
/**
*
* #ORM\OneToMany(targetEntity="Cocorico\ListingBundle\Model\ListingOptionInterface", mappedBy="listing", cascad\
e={"persist", "remove"}, orphanRemoval=true)
*/
protected $options;
public function __construct()
{
$this->images = new ArrayCollection();
$this->listingListingCharacteristics = new ArrayCollection();
$this->listingListingCategories = new ArrayCollection();
$this->discounts = new ArrayCollection();
$this->bookings = new ArrayCollection();
$this->threads = new ArrayCollection();
$this->options = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add characteristics
*
* #param ListingListingCharacteristic $listingListingCharacteristic
* #return Listing
*/
public function addListingListingCharacteristic(ListingListingCharacteristic $listingListingCharacteristic)
{
$this->listingListingCharacteristics[] = $listingListingCharacteristic;
return $this;
}
/**
* Remove characteristics
*
* #param ListingListingCharacteristic $listingListingCharacteristic
*/
public function removeListingListingCharacteristic(ListingListingCharacteristic $listingListingCharacteristic)
{
$this->listingListingCharacteristics->removeElement($listingListingCharacteristic);
$listingListingCharacteristic->setListing(null);
}
/**
* Get characteristics
*
* #return \Doctrine\Common\Collections\Collection|ListingListingCharacteristic[]
*/
public function getListingListingCharacteristics()
{
return $this->listingListingCharacteristics;
}
/**
* Get characteristics ordered by Group and Characteristic
*
* #return ArrayCollection
*/
public function getListingListingCharacteristicsOrderedByGroup()
{
$iterator = $this->listingListingCharacteristics->getIterator();
$iterator->uasort(
function ($a, $b) {
/**
* #var ListingListingCharacteristic $a
* #var ListingListingCharacteristic $b
*/
$groupPosA = $a->getListingCharacteristic()->getListingCharacteristicGroup()->getPosition();
$groupPosB = $b->getListingCharacteristic()->getListingCharacteristicGroup()->getPosition();
$characteristicPosA = $a->getListingCharacteristic()->getPosition();
$characteristicPosB = $b->getListingCharacteristic()->getPosition();
if ($groupPosA == $groupPosB) {
if ($characteristicPosA == $characteristicPosB) {
return 0;
}
return ($characteristicPosA < $characteristicPosB) ? -1 : 1;
}
return ($groupPosA < $groupPosB) ? -1 : 1;
}
);
return new ArrayCollection(iterator_to_array($iterator));
}
/**
* Add characteristics
*
* #param ListingListingCharacteristic $listingListingCharacteristic
* #return Listing
*/
public function addListingListingCharacteristicsOrderedByGroup(
ListingListingCharacteristic $listingListingCharacteristic
) {
return $this->addListingListingCharacteristic($listingListingCharacteristic);
}
/**
* Remove characteristics
*
* #param ListingListingCharacteristic $listingListingCharacteristic
*/
public function removeListingListingCharacteristicsOrderedByGroup(
ListingListingCharacteristic $listingListingCharacteristic
) {
$this->removeListingListingCharacteristic($listingListingCharacteristic);
}
/**
* Add category
*
* #param ListingListingCategory $listingListingCategory
* #return Listing
*/
public function addListingListingCategory(ListingListingCategory $listingListingCategory)
{
$listingListingCategory->setListing($this);
$this->listingListingCategories[] = $listingListingCategory;
return $this;
}
/**
* Remove category
*
* #param ListingListingCategory $listingListingCategory
*/
public function removeListingListingCategory(ListingListingCategory $listingListingCategory)
{
// foreach ($listingListingCategory->getValues() as $value) {
// $listingListingCategory->removeValue($value);
// }
$this->listingListingCategories->removeElement($listingListingCategory);
}
/**
* Get categories
*
* #return \Doctrine\Common\Collections\Collection|ListingListingCategory[]
*/
public function getListingListingCategories()
{
return $this->listingListingCategories;
}
/**
* Set user
*
* #param \Cocorico\UserBundle\Entity\User $user
* #return Listing
*/
public function setUser(User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \Cocorico\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Add images
*
* #param ListingImage $image
* #return Listing
*/
public function addImage(ListingImage $image)
{
$image->setListing($this); //Because the owning side of this relation is listing image
$this->images[] = $image;
return $this;
}
/**
* Remove images
*
* #param ListingImage $image
*/
public function removeImage(ListingImage $image)
{
$this->images->removeElement($image);
$image->setListing(null);
}
/**
* Get images
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
/**
* Set location
*
* #param ListingLocation $location
* #return Listing
*/
public function setLocation(ListingLocation $location = null)
{
$this->location = $location;
//Needed to persist listing_id on listing_location table when inserting a new listing embedding a listing location form
$this->location->setListing($this);
}
/**
* Get location
*
* #return ListingLocation
*/
public function getLocation()
{
return $this->location;
}
/**
* Add discount
*
* #param ListingDiscount $discount
* #return Listing
*/
public function addDiscount(ListingDiscount $discount)
{
$discount->setListing($this);
$this->discounts[] = $discount;
return $this;
}
/**
* Remove discount
*
* #param ListingDiscount $discount
*/
public function removeDiscount(ListingDiscount $discount)
{
$this->discounts->removeElement($discount);
$discount->setListing(null);
}
/**
* Get discounts
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getDiscounts()
{
return $this->discounts;
}
/**
* #param ArrayCollection|ListingDiscount[] $discounts
*/
public function setDiscounts(ArrayCollection $discounts)
{
foreach ($discounts as $discount) {
$discount->setListing($this);
}
$this->discounts = $discounts;
}
/**
* #return \Doctrine\Common\Collections\Collection|Booking[]
*/
public function getBookings()
{
return $this->bookings;
}
/**
* #param ArrayCollection|Booking[] $bookings
*/
public function setBookings(ArrayCollection $bookings)
{
foreach ($bookings as $booking) {
$booking->setListing($this);
}
$this->bookings = $bookings;
}
/**
* Add booking
*
* #param Booking $booking
*
* #return Listing
*/
public function addBooking(Booking $booking)
{
$this->bookings[] = $booking;
return $this;
}
/**
* Remove booking
*
* #param Booking $booking
*/
public function removeBooking(Booking $booking)
{
$this->bookings->removeElement($booking);
}
/**
* #return mixed
*/
public function getThreads()
{
return $this->threads;
}
/**
* #param ArrayCollection|Thread[] $threads
*/
public function setThreads(ArrayCollection $threads)
{
foreach ($threads as $thread) {
$thread->setListing($this);
}
$this->threads = $threads;
}
/**
* Add thread
*
* #param Thread $thread
*
* #return Listing
*/
public function addThread(Thread $thread)
{
$this->threads[] = $thread;
return $this;
}
/**
* Remove thread
*
* #param Thread $thread
*/
public function removeThread(Thread $thread)
{
$this->threads->removeElement($thread);
}
/**
* Add ListingOption
*
* #param ListingOptionInterface $option
* #return Listing
*/
public function addOption($option)
{
$option->setListing($this);
$this->options[] = $option;
return $this;
}
/**
* Remove ListingOption
*
* #param ListingOptionInterface $option
*/
public function removeOption($option)
{
$this->options->removeElement($option);
}foreach ($threads as $thread) {
$thread->setListing($this);
}
$this->threads = $threads;
}
/**
* Add thread
*
* #param Thread $thread
*
* #return Listing
*/
public function addThread(Thread $thread)
{
$this->threads[] = $thread;
return $this;
}
/**
* Remove thread
*
* #param Thread $thread
*/
public function removeThread(Thread $thread)
{
$this->threads->removeElement($thread);
}
/**
* Add ListingOption
*
* #param ListingOptionInterface $option
* #return Listing
*/
public function addOption($option)
{
$option->setListing($this);
$this->options[] = $option;
return $this;
}
/**
* Remove ListingOption
*
* #param ListingOptionInterface $option
*/
public function removeOption($option)
{
$this->options->removeElement($option);
}
/**
* Get ListingOptions
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getOptions()
{
return $this->options;
}
/**
* #param ArrayCollection $options
* #return $this
*/
public function setOptions(ArrayCollection $options)
{
foreach ($options as $option) {
$option->setListing($this);
}
$this->options = $options;
return $this;
}
/**
* #param int $minImages
* #param bool $strict
*
* #return array
*/
public function getCompletionInformations($minImages, $strict = true)
{
$characteristic = 0;
foreach ($this->getListingListingCharacteristics() as $characteristics) {
if ($characteristics->getListingCharacteristicValue()) {
$characteristic = 1;
}
}
return array(
"title" => $this->getTitle() ? 1 : 0,
"description" => (
($strict && $this->getDescription()) ||
(!$strict && strlen($this->getDescription()) > 250)
) ? 1 : 0,
"price" => $this->getPrice() ? 1 : 0,
"image" => (
($strict && count($this->getImages()) >= $minImages) ||
(!$strict && count($this->getImages()) > $minImages)
) ? 1 : 0,
"characteristic" => $characteristic,
);
}
public function getTitle()
{
return (string)$this->translate()->getTitle();
}
public function getSlug()
{
return (string)$this->translate()->getSlug();
}
public function __toString()
{
return (string)$this->getTitle();
}
/**
* To add impersonating link into admin :
*
* #return User
*/
public function getImpersonating()
{
return $this->getUser();
}
}
I have found all subjects on google and did not get the solution.
I have this error:
Expected argument of type "PL\PlatformBundle\Entity\Module",
"Doctrine\Common\Collections\ArrayCollection" given
I want to select multiple Modules in the list but when I click on save I have the error.
My Form:
<?php
namespace PL\AppAccueilBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Doctrine\ORM\EntityRepository;
class AppAccueilModuleType extends AbstractType {
private $idCompany;
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$this->idCompany = $options['idCompany'];
$builder
->add('company', CollectionType::class, array(
'class' => 'PLUserBundle:Company',
'choice_label' => 'Name',
'multiple' => false
))
->remove('company')
->add('module', EntityType::class, array(
'class' => 'PLPlatformBundle:Module',
'choice_label' => function ($allChoices) {
if ($allChoices->getRef() != null)
return $allChoices->getRef() . ' - ' . $allChoices->getTitre() . " (" . $allChoices->getSupport()->getTitre() . ")";
else
return $allChoices->getTitre() . " (" . $allChoices->getSupport()->getTitre() . ")";
},
'multiple' => true,
'expanded' => false,
'query_builder' => function (EntityRepository $er) {
return $er->getModulesListForCompany($this->idCompany);
},
))
->add('save', SubmitType::class);;
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'PL\AppAccueilBundle\Entity\AppAccueilModule',
'idCompany' => null
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix() {
return 'pl_appaccueilbundle_appaccueilmodule';
}
}
My Entity:
<?php
namespace PL\PlatformBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use PL\AppAccueilBundle\Entity\ParcoursModule;
use PL\AppChallengeBundle\Entity\ChallengeModule;
use PL\UserBundle\Entity\Company;
use PL\UserBundle\Entity\Logo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Module
* #ORM\Table(name="pl_module")
* #ORM\Entity(repositoryClass="PL\PlatformBundle\Repositor\ModuleRepository")
*/
class Module {
/**
* #var int
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
* #ORM\Column(name="titre", type="string", length=255)
*/
private $titre;
/**
* #var string
* #ORM\Column(name="ref", type="string", length=255, nullable=true)
*/
private $ref;
/**
* #ORM\ManyToOne(targetEntity="PL\PlatformBundle\Entity\Support")
* #ORM\JoinColumn(nullable=false)
*/
private $support;
/**
* #ORM\ManyToOne(targetEntity="PL\PlatformBundle\Entity\Theme")
* #ORM\JoinColumn(nullable=false)
*/
private $theme;
/**
* #ORM\ManyToOne(targetEntity="PL\UserBundle\Entity\Company")
* #ORM\JoinColumn(nullable=false)
*/
private $company;
/**
* #ORM\OneToOne(targetEntity="PL\UserBundle\Entity\Logo", cascade="persist")
* #ORM\JoinTable(name="pl_logo")
* #Assert\Valid()
*/
private $logo;
/**
* #ORM\ManyToMany(targetEntity="PL\PlatformBundle\Entity\Document", cascade="persist")
* #ORM\JoinTable(name="pl_module_document")
*/
private $documents;
/**
* #ORM\ManyToMany(targetEntity="PL\PlatformBundle\Entity\ChasseZone", cascade="persist")
* #ORM\JoinTable(name="pl_module_chasse_zone")
*/
private $chasseZone;
/**
* #ORM\ManyToMany(targetEntity="PL\PlatformBundle\Entity\Question", cascade="persist")
* #ORM\JoinTable(name="pl_module_quiz")
* #ORM\OrderBy({"position" = "ASC"})
*/
private $question;
/**
* #var string
* #ORM\Column(name="code_iframe", type="string", length=4095, nullable=true)
*/
private $codeIframe;
/**
* #ORM\OneToOne(targetEntity="PL\PlatformBundle\Entity\ChasseImage", cascade="persist")
* #ORM\JoinTable(name="pl_module_chasse_image")
* #Assert\Valid()
*/
private $chasseImage;
/**
* #ORM\OneToMany(targetEntity="PL\AppAccueilBundle\Entity\ParcoursModule", mappedBy="module", cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $parcours;
/**
* #ORM\OneToMany(targetEntity="PL\AppChallengeBundle\Entity\ChallengeModule", mappedBy="module", cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $challenges;
/**
* Get id
* #return int
*/
public function getId() {
return $this->id;
}
/**
* Set titre
* #param string $titre
* #return module
*/
public function setTitre($titre) {
$this->titre = $titre;
return $this;
}
/**
* Get titre
* #return string
*/
public function getTitre() {
return $this->titre;
}
/**
* Set ref
* #param string $ref
* #return module
*/
public function setRef($ref) {
$this->ref = $ref;
return $this;
}
/**
* Get ref
* #return string
*/
public function getRef() {
return $this->ref;
}
/**
* Set support
* #param Support $support
* #return Module
*/
public function setSupport(Support $support) {
$this->support = $support;
return $this;
}
/**
* Get support
* #return Support
*/
public function getSupport() {
return $this->support;
}
/**
* Set theme
* #param Theme $theme
* #return Module
*/
public function setTheme(Theme $theme) {
$this->theme = $theme;
return $this;
}
/**
* Get theme
* #return Theme
*/
public function getTheme() {
return $this->theme;
}
/**
* Set company
*
* #param Company $company
* #return Module
*/
public function setCompany(Company $company) {
$this->company = $company;
return $this;
}
/**
* Get company
* #return Company
*/
public function getCompany() {
return $this->company;
}
/**
* Set logo
* #param Logo $logo
* #return Module
*/
public function setLogo(Logo $logo = null) {
$this->logo = $logo;
return $this;
}
/**
* Get logo
* #return Logo
*/
public function getLogo() {
return $this->logo;
}
/**
* Constructor
*/
public function __construct() {
$this->documents = new ArrayCollection();
$this->chasseZone = new ArrayCollection();
$this->question = new ArrayCollection();
$this->parcours = new ArrayCollection();
$this->challenges = new ArrayCollection();
}
/**
* Add document
* #param Document $document
* #return Module
*/
public function addDocument(Document $document) {
$this->documents[] = $document;
return $this;
}
/**
* Remove document
* #param Document $document
*/
public function removeDocument(Document $document) {
$this->documents->removeElement($document);
}
/**
* Get documents
* #return \Doctrine\Common\Collections\Collection
*/
public function getDocuments() {
return $this->documents;
}
/**
* Set codeIframe
* #param string $codeIframe
* #return Module
*/
public function setCodeIframe($codeIframe) {
$this->codeIframe = $codeIframe;
return $this;
}
/**
* Get codeIframe
* #return string
*/
public function getCodeIframe() {
return $this->codeIframe;
}
/**
* Set chasseImage
* #param ChasseImage $chasseImage
* #return Module
*/
public function setChasseImage(ChasseImage $chasseImage = null) {
$this->chasseImage = $chasseImage;
return $this;
}
/**
* Get chasseImage
* #return ChasseImage
*/
public function getChasseImage() {
return $this->chasseImage;
}
/**
* Remove chasseZone
* #param ChasseZone $ch
*/
public function removeChasseZone(ChasseZone $ch) {
$this->chasseZone->removeElement($ch);
}
/**
* Get chasseZone
* #return \Doctrine\Common\Collections\Collection
*/
public function getChasseZone() {
return $this->chasseZone;
}
/**
* Add chasseZone
* #param \PL\PlatformBundle\Entity\ChasseZone $chasseZone
* #return Module
*/
public function addChasseZone(ChasseZone $chasseZone) {
$this->chasseZone[] = $chasseZone;
return $this;
}
/**
* Get questions
* #return \Doctrine\Common\Collections\Collection
*/
public function getQuestions() {
return $this->question;
}
/**
* Add question
* #param \PL\PlatformBundle\Entity\Question $question
* #return Module
*/
public function addQuestion(Question $question) {
$this->question[] = $question;
return $this;
}
/**
* Remove question
* #param Question $question
*/
public function removeQuestion(Question $question) {
$this->question->removeElement($question);
}
/**
* Get parcours
* #return \Doctrine\Common\Collections\Collection
*/
public function getParcours() {
return $this->parcours;
}
/**
* Add parcours
* #param ParcoursModule $parcours
* #return Module
*/
public function addParcours(ParcoursModule $parcours) {
$this->parcours[] = $parcours;
$parcours->setModule($this);
return $this;
}
/**
* Remove parcours
* #param ParcoursModule $parcours
*/
public function removeParcours(ParcoursModule $parcours) {
$this->parcours->removeElement($parcours);
}
/**
* Get Challenges
* #return ArrayCollection
*/
public function getChallenges() {
return $this->challenges;
}
/**
* Add challenge module
* #param ChallengeModule $challenge
* #return Module
*/
public function addChallenge(ChallengeModule $challenge) {
$this->challenges[] = $challenge;
$challenge->setModule($this);
return $this;
}
/**
* Remove challenges module
* #param ChallengeModule $challenge
*/
public function removeChallenges(ChallengeModule $challenge) {
$this->challenges->removeElement($challenge);
}
}
Thinks
I am creating a Chat system. I have two entities Chat and ChatThread.
namespace Acme\UitBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Chat
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Acme\UitBundle\Entity\ChatRepository")
* #ORM\HasLifecycleCallbacks()
*/
class Chat
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="chats")
* #ORM\JoinColumn(name="uid", referencedColumnName="id")
*/
private $user;
/**
* #var string
*
* #ORM\Column(name="msg", type="text")
*/
private $msg;
/**
* #ORM\ManyToOne(targetEntity="ChatThread", inversedBy="chats")
* #ORM\JoinColumn(name="tid", referencedColumnName="id")
*/
private $thread;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* #param \stdClass $user
* #return Chat
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \stdClass
*/
public function getUser()
{
return $this->user;
}
/**
* Set msg
*
* #param string $msg
* #return Chat
*/
public function setMsg($msg)
{
$this->msg = $msg;
return $this;
}
/**
* Get msg
*
* #return string
*/
public function getMsg()
{
return $this->msg;
}
/**
* Set date
*
* #param \DateTime $date
* #return Chat
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* #ORM\PrePersist
*/
public function setDateValue()
{
$this->date = new \DateTime();
}
/**
* Set thread
*
* #param \Acme\UitBundle\Entity\ChatThread $thread
* #return Chat
*/
public function setThread(\Acme\UitBundle\Entity\ChatThread $thread = null)
{
$this->thread = $thread;
return $this;
}
/**
* Get thread
*
* #return \Acme\UitBundle\Entity\ChatThread
*/
public function getThread()
{
return $this->thread;
}
}
Entity ChatThread :
namespace Acme\UitBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\OrderBy;
use Doctrine\Common\Collections\ArrayCollection;
/**
* ChatThread
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Acme\UitBundle\Entity\ChatThreadRepository")
*/
class ChatThread
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="User", inversedBy="inside")
*/
private $inside;
/**
* #ORM\OneToMany(targetEntity="Chat", mappedBy="thread")
* #OrderBy({"date" = "DESC"})
*/
private $chats;
public function __construct()
{
$this->chats=new ArrayCollection();
$this->inside=new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set inside
*
* #param array $inside
* #return ChatThread
*/
public function setInside($inside)
{
$this->inside = $inside;
return $this;
}
/**
* Get inside
*
* #return array
*/
public function getInside()
{
return $this->inside;
}
/**
* Add inside
*
* #param \Acme\UitBundle\Entity\User $inside
* #return ChatThread
*/
public function addInside(\Acme\UitBundle\Entity\User $inside)
{
$this->inside[] = $inside;
return $this;
}
/**
* Remove inside
*
* #param \Acme\UitBundle\Entity\User $inside
*/
public function removeInside(\Acme\UitBundle\Entity\User $inside)
{
$this->inside->removeElement($inside);
}
/**
* Add chats
*
* #param \Acme\UitBundle\Entity\Chat $chats
* #return ChatThread
*/
public function addChat(\Acme\UitBundle\Entity\Chat $chats)
{
$this->chats[] = $chats;
return $this;
}
/**
* Remove chats
*
* #param \Acme\UitBundle\Entity\Chat $chats
*/
public function removeChat(\Acme\UitBundle\Entity\Chat $chats)
{
$this->chats->removeElement($chats);
}
/**
* Get chats
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getChats()
{
return $this->chats;
}
}
What I want to do is Take users to be added in inside and create a ChatThread and add a msg in Chat.
I have a form of chatType which takes user id's as collection with name thread. I have used a data transformer to transform inside into ChatThread.
namespace Acme\UitBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Acme\UitBundle\Form\DataTransformer\ThreadToInsideTransformer;
class ChatType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$entityManager = $options['em'];
$transformer = new ThreadToInsideTransformer($entityManager);
$builder->add(
$builder->create('thread', 'collection' ,array('allow_add' => true,'label' => false))->addModelTransformer($transformer)
);
$builder->add('msg','textarea');
/* $builder->add('inside','collection',array(
'allow_add' => true,
'allow_delete' => true)); */
$builder->add('hook', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Acme\UitBundle\Entity\Chat'
))
->setRequired(array(
'em',
))
->setAllowedTypes(array(
'em' => 'Doctrine\Common\Persistence\ObjectManager',
));
}
public function getName() {
return 'chat';
}
}
data transformer
namespace Acme\UitBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Common\Persistence\ObjectManager;
use Acme\UitBundle\Entity\ChatThread;
class ThreadToInsideTransformer implements DataTransformerInterface
{
/**
* #var ObjectManager
*/
private $om;
/**
* #param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
/**
* Transforms an object (thread) to an array (inside).
*
* #param Thread $thread
* #return array
*/
public function transform($thread)
{
if (null === $thread) {
return "";
}
return $thread->getInside();
}
/**
* Transforms a array (inside) to an object (thread).
*
* #param array $inside
*
* #return Thread
*
* #throws Create new Thread if object (thread) is not found.
*/
public function reverseTransform($inside)
{
if (!is_array($inside)) {
return null;
}
$a=$this->om
->getRepository('AcmeUitBundle:User')
->findById($inside);
if (null === $a) {
throw new TransformationFailedException(sprintf(
'Error. . .'
));
}
$thread = $this->om
->getRepository('AcmeUitBundle:ChatThread')
->findByInsideChat($a)
;
if (null === $thread) {
$thread=new ChatThread();
$thread->setInside($inside);
$this->om->persist($thread);
$this->om->flush();
}
return $thread;
}
}
When I submit form, I get this error Expected argument of type "object, array or empty", "string" given.
Hi I'm writing a little Web App with Symfony.
I have a one to many relation ship between two entities.
Entity one:
/**
* #ORM\Entity
* #ORM\Table(name="Helfer")
*/
class Helfer {
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
*/
protected $name;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
*/
protected $lastName;
/**
* #ORM\Column(type="string")
*/
protected $tel;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
* #Assert\Email()
*/
protected $email;
/**
* #ORM\Column(type="string")
*/
protected $organisation;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank()
* #Assert\Range(min=6, minMessage="Du musst mindestens 6 Jahre Alt sein um bei uns helfen zu können.")
*/
protected $age;
/**
* #ORM\OneToMany(targetEntity="Aufgaben", mappedBy="helfer", cascade={"persist","remove"})
*/
protected $aufgaben;
/**
* #ORM\OneToMany(targetEntity="Zeiten", mappedBy="helfer", cascade={"persist","remove"})
*/
protected $zeiten;
/**
* Constructor
*/
public function __construct()
{
$this->aufgaben = new \Doctrine\Common\Collections\ArrayCollection();
$this->zeiten = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Helfer
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set lastName
*
* #param string $lastName
* #return Helfer
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set tel
*
* #param string $tel
* #return Helfer
*/
public function setTel($tel)
{
$this->tel = $tel;
return $this;
}
/**
* Get tel
*
* #return string
*/
public function getTel()
{
return $this->tel;
}
/**
* Set email
*
* #param string $email
* #return Helfer
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set organisation
*
* #param string $organisation
* #return Helfer
*/
public function setOrganisation($organisation)
{
$this->organisation = $organisation;
return $this;
}
/**
* Get organisation
*
* #return string
*/
public function getOrganisation()
{
return $this->organisation;
}
/**
* Set age
*
* #param integer $age
* #return Helfer
*/
public function setAge($age)
{
$this->age = $age;
return $this;
}
/**
* Get age
*
* #return integer
*/
public function getAge()
{
return $this->age;
}
/**
* Add aufgaben
*
* #param \DLRG\HelferBundle\Entity\Aufgaben $aufgaben
* #return Helfer
*/
public function addAufgaben(\DLRG\HelferBundle\Entity\Aufgaben $aufgaben)
{
$this->aufgaben[] = $aufgaben;
return $this;
}
/**
* Remove aufgaben
*
* #param \DLRG\HelferBundle\Entity\Aufgaben $aufgaben
*/
public function removeAufgaben(\DLRG\HelferBundle\Entity\Aufgaben $aufgaben)
{
$this->aufgaben->removeElement($aufgaben);
}
/**
* Get aufgaben
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getAufgaben()
{
return $this->aufgaben;
}
/**
* Add zeiten
*
* #param \DLRG\HelferBundle\Entity\Zeiten $zeiten
* #return Helfer
*/
public function addZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
{
$this->zeiten[] = $zeiten;
return $this;
}
/**
* Remove zeiten
*
* #param \DLRG\HelferBundle\Entity\Zeiten $zeiten
*/
public function removeZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
{
$this->zeiten->removeElement($zeiten);
}
/**
* Get zeiten
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getZeiten()
{
return $this->zeiten;
}
}
Entity two:
/**
* #ORM\Entity
* #ORM\Table(name="Aufgaben")
*/
class Aufgaben {
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $name;
/**
* #ORM\Column(type="string")
*/
protected $beschreibung;
/**
* #ORM\ManyToOne(targetEntity="Helfer", inversedBy="aufgaben")
* #ORM\JoinColumn(name="helfer_aufgaben_id", referencedColumnName="id")
*/
protected $helfer;
/**
* #ORM\OneToMany(targetEntity="Zeiten", mappedBy="aufgaben", cascade={"persist","remove"})
*/
protected $zeiten;
/**
* Constructor
*/
public function __construct()
{
$this->zeiten = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Aufgaben
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set beschreibung
*
* #param string $beschreibung
* #return Aufgaben
*/
public function setBeschreibung($beschreibung)
{
$this->beschreibung = $beschreibung;
return $this;
}
/**
* Get beschreibung
*
* #return string
*/
public function getBeschreibung()
{
return $this->beschreibung;
}
/**
* Set helfer
*
* #param \DLRG\HelferBundle\Entity\Helfer $helfer
* #return Aufgaben
*/
public function setHelfer(\DLRG\HelferBundle\Entity\Helfer $helfer = null)
{
$this->helfer = $helfer;
return $this;
}
/**
* Get helfer
*
* #return \DLRG\HelferBundle\Entity\Helfer
*/
public function getHelfer()
{
return $this->helfer;
}
/**
* Add zeiten
*
* #param \DLRG\HelferBundle\Entity\Zeiten $zeiten
* #return Aufgaben
*/
public function addZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
{
$this->zeiten[] = $zeiten;
return $this;
}
/**
* Remove zeiten
*
* #param \DLRG\HelferBundle\Entity\Zeiten $zeiten
*/
public function removeZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
{
$this->zeiten->removeElement($zeiten);
}
/**
* Get zeiten
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getZeiten()
{
return $this->zeiten;
}
}
My database has entries in Aufgaben table. Now my user should enter their names and attributes in Helfer.php. Than the user should choose some values from the Aufgaben Entity.
I try to add the Values from Aufgaben.php to a collection.
class HelferType extends AbstractType {
private $aufgaben;
public function __construct($aufgaben) {
$this->aufgaben = $aufgaben;
}
/**
*
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add ( 'name' )->add ( 'lastName' )->add ( 'tel' )->add ( 'email' )->add ( 'organisation' )->add ( 'age' );
$builder->add ( 'aufgaben', 'collection', array (
'type' => 'choice',
'options' => array (
'choices' => $this->aufgaben
)
) );
}
But this code doesn't work.
Any Ideas ?
Workflow should be:
Enter User information
Choose one or more values from Aufgaben.php
Save all
Thanks in advance
Depending on your exact needs, one possible solution would be to use an entity form type.
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add ( 'name' )->add ( 'lastName' )->add ( 'tel' )->add ( 'email' )->add ( 'organisation' )->add ( 'age' );
$builder->add ( 'aufgaben', 'entity', array (
'class' => 'YourBundle:Aufgaben',
'multiple' => true,
'property' => 'name',
'choices' => $this->aufgaben
)
);
}
This would result in a SELECT tag in your HTML.
maybe this and this can help you.
I resolve this kind of cases overriding the __toString() method in my entity file, not the repository file.
I need to add questionnaire of multiple choice questions to my registration form. The questions and options are in two entities:
<?php
namespace Me\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Question
*
* #ORM\Table(name="question")
* #ORM\Entity(repositoryClass="Me\UserBundle\Entity\QuestionRepository")
*/
class Question
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="questionText", type="text")
*/
private $questionText;
/**
* #var boolean $expanded
*
* #ORM\Column(name="expanded", type="boolean")
*/
private $expanded;
/**
* #var boolean $multiple
*
* #ORM\Column(name="multiple", type="boolean")
*/
private $multiple;
/**
* #var Questionnaire $questionnaire
*
* #ORM\ManyToOne(targetEntity="Questionnaire", inversedBy="questions")
* #ORM\JoinColumn(name="questionnaire", referencedColumnName="id", onDelete="cascade")
*/
private $questionnaire;
/**
* #var \Doctrine\Common\Collections\ArrayCollection $options
*
* #ORM\OneToMany(targetEntity="Option", mappedBy="question", cascade={"all"})
*/
private $options;
public function __construct()
{
$this->expanded = false;
$this->multiple = false;
$this->options = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set questionText
*
* #param string $questionText
* #return Question
*/
public function setQuestionText($questionText)
{
$this->questionText = $questionText;
return $this;
}
/**
* Get questionText
*
* #return string
*/
public function getQuestionText()
{
return $this->questionText;
}
/**
* #param mixed $options
*/
public function setOptions($options)
{
$this->options[] = $options;
return $this;
}
/**
* #return mixed
*/
public function getOptions()
{
return $this->options;
}
function __toString()
{
return $this->getQuestionText();
}
/**
* #param boolean $expanded
*/
public function setExpanded($expanded)
{
$this->expanded = $expanded;
}
/**
* #return boolean
*/
public function getExpanded()
{
return $this->expanded;
}
/**
* #param boolean $multiple
*/
public function setMultiple($multiple)
{
$this->multiple = $multiple;
}
/**
* #return boolean
*/
public function getMultiple()
{
return $this->multiple;
}
/**
* #param \Me\UserBundle\Entity\Questionnaire $questionnaire
*/
public function setQuestionnaire($questionnaire)
{
$this->questionnaire = $questionnaire;
}
/**
* #return \Me\UserBundle\Entity\Questionnaire
*/
public function getQuestionnaire()
{
return $this->questionnaire;
}
}
and
<?php
namespace Me\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* QuestionOption
*
* #ORM\Table(name="option")
* #ORM\Entity(repositoryClass="Me\UserBundle\Entity\OptionRepository")
*/
class Option
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="questionId", type="integer")
*/
private $questionId;
/**
* #var string
*
* #ORM\Column(name="optionText", type="string", length=255)
*/
private $optionText;
/**
* #ORM\ManyToOne(targetEntity="Question", inversedBy="options")
* #ORM\JoinColumn(name="questionId", referencedColumnName="id", onDelete="cascade")
**/
private $question;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set optionText
*
* #param string $optionText
* #return Option
*/
public function setOptionText($optionText)
{
$this->optionText = $optionText;
return $this;
}
/**
* Get optionText
*
* #return string
*/
public function getOptionText()
{
return $this->optionText;
}
/**
* #return mixed
*/
public function getQuestion()
{
return $this->question;
}
/**
* #param mixed $question
*/
public function setQuestion($question)
{
$this->question = $question;
}
/**
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
function __toString()
{
return $this->getOptionText();
}
}
I also have a questionnaire entity, though I don't think I really need it because users won't be creating questionnaires, only filling the single questionnaire during registration.
My user entity:
<?php
namespace Me\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity(repositoryClass="Me\UserBundle\Entity\UserRepository")
*/
class User
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="firstName", type="string", length=50)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="middleInitial", type="string", length=50)
*/
private $middleInitial;
/**
* #var string
*
* #ORM\Column(name="lastName", type="string", length=50)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="homePhoneArea", type="string", length=3)
*/
private $homePhoneArea;
/**
* #var string
*
* #ORM\Column(name="homePhoneNumber", type="string", length=7)
*/
private $homePhoneNumber;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=50)
*/
private $email;
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*/
public $questions;
public function __construct()
{
$this->questions = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* #param string $firstName
* #return User
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set middleInitial
*
* #param string $middleInitial
* #return User
*/
public function setMiddleInitial($middleInitial)
{
$this->middleInitial = $middleInitial;
return $this;
}
/**
* Get middleInitial
*
* #return string
*/
public function getMiddleInitial()
{
return $this->middleInitial;
}
/**
* Set lastName
*
* #param string $lastName
* #return User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set homePhoneArea
*
* #param string $homePhoneArea
* #return User
*/
public function setHomePhoneArea($homePhoneArea)
{
$this->homePhoneArea = $homePhoneArea;
return $this;
}
/**
* Get homePhoneArea
*
* #return string
*/
public function getHomePhoneArea()
{
return $this->homePhoneArea;
}
/**
* Set homePhoneNumber
*
* #param string $homePhoneNumber
* #return User
*/
public function setHomePhoneNumber($homePhoneNumber)
{
$this->homePhoneNumber = $homePhoneNumber;
return $this;
}
/**
* Get homePhoneNumber
*
* #return string
*/
public function getHomePhoneNumber()
{
return $this->homePhoneNumber;
}
/**
* Set email
*
* #param string $email
* #return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* #return \Doctrine\Common\Collections\ArrayCollection
*/
public function getQuestions()
{
return $this->questions;
}
}
My user controller:
public function newAction()
{
$user = new User();
$em = $this->getDoctrine()->getManager();
$questions = $em->getRepository('MeUserBundle:Question')->findAll();
if (!$questions) {
throw $this->createNotFoundException('Unable to find Questions.');
}
$builder = $this->createFormBuilder();
$optionEntities = array();
foreach ($questions as $question)
{
$options = array();
foreach ($question->getOptions() as $option)
{
$options[$option->getId()] = $option->getOptionText();
$optionEntities[$option->getId()] = $option;
}
$builder->add('question_'. $question->getId(), 'choice', array(
'label' => $question->getQuestionText(),
'expanded' => $question->getExpanded(),
'multiple' => $question->getMultiple(),
'choices' => $options
));
}
$user->getQuestions()->add($questions);
$form = $this->createForm(new MyFormType(), array('User' => $user));
return $this->render('MeUserBundle:User:new.html.twig', array(
'entity' => $user,
'form' => $form->createView(),
));
}
The form type as it stands today:
<?php
namespace Me\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MyFormType extends AbstractType
{
protected $questions;
public function __construct( $questions)
{
$this->questions = $questions;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('questions', 'collection', array(
'type' => new QuestionType()
))
->add('firstName')
->add('middleInitial')
->add('lastName')
->add('homePhoneArea')
->add('homePhoneNumber')
->add('email')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
));
}
public function getName()
{
return 'myform_type';
}
}
This setup doesn't work to get the questions and their associated options, and display them in the same user creation form. I've seen instructions and docs for combining forms, but not creating forms with this kind of configuration. Any guidance would be appreciated.
I'm not sure if I've understood your question correctly but maybe this could help.
From what I see you're building the form(for questions) but you're not using it anywhere! The easiest way is to pass the questions(in your case $user->getQuestions() ) to the MyFormType and the add all the questions inside of the type.
So it would look like something like this
$this->createForm(new MyFormType($user->getQuestions()), array('User' => $user));
And inside your type
protected $questions;
public function __construct($questions)
{
$this->questions = $questions;
}
protected $questions;
public function __construct($questions)
{
$this->questions = $questions;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach ($this->questions as $question)
{
$options = array();
foreach ($question->getOptions() as $option)
{
$options[$option->getId()] = $option->getOptionText();
$optionEntities[$option->getId()] = $option;
}
$builder->add('question_'. $question->getId(), 'choice', array(
'label' => $question->getQuestionText(),
'expanded' => $question->getExpanded(),
'multiple' => $question->getMultiple(),
'choices' => $options
));
}
}
Edit 1
Why don't you try the following?
Add the method setQuestionnaire in User and create a type called QuestionnaireType which is responsible to create the whole questionnaire
In the UserType(sorry for the wrong names) add the QuestionnaireType as an embedded form
Once the user submits the data and you call the bind symfony will pass the whole questionnaire object to the created method so you can iterate over it and save the user's aswers!
Your users entity needs a relation to his $answers you should store in an $answers-field in your user entity, (look up "embedding collections")
Then in your controller that digests the form your store the values by $user->setAnswers(value)) and then you'll find the answers values in the users entity's $answers field ($user->getAnswers()).
ANd dont forget to add your getters and setters.
$php app/console doctrine:generate:entities BundleName:Entityname
At first you could add a setQuestions method in user, this method will receive an ArrayCollection.
Then have you got a look at http://symfony.com/doc/current/reference/forms/types/collection.html ?
And maybe you could ask yourself what does this form should display/do
Does it need to embbed a collection or a list of QuestionFormType could be ok?
Each time i have to do embedded form type, i draw sketches and activity diagram for being sure to not dive into a to complex form structure