Martin Kreidenweis wrote:
> Hi,
>
>   
>> I am using the sfPropelActAsNestedSetBehaviour and I have to make the 
>> new element either the root or the child of another node. So I did this 
>> in my executeEdit() method:
>>
>> [php]
>> public function executeSave()
>>   {
>>     $this->store_catalog = $this->getStoreCatalogOrCreate();
>>
>>     if ($this->getRequest()->getMethod() == sfRequest::POST)
>>     {
>>       try
>>       {
>>         $this->store_catalog->makeRoot();
>>         $this->store_catalog->save();
>>       }
>>       catch (Exception $e)
>>       {
>>         ;
>>       }
>>     }
>>
>>     return parent::executeSave();
>>   }
>> [/php]
>>
>>
>> But this caused my form to created two new records in the database: one 
>> was the call of $this->store_catalog->save(); in my executeSave() and 
>> the other record was created by the call to parent::executeSave(); The 
>> long story short, the class method getStoreCatalogOrCreate() which is 
>> auto generated does not account for cases where $this->store_catalog 
>> already exists and always overwrites it thus creating a new record when 
>> $this->store_catalog is already a valid StoreCatalog object. I do not 
>> think I explained very well but this is as good as I can do it :)
>>
>> This is that needs to be added to $this->getStoreCatalogOrCreate()
>>
>> [php]
>> protected function getStoreCatalogOrCreate($id = 'id')
>>   {
>>     if ($this->store_catalog instanceof StoreCatalog) {
>>       return $this->store_catalog;
>>     }
>>     // then the rest of the code it is currently generated
>>   }
>> [/php]
>>
>> I found a workaround to this problem by adding $_POST['id'] = 
>> $this->store_catalog->getId(); right after my call to 
>> $this->store_catalog->save() thus tricking the getStoreCatalogOrCreate() 
>> method to think that the form is submitted in order to edit the record 
>> and not to create it but I do not want to use such a workaround when I 
>> think a permanent solution can be implemented.
>>     
>
> It you don't want to actually change the executeEdit() workings but only
> do something with the object, why not just overwrite the some of the
> other methods?
> Like that getStoreCatalogOrCreate(), which would have the benefit, that
> you can modify new objects before showing the create form:
>
> protected function getStoreCatalogOrCreate($id = 'id')
> {
>   $storeCatalog = parent::getStoreCatalogOrCreate($id);
>
>   if ($storeCatalog->isNew())
>   {
>     $storeCatalog->makeRoot(); // or something...
>   }
>   else
>   {
>     $storeCatalog->doSomethingElse();
>   }
>
>   return $storeCatalog;
> }
>
> Or overwrite the saveStoreCatalog() method, which might be simplest in
> your case:
>
> protected function saveStoreCatalog($storeCatalog)
> {
>   $storeCatalog->makeRoot(); // or something
>
>   parent::save($storeCatalog);
> }
>
> Finally, if you really want to customize the executeEdit() method, just
> do everything that needs to be done there yourself and don't call the
> parent's method at all.
>
>  Martin
>
> >
>
>   
Yes, you are both right. If I wanted to just execute some code before 
the save method then I would overload saveStoreCatalog(). The thing is 
that I want to make decision based on that I get from the form and I 
want to be able to use the generator to tweak the form and add new 
fields without the need to write myself executeSave() which is why want 
to call parent::executeSave() in the end.

I think I will go with saveStoreCatalog() but I do not think it is a 
good idea to put $this->getRequestParameter() in this method, I think 
all the logic should go in the execute*() methods.

So, thank you both for the hint but don't you think my suggestion is 
valid and will not hurt whoever does not want to use it but will help 
people like me to not touch the "special" methods in the action?

Kupo

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/symfony-devs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to