Dependencies in forms Symfony2
I'm working on a webapplication in Symfony2. I came to a point in which I need some advice/explanation from some one more advanced in Symfony.
I have a part of my database that is set up as follows:
I have cards that belong to a card attribute set and consists of card values.
I have card attribute sets that have many attributes, a card attribute can belong to many card attribute sets (obviously a many to many relationship).
Then depending on the card attribute the attribute has an attribute value, for example a text has a value_text of type varchar and a boolean has a value_boolean of type boolean.
You can imagine when making a form to create a new card, the form needs to generate input fields depending on the card attribute set it belongs to and depending on the attributes that belong to the attribute set right?
So here's my question; is there a way to dynamically generate input fields in a form depending the entity chosen by the user. I've read about events but I'm not sure that they satisfy my needs.
This is the code for my entities (I removed to Getters and Setters for a more simple view):
Card:
/**
* card
*
* @ORMTable()
* @ORMEntity(repositoryClass="clientsBundleEntitycardRepository")
* @UniqueEntity(
* fields={"cardLabel"},
* message="A card with this label already exists"
* )
*/
class card
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="card_label", type="string", length=999)
*/
private $cardLabel;
/**
* @ORMManyToOne(targetEntity="project", inversedBy="project_cards")
* @ORMJoinColumn(name="project_id", referencedColumnName="id", onDelete = "SET NULL")
*/
protected $card_project;
/**
* @ORMManyToOne(targetEntity="cardAttributeSet", inversedBy="cas_cards")
* @ORMJoinColumn(name="cas_id", referencedColumnName="id")
**/
protected $cardAttrSet;
/**
* @ORMOneToMany(targetEntity="cardAttrValue", mappedBy="card", cascade={"persist"}, orphanRemoval=true)
**/
protected $card_values;
/**
* @ORMManyToMany(targetEntity="user", mappedBy="cards")
*/
private $users;
public function __construct() {
$this->card_values = new ArrayCollection();
$this->users = new ArrayCollection();
}
}
Card Attribute:
/**
* cardAttribute
*
* @ORMTable()
* @ORMEntity(repositoryClass="clientsBundleEntitycardAttributeRepository")
* @UniqueEntity(
* fields={"name"},
* message="An attribute with this name already exists"
* )
*/
class cardAttribute
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORMColumn(name="type", type="string", length=255)
*/
private $type;
}
Card Attribute Set
/**
* cardAttributeSet
*
* @ORMTable()
* @ORMEntity(repositoryClass="clientsBundleEntitycardAttributeSetRepository")
* @UniqueEntity(
* fields={"casLabel"},
* message="An attribute set with this label already exists"
* )
*/
class cardAttributeSet
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var string
*
* @ORMColumn(name="cas_label", type="string", length=255)
*/
private $casLabel;
/**
* @ORMOneToMany(targetEntity="card", mappedBy="cardAttrSet")
*/
private $cas_cards;
/**
* @ORMManyToMany(targetEntity="cardAttribute")
* @ORMJoinTable(name="cas_attribute",
* joinColumns={@ORMJoinColumn(name="cas_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="attribute_id", referencedColumnName="id")}
* )
*/
private $attributes;
public function __construct()
{
$this->cas_cards = new ArrayCollection();
$this->attributes = new ArrayCollection();
}
}
Card Attribute Value
/**
* cardAttrValue
*
* @ORMTable()
* @ORMEntity(repositoryClass="clientsBundleEntitycardAttrValueRepository")
* @UniqueEntity(
* fields={"valueText"}
* )
*/
class cardAttrValue
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="value_text", type="string", length=255, nullable=true)
*/
private $valueText;
/**
* @var string
*
* @ORMColumn(name="value_varchar", type="string", length=255, nullable=true)
*/
private $valueVarchar;
/**
* @var integer
*
* @ORMColumn(name="value_int", type="integer", nullable=true, nullable=true)
*/
private $valueInt;
/**
* @var boolean
*
* @ORMColumn(name="value_boolean", type="boolean", nullable=true, nullable=true)
*/
private $valueBoolean;
/**
* @ORMManyToOne(targetEntity="card", inversedBy="card_values")
* @ORMJoinColumn(name="card_id", referencedColumnName="id")
**/
private $card;
/**
* @ORMManyToOne(targetEntity="cardAttribute")
* @ORMJoinColumn(name="cardAttributes_id", referencedColumnName="id")
**/
private $cardAttribute;
}
Create a form type CardAttributeValueType
for CardAttributeValue
entity, inside this form add fields depending on passed attribute type:
class CardAttributeValueType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
$value = $event->getData();
$form = $event->getForm();
if (!$value) {
return;
}
switch ($value->getCardAttribute()->getType()) {
case 'text':
$form->add('valueVarchar', 'text');
break;
// Same for other attribute types
}
}
}
Then, add collection
field type for card_values
inside CardType
form type and pass CardAttributeValueType
as collection item type.
In the Card
entity edit getCardValues()
method so it will return every attribute from CardAttributeSet
, not only ones for which value entities exist.
UPDATE
public function getCardValues()
{
$collection = new ArrayCollection();
if (!$this->cardAttrSet) {
return $collection;
}
// Add existing values
foreach ($this->card_values as $value) {
$collection[$value->getCardAttribute()->getId()] = $value;
}
// Get all attributes from the set and create values for missing attributes
foreach ($this->cardAttrSet->getAttributes() as $attr) {
if (!isset($collection[$attr->getId()])) {
$value = new cardAttrValue();
$value->setCardAttribute($attr);
$collection[$attr->getId()] = $value;
}
}
return $collection;
}
链接地址: http://www.djcxy.com/p/27108.html
上一篇: sql server和mysql之间的链接服务器上的分布式事务
下一篇: 形式Symfony2中的依赖关系