This is part 3 in the Zend_Db series. I’m only scratching the surface with what you can do with Zend_Db_Table classes, however starting is often the hardest part. I’ve posted the code once again on the Google code for this episode, so please don’t be shy and grab a copy.
There’s a lot of discussion over where and how I’m using the model in the comments, and I have a feeling that as I keep doing this, some of my controversial design decisions (whether out of ignorance, which I’ll gladly acknowledge, or professional experience) might start to surface. I’m really hoping to do a series of similar videos with Doctrine, since I find their solution to database persistence really elegant. If anyone has any suggestions, or would like access to the Google code, I would love to open up the discussion and find the “best practices” here. Personally, I would start by moving our custom made form to Zend_Form and adding some much needed validation to the project.
Lastly, I need to apologize for the delay, this week has been quite full due to work and personal commitments. I’m going to keep the video a week schedule as best I can. The video also deals with setting up the MySQL database, if this isn’t interesting, skip to the 12 minute mark.

Great one, as per usual.
btw: are you using netbeans with some ZF plugin or Zend studio?
Hi there
Have not seen this video yet.just during downloading thi video i wana appreciate your struggle.I am sure this podcast is neat and clear as former ones.keep good working.
thank you
im from morocco ican’t support you because we have no international credit card
, you can put same AdSense
Fadel,
You don’t need an credit card. You can use your bank account as well. Just look for more information at http://paypal.com/
—
Thanks for the nice tutorial again.
Only i use the model and zend db on an other way.
Zend Studio. I think I’ll be using both interchangeably depending on what I’m using professionally.
Good Work. What font do you use in the editor?
Hi,
First of all, thanks for the grate videos, I’ve saw a link to that website in some PHP forum, and i very enjoyed few of your videos.
At getContactTypeByName, you dont need to lower the case, since you’re using the latin1_swidish_ci (as seen at the phpMyAdmin) the ci means Case Insensetive, and therefore:
mysql> SELECT ‘NAME’ = ‘name’;
+—————–+
| ‘NAME’ = ‘name’ |
+—————–+
| 1 |
+—————–+
1 row in set (0.00 sec)
without changing the case, unless i’m missing something =)
I use Consolas!
Yuri:
good point! However, I don’t think its wise to make the assumption that your collation will always be so lax. I’m not a db expert (I’m a big fan of tools that abstract the developer away from database implementations), so thanks for mentioning this!
Hi Jon,
Here is one question, and I think it is easy.
I was wondering how to print category name in view if u are just sending users to view with fetchAll() method ?
I know that u can use some method call in view which will return category name on its id but I wanna know what is the best way!?
Hi Splendid,
A better query then a fetchAll would be one where you specify specific columns in your Select.
There’s an example (15.48) in the zend docs that should be a good guide:
$select = $db->select()
->from( …specify table and columns… )
->where( …specify search criteria… )
->order( …specify sorting criteria… );
Good video, i haven’t much used the Zend_Db as it really looks difficult and pain to use.
More on video, you could talk more on the dependentRowset and the magic functions.
@star: look at Doctrine
I got to the end of this screencast and ended up with the following exception:
No reference from table UsersTable to table ContactTypesTable
I’m unfortunately stumped. Any ideas?
Hey Russ,
I would double check the code sample:
http://code.google.com/p/zendcasts/source/browse/trunk/ep20-one-to-many-with-zend-db/application/default/models/ContactTypesTable.php
and
http://code.google.com/p/zendcasts/source/browse/trunk/ep20-one-to-many-with-zend-db/application/default/models/UsersTable.php
I don’t understand why you create the protected $_referenceMap in the UsersTable but you didn’t use it. and also the $_dependentTables in the ContactTypeTable
I thought it will be some useful trick at beginning.
Hey Elliot, those two variables are used for running the findDependentRowset methods in Zend_Db. We use them for establishing the relationship and making those queries run without actually doing the joins ourselves
Hi, Jon, I will Check it out. Thanks for your Reply =D
Your ZendCast Is really helpful.
Have a nice day!
Hi,
I’m wondering why you are using current() and findDependentRowset()methods directly from action controller.
In that case you’re losing the benefit of the Service Layer which is to uncouple application logic and persistence (Zend_Db).
There, you’re just using Zend_Db_Table methods from controller.
Or maybe it was to just get to the point with findDependentRowset and you decided to not specify this detail (but still important). If that’s it, i’m sorry for disturbing
Cya,
Benjamin.
Hi Benjamin,
You’re absolutely correct! This video was done with the intent of showing the Zend_Db_Table functionality, unfortunately I didn’t get to really build on the service layer concept due to time. In production, I would probably keep the responsibility for OR-mapping in the models. The service layer is really just a primitive implementation of the Repository concept. Ideally, if this was a larger application, I would suggest using a Repository with a UnitOfWork that would isolate transactions and object creation / persistence. The upper tier would only be concerned with querying the Repository for objects and then passing them back.
Hi,
telling you up front: great tutorial!
Besides your introduction of the service layer, you mention the use of domain driven principles like repositories and unit of work. Is it possible to do a tutorial where those principles (like factories, repositories, unit of work, etc..) are exposed?
Thanks.
Hi Jon,
I got an issue at the end of this tutorial and i think this is something about the new framework 1.9. In fact from the beginning of this tutorial i have to name my models “Default_Model_ContactTypesTable” (which is really annoying) to make things work properly.
And when i use findDependentRowset(‘UsersTable’) i’ve got an error that i didn’t manage to solve : Message: File “UsersTable.php” does not exist or class “UsersTable” was not found in the file
Of course UsersTable.php exist :p
Any idea ?
Thank you.
Seb.
Hey Seb,
I haven’t touched Zend_Db_Table in 1.9, however I think I read something about how the model_ prefix isn’t required. I believe someone posted about this on the Zendcasts forum a couple weeks back. Hopefully, all this will be sorted once PHP 5.3 is adopted and we have proper namespacing.
Another great tutorial – Thanks Jon.
Everything works like a charm – right up to the end. When I click on the ContactType (ie: Business, Other or Personal), it always defaults to the first one “Business”.
I’ve check all the code (I think) as well as the database and I just can’t see the problem.
Anybody else come across this?
Too much, too confusing. Got little of it mate.
My point was to understand how to map the DB to the models, and then how to use it on the controllers. I must say i’ve learned quite a bit over all the casts, but not this bit mate.
Thanks anyway
I’m getting everything to work as it should in the tutorial; however, I’m trying to understand how the GetContactTypeByName($name) function is returning more than one row in the view from the database using fetchrow() in the UserService. The reference manual says fetchrow() should return the first row only and in other scripts I’ve been writing, it does just that. I just want to understand where in the tutorial you got around that.
Hey Jon,
what are your ideas with regard to Zend_Db_Table and speed. I’m currently doing some research on one to many and many to many relations with Zend_Db_Table.
Using examples like yours or the ones given in the Zend Framework Reference guide, I see a dramatic performace with regard to speed.
If I for instance have tables
A
B
and AB
where AB defines the many to many relationship between A and B (two identifier colums in AB, namely Aid and Bid, both function as joined primary key, both in Foreign Keys to the respective tables)
Now I’ve got the Model structure set up as supposed and everything works fine. But then I do
$result = A->fetchAll()
foreach($result as $row){
$details = $row->findManyToManyRowset(‘B’, ‘AB’);
foreach($details as $detail){
…echo something…
}
}
If I do this for let’s say 150 parentrows with an average of 3 child rows this operation takes a whopping 13 seconds. If I do a join statement in parent’s foreach loop, if finishes in 1-2 seconds…
Relations with Zend_Db_Table are SLOW
Niels, they are not slow. You just do a lot of sql queries in the loop, that’s the reason.
any idea why im getting a Cannot redeclare class UsersTable error when i try to use the findDependentRowset method?
First, great tutorials. Thank you.
I am wondering why everybody is running away from SQL.
Why all new developers do not use pure SQL and go for some strange abstraction that require complexly new skills, and make things so difficult and kill performance.
I newer see somebody to change DB vendor on running project, and i am in this business for a long time.
Just want to know your opinion, because i really do not understand why when SQL is abstraction itself, you wrap it whit another layer of abstraction (most of the time 2-3 layers)
Thank you!
Hi Niels,
obviously the former approach will be slower since you’re essentially making a separate transaction for each call and you’re generating many more queries. This is where a proper ORM helps a lot since it will (or should) cache repeat queries that rely on a particular relation. Handling the joins yourself can be tedious since it pulls you outside of the object model and into a db relational model.
Where is in svn delete.phtml?
Oh my gosh! You opened up a new world to me. I’ve been coding queries manually using (don’t laugh) vbscript and to see the relations laid out through Zend Framework was absolutely awesome to see! Thank so much!
Hi,
First of all thanks for the wonderful video’s, I’m learning a lot!
But I have a question:
On my index, I want to display the contacttype besides the name. Now I’m using a foreach on standard info. How do I mix these together to display the names from both the tables?
Did anyone get this working with 1.10?