I have a set of entities in Doctrine which depends on each other to build certain data, and I need to create a form which uses data from two of those entities.
I have a Magazine entity, an Issue entity and a Chapter entity. The Magazine (Mag1, Mag2) has it's name, the Issue, that belongs to only one Magazine, has it's 'number' (Mag1->Issue 1, Mag1->Issue 2, Mag3-> Issue 1, Mag2 -> Issue 'Summer'). The Chapter have to belong to just one Issue, but when creating the form, to build the Issue selector I need to concatenate properties from two entities:
class ChapterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('number')
->add('issue', 'entity', array(
'class' => 'Bundle:Issue',
'property' => 'magazine.name'
))
;
}
...
What I need to do is concatenate in the 'property' something like 'magazine.name'+'number' (where 'number' is the Issue which will be added number. Trying to concatenate with the . like in php strings doesn't work since they aren't strings so I don't know what I have to do or if It's possible to do It this way.
In the Issue create a new getter that does the concat. Given that you have properly setup the ManyToOne relationship, the getter should be something like:
public function getMagazzineAndIssue() {
return $this->magazine->getName() . $this->number;
}
in the form, use this new method as the property:
$builder
->add('name')
->add('number')
->add('issue', 'entity', array(
'class' => 'Bundle:Issue',
'property' => 'magazineAndIssue'
))
Related
Currently, I develop an app with PHP Symfony Framework. I've got a problem with Form Builder (I think).
I have two entities. Question and Choice.
Question and Choice are OneToMany Relationship Entity. One Question has many Choice.
Another two entities, Video and Category, the relationship is just the same with Question and Choice.
I create scaffolding crud for those entity with php bin/console make:crud.
Then I add the relationship symfony like in this guide from Symfony.
The logic is, I must select the Category first to create new Video. Same with the Choice, I must select the Question first to create new Choice data.
My problem appear when I open the Choice Create Form [/choice/new]. It says
Variable "expanded" does not exist.
Then the error details show on this lines
return $this->render('choice/new.html.twig', [
'choice' => $choice,
'form' => $form->createView(), // The highlighted error appear on this line
]);
But, It just happen in the Question-Choice, My Category-Video relationship is just fine. I tried to make Question-Choice as same as Category-Video (I changed the name of the entity for sure), I triple check it, but the error on Choice Create Form still occur.
This is my App\Form\ChoiceType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content')
->add('letter')
->add('image')
->add('question', EntityType::class, [
'class' => Question::class,
'choice_label' => 'content'
])
;
}
Notice the add('question')
and this is my App\Form\VideoType buildForm method
<?php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('url', FileType::class, [
'label' => 'Video File',
'required' => false,
])
->add('thumbnail', FileType::class, [
'required' => false,
])
->add('description')
->add('category', EntityType::class, [
'class' => Category::class,
'choice_label' => 'name'
])
;
}
Notice the add('category')
So, anyone know what is happening?
I renamed App\Form\ChoiceType to App\Form\TheChoiceType, make some adjusment for class name changing on the controller. Everything is work!
I don't believe this! The solution is to rename the form type.
I could not really find my specific case on the internet and therefore decided on writing my own question.
I have a form to create a Member object. This member object has a reference to an application object. Both are saved in 2 different databases.
Creating a Member object is no issue only when I edit and fill out the form, do I encounter an error ->
Entity of type "Application"
passed to the choice field must be managed. Maybe you forget to
persist it in the entity manager
Here is my Form Code
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('membberName', null, array('label' => false))
->add('memberDescription', TextareaType::class, array('label' => false))
->add('memberVisible', null, array('label' => false))
->add('memberApp', EntityType::class, [
'label' => false,
'class' => Application::class,
'choice_label' => function (Application $application) {
return sprintf('(%d) %s', $application->getAppId(), $application->getAppurlUrl());
},
'choices' => $this->applicationRepository->getAll(),
])
->add('Save', SubmitType::class, [
'attr' => ['class' => 'create-button']
]);
}
I found a lot of cases about this issue but none of them could help me.
I only encounter this issue if I load an entity that is related to another one outside their own database.
To summarize: calling the create view page and pressing on submit works.
Calling the edit view causes the above mentioned issue.
Do I have to define or configure anything so my form can load correctly?
Try setting up custom em for your entity form field memberApp in options to your second database entity manager.
like described in docs: https://symfony.com/doc/current/reference/forms/types/entity.html#em
em type: string | Doctrine\Common\Persistence\ObjectManager default:
the default entity manager
If specified, this entity manager will be used to load the choices
instead of the default entity manager.
Another way, probably would be to set ['mapped' => false] for this field and handling flush manually with correct database's em in controller or service
Note: I don't want to create services. It's just a simple application. In flat php this would be a piece of cake, but for some reason it's inordinately difficult in symfony/doctrine... :( The EntityManager is available in controllers, but for some reason it's not available in other classes. I don't care about MVP and clean code and all that. I just want it to work. I don't understand why it has to be such a pain in the butt.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$current_year=getdate()['year'];
$em = new \Doctrine\ORM\EntityManager;
$approving_teachers = $em->createQuery('select u from Teacher t where IsAuthorized = 1')->getResult();
$builder
->add('startDatetime', 'datetime', array('years' => range($current_year-1, $current_year), 'time_widget' => 'text'))
->add('hoursServed', 'number', array('constraints'=> array(new \Symfony\Component\Validator\Constraints\Range(array('min'=>0, 'max'=>500)))))
->add('activity')
->add('student')
->add('approvingTeacher', 'entity', array(
'class' => 'Teacher',
'choices' => $approving_teachers
))
;
}
Why don't you use an 'entity field type': http://symfony.com/doc/current/reference/forms/types/entity.html?
With entity field type you can use EntityRepository to retrieve anything you need.
Add query_builder to your approvingTeacher field. And use EntityRepository.
use Doctrine\ORM\EntityRepository;
...
->add('approvingTeacher', 'entity', array(
...
'query_builder' => function(EntityRepository $er) {
return something...
}
))
Also, check the docs: http://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities
I've got 3 entities:
- overriden User
- Address
- Company
User Entity got entity fields Address and Company.
Now I'm trying to build User Registration Form using as well fields from Address and Company entities. The problem is - I have no idea how to proceed with this.
I was trying to do sth like this:
$this->addCommonFields($builder);
$builder
->add('company', 'entity', array(
'label' => 'street',
'class' => 'AcmePsoBundle:Company',
'property' => 'street',
));
but then I receive dropdown list and it supposed to be textfield.
#Edit: Should I use DataTransformer?
You should create new form type for your AcmePsoBundle:Company entity, and use it as:
$builder->add('company', new YourCompanyFormType());
YourCompanyFormType.php example:
class YourCompanyFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'street',
'text'
);
}
And don't forget to set empty Company to new user entity $user->setCompany(new Company()); before form.
I've got form class where I'm defining some inputs, something liek this:
class User extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('mail', 'text', array('label' => 'Mail'))
->add('password', 'text', array('label' => 'Hasło'))
->add('description', 'textarea', array('label' => 'Opis'));
}
}
I want to change mail and password input type to readonly and set them some values.
Now, I use form this way:
$form = $this->createForm(new User($this->get('database_connection')));
I tried many things, but Symfony2 has so many Form classes and I've lost in that.
I want to simply add some atributes to existing, added inputs.
I don't use Doctrine2 ORM, I use Doctrine DBAL, if it does matter.
Thanks in advance.
your can set default value with 'data' parameter and readonly with attr parameter
$builder
->add('mail', 'text', array('label' => 'Mail', 'data' => 'Default value'
attr => array('readonly=>'readonly')));