I am having issues saving the selections made in a entity form field with multiple=true.
The selections come through when $form->bindRequest($request) is called but don't persist in the database when flush is called.
Here is the relevant controller code:
$news_item = new News();
$form = $this->createFormBuilder($news_item)
->add('products', 'entity',
array('class' => 'AcmeDemoBundle:Product',
'multiple' => TRUE))
->getForm();
$request = $this->getRequest();
if($request->getMethod() == "POST") {
$form->bindRequest($request);
if($form->isValid()) {
$this->em->persist($news_item);
$this->em->flush();
}
}
I have checked the $news_item object after $form->isValid() and a count($news_item->getProducts()) returns the correct number of items. The $news_item itself is saved in the DB but the ManyToMany relationship isn't being saved.
Here are the entities for reference (clipped for brevity):
/**
* #ORM\Entity
* #ORM\Table(name="Product")
*/
class Product {
/*
* #ORM\Id #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="News", inversedBy="products")
*/
protected $news_items = null;
public function __construct() {
$this->news_items = new ArrayCollection();
}
}
/**
* #ORM\Entity
* #ORM\Table(name="News")
*/
class News {
/**
* #ORM\Id #ORM\Column(type="integer") #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="Product", mappedBy="news_items")
*/
protected $products = null;
public function __construct() {
$this->products = new ArrayCollection();
}
}
I think you are missing $product->addNewsItem($news_item) and $news_item->addProduct($product) in your code this because in bi-directional associations (seems your case) you have to update the fields on both sides.
To avoid this you can set the cascade option on both sides of the association:
#ORM\ManyToMany(targetEntity="Product", mappedBy="news_items",
cascade={"persist", "remove"})
This way your code will work. You can choose the appropriate cascade options looking at here.
I'm not having the same problem that you are having, but I needed to be able to load FixtureData using similar entities. I was referred to this documentation: http://www.doctrine-project.org/docs/orm/2.1/en/reference/working-with-associations.html and I believe it's going to solve my problem. I hope this works for you too.
JS
Related
I am a beginner in Symfony.
I have a strange problem in my form.
I have 2 entities : Proposal_Lsi and Lsi_Beams. One proposal can have multiple beams, but a beam can only have one proposal. I figured I should use a OneToMany/ManyToOne relation, and that my owning side is the beam one, and inverse side is proposal.
I followed the official guide at https://symfony.com/doc/3.1/form/form_collections.html about Form Collections.
Everything renders just fine, I can submit a new proposal with multiple beams, and all is correctly stored in the database.
The problem occurs whenever I try to add new beams to my proposal : the systems overwrites (update query) existing beams (starting by the first one in the database) instead of adding new ones (insert query).
What am I missing ?
Here's some of my code, if that can help.
Proposal Class:
class Proposal_lsi{
/**
* #ORM\OneToOne(targetEntity="Emir2Bundle\Entity\Proposal", inversedBy="proposal_lsi")
* #ORM\JoinColumn(name="proposal", referencedColumnName="id")
* #ORM\Id
*/
private $proposal;
/**
* #ORM\OneToMany(targetEntity="Emir2Bundle\Entity\Lsi_beams", mappedBy="proposal_lsi")
*/
private $lsi_beams;
...
/**
* Add lsiBeam
*
* #param \Emir2Bundle\Entity\Lsi_beams $lsiBeam
* #return Proposal_lsi
*/
public function addLsiBeam(\Emir2Bundle\Entity\Lsi_beams $lsiBeam)
{
$lsiBeam->setProposalLsi($this);
$this->lsi_beams[] = $lsiBeam;
return $this;
}
}
Beams Class:
class Lsi_beams{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Emir2Bundle\Entity\Proposal_lsi", inversedBy="lsi_beams", cascade={"persist"})
* #ORM\JoinColumn(name="proposal_lsi", referencedColumnName="proposal", nullable=false)
*/
private $proposal_lsi;
...
}
And the form in the controller :
$form = $this->createFormBuilder($proposallsi)
->setAction($this->generateUrl('lsi_submission', array('id' => $id)))
->setMethod('POST')
->add('lsi_beams', CollectionType::class, array(
'entry_type' => LsiBeamsType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false
)
)
...
What am I doing wrong ? Let me know if you need more code.
Thanks for any reply !
Notes:
use Doctrine ArrayCollection to better keep track of collections
put cascade={"persist"} at the inverse side of association (where you have mappedBy)
Keep entity names singular (e.g. Lsi_beam instead of Lsi_beams)
Keep your naming strategy clear and strait. Don't use undescores in your class & property names (e.g. use $lsiBeams instead of $lsi_beams)
ProposalLsi
use Doctrine\Common\Collections\ArrayCollection;
class ProposalLsi
{
/**
* #ORM\OneToMany(targetEntity="LsiBeam", mappedBy="proposalLsi", cascade={"persist"})
*/
private $lsiBeams;
public function __construct()
{
$this->lsiBeams = new ArrayCollection();
}
public function addLsiBeam(LsiBeams $lsiBeam)
{
if ($this->lsiBeams->contains($lsiBeam)) {
return;
} else {
$lsiBeam->setProposalLsi($this);
$this->lsiBeams->add($lsiBeam);
}
return $this;
}
public function removeLsiBeam(LsiBeams $lsiBeam)
{
if (!$this->lsiBeams->contains($lsiBeam)) {
return;
} else {
$lsiBeam->setProposalLsi(null);
$this->lsiBeams->removeElement($lsiBeam);
}
return $this;
}
}
LsiBeam
class LsiBeam
{
/**
* #ORM\ManyToOne(targetEntity="ProposalLsi", inversedBy="lsiBeams")
*/
private $proposalLsi;
public function setProposalLsi(?ProposalLsi $proposalLsi)
{
$this->proposalLsi = $proposalLsi;
}
}
In my admin I have a OneToMany defined as it:
/**
* #ORM\OneToMany(targetEntity="Module", mappedBy="sequence", cascade={"persist", "remove"})
*/
private $modules;
And the inversed side:
/**
* #ORM\ManyToOne(targetEntity="ModuleSequence", inversedBy="modules", cascade={"persist"}, fetch="LAZY")
* #ORM\JoinColumn(name="sequence_id", referencedColumnName="id")
*/
protected $sequence;
In my admin class I defined the 'modules' field as it:
->add('modules', 'sonata_type_collection',array(
'by_reference' => false
))
Finally in the ModuleSequence Entity here's the addModule method:
/**
* Add modules
*
* #param \AppBundle\Entity\Module $module
* #return ModuleSequence
*/
public function addModule(\AppBundle\Entity\Module $module)
{
$module->setSequence($this);
$this->modules[] = $module;
return $this;
}
I have the "add" button, I get the modal, I fill it and validate. The Ajax request is sent into the profiler but no new row appear.
The 'sequence_id' is not set in the database and I don't know why... Any idea please?
When I use the 'inline' & 'table' options, the id is well set.
Had the same issue and solved it with overriding the prePersist and preUpdate methods and then persist the associations.
public function prePersist($object)
{
$this->persistBlocks($object);
$content->mergeNewTranslations();
}
public function preUpdate($object)
{
$this->prePersist($object);
}
private function persistModules($object)
{
$em = $this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager');
foreach($object->getModules() as $module)
{
$module->setObject($object); // change Object to the name of your Entity!!
$em->persist($module);
}
}
After a long discussion on Sonata Admin GitHub here:
https://github.com/sonata-project/SonataAdminBundle/issues/4236
Problem partially solved...
I'm in a situation that need to update a Doctrine2 Entity and exclude some fields.
With ZF2 i have an action to handle update using Zend\Form and validation filter. In particular Dish Entity have a blob column called photo that is required. During an update i want to replace the photo only if a new file is provided.
Here there are the source code for the entity and the controller action that update dish.
Dishes\Entity\Dish.php
<?php
namespace Dishes\Entity;
use Doctrine\ORM\Mapping as ORM;
/** #ORM\Entity **/
class Dish
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
**/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $name;
/**
* #ORM\Column(type="text")
*/
protected $description;
/**
* #ORM\Column(type="integer")
*/
protected $time;
/**
* #ORM\Column(type="integer")
*/
protected $complexity;
/**
* #ORM\Column(type="blob")
*/
protected $photo;
/**
* Magic getter to expose protected properties.
*
* #param string $property
* #return mixed
*/
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;
}
}
Dishes\Controller\AdminController.php
public function editDishAction()
{
//Params from url
$id = (int) $this->params()->fromRoute('id', 0);
$objectManager = $this->objectManager;
$hydrator = new DoctrineObject($objectManager, false);
$form = new DishForm();
$existingDish = $objectManager->find('Dishes\Entity\Dish', $id);
if ($existingDish === NULL)
$this->notFoundAction();
$request = $this->getRequest();
if ($request->isPost())
{
$filter = new DishFilter();
$filter->get('photo')->setRequired(false);
$form->setHydrator($hydrator)
->setObject($existingDish)
->setInputFilter($filter);
$post = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
//Backup photo stream
$imageData = $existingDish->photo;
$form->setData($post);
if ($form->isValid())
{
//If user upload a new image read it.
if(!empty($existingDish->photo['tmp_name']))
$imageData = file_get_contents($existingDish->photo['tmp_name']);
$existingDish->photo = $imageData;
$objectManager->flush();
$this->redirect()->toRoute('zfcadmin/dishes');
}
}
else
{
$data = $hydrator->extract($existingDish);
unset($data['photo']);
$form->setData($data);
}
return new ViewModel(array('form' => $form));
}
Actually i set $dish->photo property to NULL but this violate DB NOT NULL constraint.
How can I tell Doctrine to exclude a particular entity field from update at runtime?
Doctrine maps every column's nullable property in database level to false by default since you don't set any nullable flag in your annotation:
/**
* #ORM\Column(type="blob")
*/
protected $photo;
This means, "Photo is required, you can't insert or update row's photo column with a null value".
If you want to have null values in your database, use the following annotation:
/**
* #ORM\Column(type="blob", nullable=true)
*/
protected $photo;
and in it's setter don't forget the null default argument value:
public function setPhoto($photo = null)
{
$this->photo = $photo;
}
For the question; seems like you're setting a new Dish object on every edit operation in the action:
$form->setHydrator($hydrator)
->setObject(new Dish)
->setInputFilter($filter);
This is correct when creating new Dish objects. When editing, you have to set an already persisted Dish instance to the form:
// I'm just writing to explain the flow.
// Accessing the object repository in action level is not a good practice.
// Use a DishService for example.
$id = 32; // Grab it from route or elsewhere
$repo = $entityManager->getRepository('Dishes\Entity\Dish');
$existingDish = $repo->findOne((int) $id);
$form->setHydrator($hydrator)
->setObject($existingDish)
->setInputFilter($filter);
I'm assuming this is edit action for an existing Dish.
So, the hydrator will correctly handle both changed and untouched fields on next call since you give an already populated Dish instance via the form.
I also recommend fetching the DishFilter from the InputFilterManager instead of creating it manually in action level:
// $filter = new DishFilter();
$filter = $serviceLocator->get('InputFilterManager')->get(DishFilter::class);
// Exclude the photo on validation:
$filter->setValidationGroup('name', 'description', 'time', 'complexity');
Hope it helps.
I have question about inserting entity into a database. I have two models:
class News {
/**
* #Column(type="string", length=100)
* #var string
*/
protected $title;
/**
* #ManyToOne(targetEntity="User", inversedBy="news")
* #JoinColumn(referencedColumnName="id")
*/
protected $author;
}
class User {
/**
* #Id #GeneratedValue #Column(type="integer")
* #var integer
*/
protected $id;
/**
* #OneToMany(targetEntity="News", mappedBy="author")
*/
protected $news;
public function __construct() {
$this->news = new \Doctrine\Common\Collections\ArrayCollection;
}
}
To add new news I must include both User and News classes (if they're in separate files, for ex. UserModel.php and NewsModel.php) and write a code:
$news = new News()
$news->setTitle('TEST title');
$news->setAuthor($database->find('User', 1));
$database->persist($news);
My question is: Is there any way to insert news without including User class?
You don't need to actually load the User.
Instead, you can use a reference proxy:
<?PHP
$news = new News()
$news->setTitle('TEST title');
$news->setAuthor($em->getReference('User',1));
$em->persist($news);
one other thing you could do (thinking in a more object-oriented kinda way) is add a method called addNews($news) on your user entity:
public function addNews($news) {
// you should check if the news doesn't already exist here first
$this->news->add($news);
$news->setAuthor($this);
}
and add cascade persist to your mapping:
/**
* #OneToMany(targetEntity="News", mappedBy="author", cascade={"persist"})
*/
protected $news;
then fetch your user, add the news, and merge the changes:
$news = new News()
$news->setTitle('TEST title');
$author = $database->find('User', 1);
$author->addNews($news);
//merge changes on author entity directly
$em->merge($author);
I preferr this approach because it gives you the opportunity to do extra checks or controls while adding the news, making for reusable and easy to read code
I have entity "Creative" and Entity "Question"... When I had a ManyToMany relation from Creative to Question, I could easily do $builder->add('questions'), and it would grab all the questions and put them in a multiselect and insert into creative_question. Well I need to have a new field (position) to creative_question, so I had to create a OneToMany / ManyToOne relation. But when I add the field ($builder->add('creativeQuestions')), the multi-select is blank and it seems to be trying to query creative_question to populate it.. which is wrong. I need to populate with Questions and insert those into creative_question.
Anyhow, here's my code:
## Creative.php
[...]
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="Offer", cascade={"persist"})
*/
protected $offer;
/**
* #ORM\OneToMany(targetEntity="CreativeQuestion", mappedBy="creative", cascade={"persist"})
*/
protected $creativeQuestions;
[...]
## CreativeQuestion.php
[...]
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="Creative", cascade={"persist"})
*/
protected $creative;
/**
* #ORM\ManyToOne(targetEntity="Question", cascade={"persist"})
*/
protected $question;
/**
* #ORM\Column(type="integer")
*/
protected $pos;
[...]
## CreativeType.php
[...]
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name')
->add('title')
->add('description')
->add('body')
->add('script')
->add('creativeQuestions') // how do i populate list with "Questions" then insert into creative_question?
->add('active');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'JStout\MainBundle\Entity\Creative'
);
}
[...]
## In My Controller:
/**
* #Extra\Route("/offer/{offerId}/creative", name="admin_offer_creative")
* #Extra\Route("/offer/{offerId}/creative/{creativeId}", name="admin_offer_creative_edit")
* #Extra\Template()
*/
public function creativeAction($offerId = null, $creativeId = null)
{
// Get Offer
$offer = $this->_getObject('Offer', $offerId);
if (null === $offer->getId()) throw new NotFoundHttpException('The page you requested does not exist!');
// Get Creative
$creative = $this->_getObject('Creative', $creativeId);
// Set offer to creative
$creative->setOffer($offer);
// Get form and handler
$form = $this->get('form.factory')->create(new Form\CreativeType(), $creative);
$formHandler = $this->get('form.handler')->create(new Form\CreativeHandler(), $form);
[...]
}
protected function _getObject($entityName, $id = null)
{
// Find object
if (null !== $id) {
if (!$object = $this->get('doctrine')->getEntityManager()->find('ZGOffersMainBundle:' . $entityName, $id)) {
throw new NotFoundHttpException('The page you requested does not exist!');
}
return $object;
}
// Initialize new object
$entityName = 'JStout\MainBundle\Entity\\' . $entityName;
if (class_exists($entityName)) {
return new $entityName();
}
throw new NotFoundHttpException('The page you requested does not exist!');
}
[...]
Again, what I need works when I remove CreativeQuestion and just do Question with a ManyToMany relation:
But, ideally I'd like to have the ability to (with jquery) add questions by selecting from a dropdown box, then drag/drop for positioning of the questions. The jquery positioning is easy, I just don't know how to go about adding the questions how I want to. If I can at least get the multiselect to work, then I can move forward, but I'm kind of stuck right now!
Anyone get this far yet with Symfony2 (beta5)?