First of all thanks for looking into this.
I'm building a form to add categories to the db table and these categories can have a parent category (self referring). there is a dropdown to select the parent category. I'm using ZF2 and Doctrine 2 to build this form. Everything works fine but the only issue i have is that on the edit page, the parent category dropdown it shows the current category as well. I'd like to know how to exclude it from the dropdown. I'm posting some of my codes below. To keep it simple i removed some unrelated lines and shorten some names.
I defined the self referring relationship on the model
//Category model
use Doctrine\ORM\Mapping as ORM;
Class Category {
/**
*
* #var integer
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
.....
.....
/**
* Parent category if available
* #var self
* #ORM\OneToOne(targetEntity="Category")
* #ORM\JoinColumn(name="parent", referencedColumnName="id", nullable=true)
*/
protected $parent;
On the form I have a dropdown listing all the categories
$parent = new \DoctrineModule\Form\Element\ObjectSelect('parent');
$parent->setOptions(array(
'label' => 'Parent Category',
'class' => '',
'object_manager' => $this->em,
'target_class' => \Data\Entity\Category::class,
'property' => 'name',
'display_empty_item' => true,
'empty_item_label' => '- Select Category -',
'required' => false
))->setAttributes(array(
'class' => 'form-control'
));
On the edit controller i load the form and and bind it to the db entry
public function editAction()
{
//get id from url
$id = $this->params()->fromRoute('id', 0);
$request = $this->getRequest();
//load selected category from database
$category = $this->em->getRepository(\Data\Entity\Category::class)->find($id);
//create form
$form = new Form\Category($this->em);
//bind selected category to form
$form->bind($category);
......
}
Thanks.
You need to pass the category id of the category being edited to the form and set object selects search params to pass the id to the entity repository. You will then need to create a search query in the repository to exclude the category id from being returned in the search results.
You can pass the category id to the form with a simple setter.
protected $categoryId;
public function setCategoryId($categoryId)
{
$this->categoryId = $categoryId;
}
In your form you will need something like
$parent->setOptions(array(
'label' => 'Parent Category',
'class' => '',
'object_manager' => $this->em,
'target_class' => \Data\Entity\Category::class,
'property' => 'name',
'is_method' => true,
'find_method' => array(
'name' => 'findCategories',
'params' => array(
'searchParams' => array('id' => $this->categoryId),
),
),
'display_empty_item' => true,
'empty_item_label' => '- Select Category -',
'required' => false
))->setAttributes(array(
'class' => 'form-control'
));
and in your categories repository
public function findCategories($searchParams)
{
$builder = $this->getEntityManager()->createQueryBuilder();
$builder->select('c')
->from(\Data\Entity\Category::class, 'c')
->where('c.id != ?1')
->setParameter(1, $searchParams['id'])
->orderBy('c.category', 'ASC');
return $builder->getQuery()->getResult(Query::HYDRATE_OBJECT);
}
note the orderBy is optional.
I hope this makes sense.
Related
I'm really confused about my Form Filter.
My Test-Project contains 2 Models.
class Category extends AbstractEntity
{
use Nameable; // just property name and getter and setter
/**
* #var boolean
* #ORM\Column(name="issue", type="boolean")
*/
private $issue;
/**
* #var Collection|ArrayCollection|Entry[]
*
* #ORM\OneToMany(targetEntity="CashJournal\Model\Entry", mappedBy="category", fetch="EAGER", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $entries;
}
the entry
class Entry extends AbstractEntity
{
use Nameable;
/**
* #var null|float
*
* #ORM\Column(name="amount", type="decimal")
*/
private $amount;
/**
* #var null|Category
*
* #ORM\ManyToOne(targetEntity="CashJournal\Model\Category", inversedBy="entries", fetch="EAGER")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false)
*/
protected $category;
/**
* #var null|DateTime
*
* #ORM\Column(name="date_of_entry", type="datetime")
*/
private $dateOfEntry;
}
And if someone needed the AbstractEntity
abstract class AbstractEntity implements EntityInterface
{
/**
* #var int
* #ORM\Id
* #ORM\Column(name="id", type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
}
Every Category can have many Entries. I'm using Doctrine for this relation. And this works fine.
I have a Form based on this FieldSet:
$this->add([
'name' => 'id',
'type' => Hidden::class
]);
$this->add([
'name' => 'name',
'type' => Text::class,
'options' => [
'label' => 'Name'
]
]);
$this->add([
'name' => 'amount',
'type' => Number::class,
'options' => [
'label' => 'Summe'
]
]);
$this->add([
'name' => 'date_of_entry',
'type' => Date::class,
'options' => [
'label' => 'Datum'
]
]);
$this->add([
'name' => 'category',
'type' => ObjectSelect::class,
'options' => [
'target_class' => Category::class,
]
]);
So my Form displays a dropdown with my categories. Yeah fine.
To load the Category for my Entry Entity i use a filter.
$this->add([
'name' => 'category',
'required' => true,
'filters' => [
[
'name' => Callback::class,
'options' => [
'callback' => [$this, 'loadCategory']
]
]
]
]);
And the callback:
public function loadCategory(string $categoryId)
{
return $this->mapper->find($categoryId);
}
The mapper loads the category fine. great. But the form is invalid because:
Object of class CashJournal\Model\Category could not be converted to int
Ok, so i'm removing the Filter, but now it failed to set the attributes to the Entry Entity, because the setter needs a Category. The Form error says:
The input is not a valid step
In Symfony i can create a ParamConverter, which converts the category_id to an valid Category Entity.
Question
How i can use the filter as my ParamConver?
Update
Also when i cast the category_id to int, i will get the error from the form.
Update 2
I changed my FieldSet to:
class EntryFieldSet extends Fieldset implements ObjectManagerAwareInterface
{
use ObjectManagerTrait;
/**
* {#inheritDoc}
*/
public function init()
{
$this->add([
'name' => 'id',
'type' => Hidden::class
]);
$this->add([
'name' => 'name',
'type' => Text::class,
'options' => [
'label' => 'Name'
]
]);
$this->add([
'name' => 'amount',
'type' => Number::class,
'options' => [
'label' => 'Summe'
]
]);
$this->add([
'name' => 'date_of_entry',
'type' => Date::class,
'options' => [
'label' => 'Datum'
]
]);
$this->add([
'name' => 'category',
'required' => false,
'type' => ObjectSelect::class,
'options' => [
'target_class' => Category::class,
'object_manager' => $this->getObjectManager(),
'property' => 'id',
'display_empty_item' => true,
'empty_item_label' => '---',
'label_generator' => function ($targetEntity) {
return $targetEntity->getName();
},
]
]);
parent::init();
}
}
But this will be quit with the error message:
Entry::setDateOfEntry() must be an instance of DateTime, string given
Have you checked the documentation for ObjectSelect? You appear to be missing a few options, namely which hydrator (EntityManager) and identifying property (id) to use. Have a look here.
Example:
$this->add([
'type' => ObjectSelect::class,
'name' => 'category', // Name of property, 'category' in your question
'options' => [
'object_manager' => $this->getObjectManager(), // Make sure you provided the EntityManager to this Fieldset/Form
'target_class' => Category::class, // Entity to target
'property' => 'id', // Identifying property
],
]);
To validate selected Element, add in your InputFilter:
$this->add([
'name' => 'category',
'required' => true,
]);
No more is needed for the InputFilter. A Category already exist and as such has been validated before. So, you should just be able to select it.
You'd only need additional filters/validators if you have special requirements, for example: "A Category may only be used once in Entries", making it so that you need to use a NoObjectExists validator. But that does not seem to be the case here.
UPDATE BASED ON COMMENTS & PAST QUESTIONS
I think you're over complicating a lot of things in what you're trying to do. It seems you want to simply populate a Form before you load it client-side. On receiving a POST (from client) you wish to put the received data in the Form, validate it and store it. Correct?
Based on that, please find a complete controller for User that I have in one of my projects. Hope you find it helpful. Providing it because updates are veering away from your original question and this might help you out.
I've removed some additional checking and error throwing, but otherwise is in complete working fashion.
(Please note that I'm using my own abstract controller, make sure to replace it with your own and/or recreate and match requirements)
I've also placed additional comments throughout this code to help you out
<?php
namespace User\Controller\User;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\ORMException;
use Exception;
use Keet\Mvc\Controller\AbstractDoctrineActionController;
use User\Entity\User;
use User\Form\UserForm;
use Zend\Http\Request;
use Zend\Http\Response;
class EditController extends AbstractDoctrineActionController
{
/**
* #var UserForm
*/
protected $userEditForm; // Provide this
public function __construct(ObjectManager $objectManager, UserForm $userEditForm)
{
parent::__construct($objectManager); // Require this in this class or your own abstract class
$this->setUserEditForm($userEditForm);
}
/**
* #return array|Response
* #throws ORMException|Exception
*/
public function editAction()
{
$id = $this->params()->fromRoute('id', null);
// check if id set -> else error/redirect
/** #var User $entity */
$entity = $this->getObjectManager()->getRepository(User::class)->find($id);
// check if entity -> else error/redirect
/** #var UserForm $form */
$form = $this->getUserEditForm(); // GET THE FORM
$form->bind($entity); // Bind the Entity (object) on the Form
// Only go into the belof if() on POST, else return Form. Above the data is set on the Form, so good to go (pre-filled with existing data)
/** #var Request $request */
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost()); // Set received POST data on Form
if ($form->isValid()) { // Validates Form. This also updates the Entity (object) with the received POST data
/** #var User $user */
$user = $form->getObject(); // Gets updated Entity (User object)
$this->getObjectManager()->persist($user); // Persist it
try {
$this->getObjectManager()->flush(); // Store in DB
} catch (Exception $e) {
throw new Exception('Could not save. Error was thrown, details: ', $e->getMessage());
}
return $this->redirectToRoute('users/view', ['id' => $user->getId()]);
}
}
// Returns the Form with bound Entity (object).
// Print magically in view with `<?= $this->form($form) ?>` (prints whole Form!!!)
return [
'form' => $form,
];
}
/**
* #return UserForm
*/
public function getUserEditForm() : UserForm
{
return $this->userEditForm;
}
/**
* #param UserForm $userEditForm
*
* #return EditController
*/
public function setUserEditForm(UserForm $userEditForm) : EditController
{
$this->userEditForm = $userEditForm;
return $this;
}
}
Hope that helps...
I currently have a working form in Symfony where I have a list of companies with checkboxes next to each company name. This is so you can check off which company is assigned to each user. The checkbox currently shows the accountID but it would also be helpful to have the entity field 'name' as well. Can you build a property with two entity fields? Here is my form in my controller:
->add('companies', 'entity', array(
'label' => 'Company',
'class' => 'Default\Bundle\Entity\Customer',
'property' => 'accountId', //This puts the company id next to the check box
'multiple' => true,
'expanded' => true,
'query_builder' => function ($repository)
{
return $repository->createQueryBuilder('c')->orderBy('c.accountId', 'ASC');
},))
->add('Save', 'submit')
->getForm();
This is what I am trying to do:
->add('companies', 'entity', array(
'label' => 'Company',
'class' => 'Default\Bundle\Entity\Customer',
'property' => 'accountId' + 'name', // I want to combine my entity fields here
'multiple' => true,
'expanded' => true,
'query_builder' => function ($repository)
here is the entity just for reference
class Customer
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #Assert\NotBlank(message="Please enter a Customer ID.")
* #Assert\Length(max="32")
* #ORM\Column(type="string", length=32)
* #var string
*/
protected $accountId;
/**
* #Assert\NotBlank(message="Please enter a company name.")
* #Assert\Length(max="60")
* #ORM\Column(type="string", length=60)
* #var string
*/
protected $name;
And one last time... I want to go from this:
To this:
Create a simple getter and use that as the property, eg:
public function getNamePlusAccountId()
{
return $this->name." (".$this->accountId.")";
}
and use 'property' => 'namePlusAccountId' in your form.
If you only need to change the label but would like to keep the form field value then http://symfony.com/doc/current/reference/forms/types/entity.html#choice-label probably what are you looking for
How to implement a group of elements in the drop-down list ObjectSelect 'optgroup_identifier'
Form\CategoryForm.php
$this->add([
'type' => ObjectSelect::class,
'name' => 'category',
'options' => [
'label' => 'Категория',
'object_manager' => $this->getObjectManager(),
'target_class' => Category::class,
'property' => 'name',
'optgroup_identifier' => '???',
'optgroup_default' => 'Главная',
'empty_option' => '== Категория ==',
'is_method' => true,
'find_method' => [
'name' => 'findAllChildCategories',
'params' => [
],
],
],
]);
Category Table is relevant Self-referencing
Entity\Category.php
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\OneToMany(targetEntity="Application\Entity\Category", mappedBy="parent", cascade={"remove"})
*/
private $children;
/**
* #var \Application\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Application\Entity\Category", inversedBy="children")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="parent", referencedColumnName="id", nullable=true)
* })
*/
private $parent;
Group name must be the parent category
$category->getParent()->getName()
Thankfully for this case, Doctrine does not do any queries to get groupings; it does it internally. optgroup_identifier is just name of a getter it uses to get group names, therefore that getter can return anything you want.
In Entity\Category, add a method dedicated to returning parent name of a category. Ensure it does not coincide with any fields so Doctrine does not return a whole object proxy into a form. For example:
public function getParentName() {
if(!$this->parent) return '';
return $this->parent->getName();
}
Since root categories will not have a parent, $this->parent will be null. Look out for that case to avoid script crashing and return empty string as a designation for it.
Then, put this getter name in optgroup_identifier of the form. Final result will be as in screenshot with sample data.
I work with Symfony2 and sonata admin. I have an entity (News) which own a subcategory. Each subcategory own one category, and each category own one Affaire.
In the add page for a news, I have a subcategory list, to choose my subcategory to link to my news. Each item of my select is formated like this :
<li> subcategory (category'affaire) > categoryName </li>.
I would like to sort the fields by the affaire (ASC).
Here is my formfield definition :
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('subCategory', 'sonata_type_model', array("label" => "Catégorie/Sous Catégorie", "btn_add" => false));
}
One News own one Subcategory
One Subcategory own one Category
One category own one Affaire.
I tried to add something like :
->add('subCategory', 'sonata_type_model',
array("label" => "Sub Category",
"btn_add" => false
),
array(
'sortable' => 'ordering',
'label' => 'subcategory.category.affaire.code',
))
But nothing changes. Any Ideas ?
Category entity :
class NewsCategory
{
/**
* #var \My\Custom\Foo\Entity\Affaire
*
* #ORM\ManyToOne(targetEntity="\My\Custom\Foo\Entity\Affaire")
* #ORM\JoinColumn(name="affaire_code", referencedColumnName="code")
*/
private $affaire;
--
Subcategory entity :
class NewsSubCategory
{
/**
* #var \My\Custom\Foo\Entity\NewsCategory
*
* #ORM\ManyToOne(targetEntity="\My\Custom\Foo\Entity\NewsCategory")
* #ORM\JoinColumn(name="category_ref", referencedColumnName="id")
*
*/
private $category;
--
News entity :
class News
{
/**
* #var \My\Custom\Foo\Entity\NewsSubCategory
*
* #ORM\ManyToOne(targetEntity="\My\Custom\Foo\Entity\NewsSubCategory")
* #ORM\JoinColumn(name="sub_category", referencedColumnName="id")
*/
private $subCategory;
[EDIT] :
I tried
->add('subCategory', 'sonata_type_model',array("label" => "Catégorie/Sous Catégorie","btn_add" => false), array("sortable" => "ordering"))
And I doesn't make an error but nothing happend. I don't understand where i could add the option (orderBy => 'Affaire') , or if it has to be done that way ...
[EDIT2] :
I even tried :
->add('subCategory.category.affaire', null,
array("label" => "Catégorie/Sous Catégorie",
"btn_add" => false
))
I don't know how you can do that with a sonata_type_model but you can change the type of your field to null or entity (null set the default type) and add a query_builder option to adapt the query used :
->add('subcategory', null, array(
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('sc')
->leftjoin('sc.category', 'c')
->orderBy('c.affaire', 'ASC');
}
))
If instead of null you choose entity, you have to add the class also :
->add('subcategory', 'entity', array(
'class' => 'MyCustomFooBundle:Subcategory',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('sc')
->leftjoin('sc.category', 'c')
->orderBy('c.affaire', 'ASC');
}
))
It seems that 'Sortable' had to be in the third parameter.
->add('subCategory', 'sonata_type_model',
array("label" => "Sub Category",
"btn_add" => false
'sortable' => 'ordering',
))
And after that, you have two options : Trying to show the Affaire
->add('subCategory.categorie.affaire', 'sonata_type_model',
array("label" => "Affaire",
"btn_add" => false
'sortable' => 'ordering',
))
Or our Entity can implement "Collections Sortable" . Try to have a look to : https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sortable.md
(Sorry, I'm not fluent in english)
I have a zf2 application that works with doctrine.
I have the following entity:
class Role
{
/**
* #var int
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
* #ORM\Column(type="string", length=255, unique=true, nullable=true)
*/
protected $name;
/**
* #var ArrayCollection
* #ORM\OneToMany(targetEntity="YrmUser\Entity\Role", mappedBy="parent")
*/
protected $children;
/**
* #var Role
* #ORM\ManyToOne(targetEntity="YrmUser\Entity\Role", inversedBy="children", cascade={"persist"})
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
}
for this entity i have a form:
class RoleForm extends Form
{
/**
* [init description]
*
* #return void
*/
public function init()
{
$this->setHydrator(
new DoctrineHydrator($this->objectManager, 'YrmUser\Entity\Role')
)->setObject(new Role());
$this->setAttribute('method', 'post');
$this->add(
array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'placeholder' =>'Name',
),
'options' => array(
'label' => 'Name',
),
)
);
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'parent',
'attributes' => array(
'id' => 'parent_id',
),
'options' => array(
'label' => 'Parent',
'object_manager' => $this->objectManager,
'property' => 'name',
'is_method' => true,
'empty_option' => '-- none --',
'target_class' => 'YrmUser\Entity\Role',
'is_method' => true,
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array('parent' => null),
),
),
),
)
);
}
}
The hydration for the select in the form works as it only shows other roles that don't have a parent.
But when editing a existing entity it shows itself in the select so i can select itself as its parent.
I figured if i would have the id of current entity inside the form i can create a custom repo with a method that retrieves all roles without a parent and does not have the current entity id.
But i cant figure out how to get the id of the currently edited entity from inside the form.
Any help is appreciated.
Cheers,
Yrm
You can fetch the bound entity within the form using $this->getObject().
You have actually already set this with setObject(new Role());. Unfortunately this means that it was not loaded via Doctine and you will have the same issue, no $id to work with.
Therefore you will need to add the 'parent role' options (value_options) after you have bound the role loaded via doctrine.
From within the controller, I normally request the 'edit' form from a service class and pass in the entity instance or id that is being edited. Once set you can then modify existing form elements before passing it back to the controller.
// Controller
class RoleController
{
public function editAction()
{
$id = $this->params('id'); // assumed id passed as param
$service = $this->getRoleService();
$form = $service->getRoleEditForm($id); // Pass the id into the getter
// rest of the controller...
}
}
By passing in the $id when you fetch the form you can then, within a service, modify the form elements for that specific role.
class RoleService implements ObjectManagerAwareInterface, ServiceLocatorAwareInterface
{
protected function loadParentRolesWithoutThisRole(Role $role);
public function getRoleEditForm($id)
{
$form = $this->getServiceLocator()->get('Role\Form\RoleEditForm');
if ($id) {
$role = $this->getObjectManager()->find('Role', $id);
$form->bind($role); // calls $form->setObject() internally
// Now the correct entity is attached to the form
// Load the roles excluding the current
$roles = $this->loadParentRolesWithoutThisRole($role);
// Find the parent select element and set the options
$form->get('parent')->setValueOptions($roles);
}
// Pass form back to the controller
return $form;
}
}
By loading the options after the form has initialized you do not need the current DoctrineModule\Form\Element\ObjectSelect. A normal Select element that has no default value_options defined should be fine.