0

I'm trying to build an Entity with a "xml" text field that must insert in the database the concatenation of other fields from the same entity. It looks like this (without annotations) :

class Category
{

private $id;

private $title;

private $description;

public function getId()
{
    return $this->id;
}

public function setTitle($title)
{
    $this->title = $title;

    return $this;
}

public function getTitle()
{
    return $this->title;
}

public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

public function getDescription()
{
    return $this->description;
}

public function setXml($xml)
{
    $xml = $this->id;
    $xml += $this->title;
    $xml += $this->description;
    return $xml;
}

public function getXml()
{
    return $this->xml;
}

I try to insert those values using a simple form like this :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title')
        ->add('description')
        ->add('xml', 'hidden')
    ;
}

When I try to submit the form, I've got an error saying my xml value is null. "An exception occurred while executing 'INSERT INTO Category (title, description, xml) VALUES (?, ?, ?, ?)' with params ["Title 1", "Desc 1", null]:"

What am I doing wrong ?

1 Answers1

0

Do not add XML field to the form, as it will be se to empty or null value.

What you can do is modify the XML field when you modify the title or description fields, as I see it you will never set a value for the XML field manually right? Then instead of a setXML method, you can have an updateXML method, which is called last in the setTitle and setDescription methods.

You can also take a look to Doctrine lifecycle methods, which let's you do actions before saving the entity to the database, this is a bit more complex.

I hope any of this helps.

jmoreno
  • 612
  • 10
  • 23