Many to Many with Zend_Db and Zend_Form
May 13th, 2009Exposing many-to-many in a practical application took a little more time and effort than I had anticipated. With that in mind, I to use this opportunity to explore how Zend_Form, Zend_Controller and Zend_Db could be integrated. This is the first in a two part set looking at our data model.
By the end of the second video, you should have a project that explores the following concepts with Zend_Db:
- A user has many tasks (many-to-many)
- A task has many users (many-to-many)
- A user has one contact type (one-to-many)
- A contact type has many users (many-to-one)
Unfortunately, this only gets through setting up the database, CRUD with Tasks and setting up the Many-to-Many classes in Zend_Db. The next video will cover how we can write a Zend_Form that will map the associations between the users and tasks. Enjoy!
If you can’t wait till the next video, grab the code on google code and play with it firsthand.

hello jon ,
i am really thankful for your casts but could you provide a downloadable zip files of these project ….
weather your answer is yes or no i’ll say thank you
Hey Jon.
Wonderful to see your approach towards explaining this really nasty topic ^^;
However, i want to mention a thing about the submit button and the adding/removing
$submit = new Zend_Form_Element_Submit($this->formState, array( ‘ignore’ => true ) );
with this simple line of code / option passed the buttons value won’t be returned by Form::getValues()
i hope this helps you/others who read it
Thanks jon. you are doing a better job again.. ı’m waiting next video impatiently. i wonder do you make some video about ACL ?
Thanks again.
@Silone:
Thanks for mentioning this! I didn’t know Zend_Form had that option.
@tafekov:
I’ll prepare a download and post it with the next episode.
Hi jon,
Can you make a screencast of zend_form_element_file below is a sample form code but I don’t know where the file is uploaded to ->setDestination(’/data’) what is the complete path of this code? Please help.
setName(’upload’);
$this->setAttrib(’enctype’, ‘multipart/form-data’);
$description = new Zend_Form_Element_Text(’description’);
$description->setLabel(’Description’)
->setRequired(true)
->addValidator(’NotEmpty’);
$file = new Zend_Form_Element_File(’file’);
$file->setLabel(’File’)
->setDestination(’/data’)
->setRequired(true);
$submit = new Zend_Form_Element_Submit(’submit’);
$submit->setLabel(’Upload’);
$this->addElements(array($description, $file, $submit));
}
}
[...] is part 2 in a series on many to many with Zend_Form and Zend_Db. I suggest starting with last week’s video on Zend_Db and many-to-many in order to follow the configuration of our models. Grab the code and follow along! This concludes [...]
@Ron,
I worked with a custom Form Element for file transfers awhile back and I remember having to merge the $_POST and $_FILES variables before passing them off to Zend_Form, however I this was a year ago (which is ancient in terms of zend framework development.) I’m not familiar with Zend_Form_Element_File, but I shoot me an email and I’ll send you the object I used for Zend_Form file transfers a year ago… hopefully that will help.
Hi Jon,
Perfect work, I’ve worked through all your video’s up to this one. Here I’ve some comments.
In Task.php on line 22:
$rowAr = $currentTask[0]->toArray();
In older version of ZendFramework you get an error.
My suggestion would be using:
$this->currentTask->current()
instead of [0]
In Task.php on line 45-46:
foreach ($this->getElements() as $element)
$this->currentTask[0]
[$element->getId()] =
$element->getValue();
My suggestion would be to collect the form data in a separate array, like:
$arrData[$element->getId()] = $element->getValue();
and after the loop setting it into the array using:
$this->currentTask[0]->setFromArray($arrData);
Perhaps you can comment on this.
Thanks again for all your work!!
Kind regards,
Johan.
Hi Johan,
why would you go about creating a separate array? I don’t see much value in that. since you’re just creating another array with no modifications. If you were filtering or modifying the data, I think your approach makes a lot of sense.
Thanks for mentioning that current() is more applicable that the [0] index!
[...] Many to many with Zend_form and Zend_Db [...]
[...] is part 2 in a series on many to many with Zend_Form and Zend_Db. I suggest starting with last week’s video on Zend_Db and many-to-many in order to follow the configuration of our models. Grab the code and follow along! This concludes [...]
Большенство почему-то считает, что материал полный. А я совсем другово мнения по этому вопросу. Давайте обсудим
Меня заинтересовал этот материал, не могли бы Вы поподробнее и с дополнениями раскрыть эту тему?
Я бы порекомендовал постояно выкладывать, подобную интересную информацию! Благодарю
Мнение очень сильно расходятся. Всё-таки я считаю, что автор во многом прав!
Once again a great tutorial, . . . but I’m stuck at the end!
When I input a name into the task form I get the following error:
Incorrect integer value: ” for column ‘id’ at row 1
I even did a cut and paste from google docs just in case I had any typo’s and I still get the error. I’m using ZF1.9 – any suggestions?