Reputation: 70
I've got the following tables with relationships:
UserAddress
table (Holds basic information for the address)UserAddressFields
table (Holds the value for each field associated to an address)UserAddressFieldsTranslation
table (Holds the field ids)UserStates
table (Holds the values of all the states in the US and the abbreviations)UserAddress has 2 foreign keys:
Profile_id
-> useraccounts.id
state_id
-> userstates.id
UserAddressFields has one foreign key:
address_id
-> useraddress.id
I've created entities for all 4 of these tables (Getters and Setters have been removed to save space, generated via console):
UserAddress.php
<?php
namespace SCWDesignsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use SCWDesignsBundle\Entity\UserStates;
use UsersBundle\Entity\User;
/**
* @ORM\Entity(repositoryClass="SCWDesignsBundle\Entity\Repository\
UserAddressesRepository")
* @ORM\Table(name="user_addresses")
*/
class UserAddresses {
/**
* @ORM\ID
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="UsersBundle\Entity\User")
* @ORM\JoinColumn(name="profile_id", referencedColumnName="id")
*/
protected $profile_id;
/**
* @ORM\ManyToOne(targetEntity="UserStates")
* @ORM\JoinColumn(name="state_id", referencedColumnName="id")
*/
protected $state_id;
/**
* @ORM\Column(type="boolean")
*/
protected $isBilling;
/**
* @ORM\Column(type="boolean")
*/
protected $isShipping;
/**
* @ORM\Column(type="string", columnDefinition="CHAR(1)")
*/
protected $addressType;
UserAddressFieldsTranslation.php
<?php
namespace SCWDesignsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use SCWDesignsBundle\Entity\UserAddressFields;
use UsersBundle\Entity\User;
/**
* @ORM\Entity(repositoryClass="SCWDesignsBundle\Entity\Repository\UserAddressFieldsTranslationRepository")
* @ORM\Table(name="user_address_fields_translation")
*/
class UserAddressFieldsTranslation {
/**
* @ORM\ID
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", columnDefinition="VARCHAR(255) NOT NULL")
*/
protected $name;
UserStates.php
<?php
namespace SCWDesignsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="SCWDesignsBundle\Entity\Repository\StatesRepository")
* @ORM\Table(name="user_states")
*/
class UserStates {
/**
* @ORM\ID
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", columnDefinition="CHAR(2)")
*/
protected $country_code;
/**
* @ORM\Column(type="string", columnDefinition="VARCHAR(64)")
*/
protected $state;
I have 2 form types, UserAddressFormType and UserAddressFieldsFormType.
UserAddressFormType
<?php
namespace SCWDesignsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserAddressFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('profile_id', null, array('label' => 'Profile ID'))
->add('state_id', null, array('label' => 'State ID'))
->add('isBilling', null, array('label' => 'Billing Address'))
->add('isShipping', null, array('label' => 'Shipping Address'))
->add('addressType', null, array('label' => 'Address Type'))
->add('addressFields', new UserAddressFieldsFormType());
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'SCWDesignsBundle\Entity\UserAddresses'
));
}
public function getName() {
return 'UserAddressFormType';
}
}
UserAddressFieldsFormType.php
<?php
namespace SCWDesignsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserAddressFieldsFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('address_id', null, array('label' => 'Address Field'))
->add('address_field_id', null, array('label' => 'Address Field ID'))
->add('value', null, array('label' => 'Value'));
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'SCWDesignsBundle\Entity\UserAddressFields'
));
}
public function getName() {
return 'UserAddressFieldsFormType';
}
}
In the controller is your basic form call. ProfileController.php
<?php
namespace SCWDesignsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use SCWDesignsBundle\Entity\UserAddresses;
use SCWDesignsBundle\Form\Type\UserAddressFormType;
use SCWDesignsBundle\Form\Type\UserAddressFieldsFormType;
class ProfileController extends Controller {
public function editAddressAction() {
$address = new UserAddresses();
$form_address = $this->createForm(new UserAddressFormType(), $address);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
// Perform some action.
return $this->redirect($this->generateUrl('fos_user_profile_show'));
}
return $this->render('SCWDesignsBundle:Profile:edit.html.twig', array(
'active_page' => 'profile',
'form_address' => $form_address->createView(),
));
}
}
Everywhere I've read this is the correct means of adding another entity to your form builder but everytime I do this, or any other variation I've ran across I get the following error
Neither the property "addressFields" nor one of the methods
"getAddressFields()", "isAddressFields()", "hasAddressFields()", "__get()" exist
and have public access in class "SCWDesignsBundle\Entity\UserAddresses".
Upvotes: 2
Views: 5093
Reputation: 1192
So in the first form you set defaults to this:
$resolver->setDefaults(array(
'data_class' => 'SCWDesignsBundle\Entity\UserAddresses'
));
So any field that you add is expected to correspond to a field in the entity. So I think geoB was right. You need to add the mapping that you want, if you want it on the class or make the form independent of the entity (so remove the 'data_class' option from the defaults). To add the mapping consider the following code for a OneToMany relation:
/**
* @ORM\OneToMany(targetEntity="UserAddressFields", mappedBy="user_address", cascade={"persist", "remove"})
*/
protected $addressFields;
public function addAddressField($addressField) {
$this->addressFields[] = $addressField;
}
public function removeAddressField() {...}
public function getAddressFields() {...}
public function __construct()
{
$this->addressFields = new ArrayCollection();
}
And if you combine this with Nawfal's answer:
$builder->add('addressFields', 'collection', array('type' => new UserAddressFieldsFormType()));
It should work!
Upvotes: 2
Reputation: 2263
You need to read closely the documentation on how to do this :
http://symfony.com/doc/current/cookbook/form/form_collections.html
Your code doesnt make sens :
->add('addressFields', new UserAddressFieldsFormType());
this is not quiet it.
$builder->add('addressFields', 'collection', array('type' => new UserAddressFieldsFormType()));
Upvotes: 2