[FLOW3-general] Custom Type Converter mandatory for automagical object creation of aggregates?

Ivan Ruiz Gallego ivan at loglan.net
Wed Sep 28 15:46:53 CEST 2011


Hi Christian,

Within the Foo class, the association with bars is declared as follows:

/**
   * The bars
   * @var 
\Doctrine\Common\Collections\ArrayCollection<\Acme\MyPackage\Domain\Model\Bar>
   * @OneToMany(mappedBy="foo",cascade={"all"})
   */
protected $bars;

Within the Bar class, I declare it as:

/**
   * Foo to which this bar is associated to
   * @var \Acme\MyPackage\Domain\Model\Foo
   * @ManyToOne(inversedBy="bars")
   */
protected $foo;

The code of the createAction looks like this:

/**
   *  Add a new foo
   *
   * @param \Acme\MyPackage\Domain\Model\Foo $foo The new foo the be added
   * @return void
   */
public function 
createAction(\Liberty\Beziehungseroeffnung\Domain\Model\Foo $foo) {
    $this->fooRepository->add($foo);
    $this->flashMessageContainer->add('Foo added');
    $this->redirect('index');
}

The create action initialization looks like this:

/**
   * Initialize create action
   *
   * @return void
   */
public function initializeCreateAction() {
 
$this->arguments['foo']->getPropertyMappingConfiguration()->allowCreationForSubProperty('bars');
 
$this->arguments['foo']->getPropertyMappingConfiguration()->allowCreationForSubProperty('bars.0');
 
$this->arguments['foo']->getPropertyMappingConfiguration()->allowCreationForSubProperty('bars.1');
 
$this->arguments['foo']->getPropertyMappingConfiguration()->allowCreationForSubProperty('bars.2');
}

Additionally, I have plain getters and setters for $bars (class Foo) and 
$foo (class Bar) respectively.

With this setup the foo and the bars get created, but the association 
doesn't get set. I've checked that at the very beginning of the create 
action. There the property $foo of all $foo->getBars() is NULL.

If I tell the bars which foo is their parent, for example in the setter 
method for bars within the class Foo, then the association gets established:

public function setBars(\Doctrine\Common\Collections\ArrayCollection 
$bars) {
     foreach ($bars as $bar) {
         $bar->setFoo($this);
     }
     $this->bars = $bars;
}

I just thought that FLOW3 would do that for me. Maybe I was expecting 
too much?

Thanks,
Ivan.



On 28.09.2011 09:13, "Christian Müller (Kitsunet)" wrote:
> Looks all good to me... Can I see the PHP code? Normally everything
> should be created and related for you. Did you check if the relation
> exists in your action in the first place when the action is called?
>
> cheers,
> Christian
>
> On 23/09/11 23:35, Ivan Ruiz Gallego wrote:
>> Hello,
>>
>> First of all, I'd like to thank the development team of FLOW3 for all
>> the work done so far. I have a newbie question
>>
>> Let's say I have a model \Acme\MyPackage\Domain\Model\Foo with a
>> OneToMany association to \Acme\MyPackage\Domain\Model\Bar. Foo and Bar
>> have both a property 'name'. For the property 'bars' within Foo, the
>> cascade annotation is set. Foo is the aggregate root and has a
>> repository and an action controller. I have a form to create foos with,
>> let's say, three associated bars. A template 'new' could look like this:
>>
>> <f:form action="create" object="{foo}" name="foo">
>> Foo's name: <f:form.textbox property="name" />
>> Bar's name: <f:form.textbox property="bars.0.name" />
>> Bar's name: <f:form.textbox property="bars.1.name" />
>> Bar's name: <f:form.textbox property="bars.2.name" />
>> <f:form.submit value="submit" />
>> </f:form>
>>
>> I initialize the create action so that the properties 'bar.0', 'bar.1',
>> 'bar.2' can be created. Additionaly, I have a setter method 'setBars'
>> within the Foo class.
>>
>> After form submission my foo and my three bars get created and
>> persisted. But FLOW3 doesn't establish the relation between foo and
>> bars, i.e. in the db the field 'mypackage_foo' from table
>> acme_mypackage_domain_model_bar is still NULL.
>>
>> What am I missing? Do I have to write a Type Converter for Foo, so that
>> FLOW3 knows how to create the Foo instance from the $_POST['foo'] array?
>> Or is something in my 'new' Fluid template wrong? Or do I have to
>> process the POST array on my own, create all objects and set the
>> relation manually?
>>
>> Thanks,
>> Ivan.
>



More information about the FLOW3-general mailing list