[TYPO3-mvc] Form for saving an object with mm relationships
Daniel Dimitrov
danielsd_bg at yahoo.fr
Fri Aug 13 14:55:47 CEST 2010
Hi,
I'm starting to feel desperate with extbase and fluid forms.
For simple forms - it is just great! No complains, but I don't understand
how to make a more complicated examples.
If we look at blog_example we will see that the frontend form for inputing a
blog doesn't have a field to input tags (the property tags is an object
storage and is creating the mm relationship). Instead tags are fix and
inputted in getBlog.
Here is what is bothering me:
http://compojoom.com/images/typo3/simpleForm.png
Rubriken(categories) are stored in cat1, cat2, cat3 fields in the database
table.
If I use <f:form.checkbox name="cat1[]" /> fluid will transform this to
<input type="checkbox" name="tx_bwshop_pi1[cat1][]" />
That is cool - this way I have all the data in the post array for cat1. But
if I do it this way, extbase is never going to call the setCat1 function in
my model.
If I change the above code to
<f:form.checkbox property="cat1[]" /> then fluid transforms it to:
<input type="checkbox" name="tx_bwshop_pi1[newProduct][cat1[]]" />
and as you can imagine this doesn't work.
Keywords(schlagwortliste) - as you can see in the above screenshot I have 6
input fields - you can enter max 6 keywords per product.
My product model looks like this:
class Tx_BwShop_Domain_Model_Product extends
Tx_Extbase_DomainObject_AbstractEntity {
/**
properties such as title, description .... are here
**/
/**
* Product first category
* @var array
*/
protected $cat1;
/**
* Product second category
* @var int
*/
protected $cat2;
/**
* Product third category
* @var int
*/
protected $cat3;
/**
*
* @var
Tx_Extbase_Persistence_ObjectStorage<Tx_BwShop_Domain_Model_Keyword>
*/
protected $keywords;
/**
set and get methods...
**/
}
the 6 keywords that you can enter should be resolved to a
Tx_BwShop_Domain_Model_Keyword, but the question here is how???
My create action looks like follows:
public function createProductAction(Tx_BwShop_Domain_Model_Product
$newProduct) {
$this->productRepository->add($newProduct);
$persistenceManager = Tx_Extbase_Dispatcher::getPersistenceManager();
$persistenceManager->persistAll();
$this->redirect('showProduct',NULL,NULL,array('product' =>
$newProduct));
}
The problem with the above code is that the $newProduct object that extbase
creates is missing some properties such as cat1,cat2,cat3 and $keywords.
What I plan is to get the information from the $_POST array myself and use
the set methods in my Tx_BwShop_Domain_Model_Product.
Is this the right approach here? or is there a better way to do it? Perhaps
extbase can "magically" create the objectStorage for me as it does in the
backend???
Another thing that bothers me with the approach I'm planning to make is that
once I find an error in the $_POST array (for example - no keyword
selected) I have
to send the user back to the form(in my case public function
newProductAction(Tx_BwShop_Domain_Model_Product $newProduct = NULL) {}) and
once the form
loads it should have the already filled data by the user. $this->redirect or
$this->forward were both not able to send the $newProduct object.
What am I missing here? Are there any complicated extensions that I can use
for a model? I couldn't find something appropriate in the typo3.org
repository.
Any help and hints are appreciated!
Thank you in advance!
Daniel
More information about the TYPO3-project-typo3v4mvc
mailing list