One to Many with Zend_Db

May 4th, 2009

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.

 

27 Responses to “One to Many with Zend_Db”

  1. Pacek says:

    Great one, as per usual.

  2. Pacek says:

    btw: are you using netbeans with some ZF plugin or Zend studio?

  3. Arash Akbarpour says:

    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.

  4. Fadel says:

    thank you

    im from morocco ican’t support you because we have no international credit card :-( , you can put same AdSense

  5. Alexander says:

    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.

  6. jon says:

    Zend Studio. I think I’ll be using both interchangeably depending on what I’m using professionally.

  7. Dan says:

    Good Work. What font do you use in the editor?

  8. Yury says:

    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 =)

  9. jon says:

    I use Consolas!

  10. jon says:

    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!

  11. Splendid says:

    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!?

  12. jon says:

    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… );

  13. star says:

    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.

  14. jon says:

    @star: look at Doctrine ;)

  15. Russ says:

    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?

  16. Elliot Yap says:

    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.

  17. jon says:

    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

  18. Elliot Yap says:

    Hi, Jon, I will Check it out. Thanks for your Reply =D
    Your ZendCast Is really helpful.
    Have a nice day!

  19. Benjamin says:

    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.

  20. jon says:

    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.

  21. ricardo says:

    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.

  22. Seb says:

    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.

  23. jon says:

    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.

  24. Bob Lorriman says:

    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?

  25. 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

  26. Russ says:

    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.

Leave a Reply

Desktop RSS feed iPhone + iPod