Author Topic: Zend_Db_Table  (Read 1726 times)

0 Members and 1 Guest are viewing this topic.

Offline moon 111Topic starter

  • Enthusiast
  • Posts: 194
  • Gender: Male
    • View Profile
Zend_Db_Table
« on: January 26, 2009, 06:18:21 AM »
I have started using ZF and I was wondering: do you usually have a class for each of your tables, or do you use the built in classes (Zend_Db_Select etc.) in your controllers? Also, I've found it very difficult to have Table relationships in the classes and Zend's documentation has been no help.

Thanks,
- Moshe

P.S. If anyone farmiliar with ZF can help me out over E-mail, or MSN when I need help, it would be greatly appreciated.
Quote from: Crayon Violent
Is this a trick question? Because I have seen plenty of sci-fi movies where people have to be frozen into suspended animation in order to make this jump, so why would you be out of your pod? I mean, there's protocol for this, right?  Tom would not be just randomly hitting the warp speed button without you knowing...

Offline gethinw

  • Irregular
  • Posts: 24
    • View Profile
    • All Things Considered Blog
Re: Zend_Db_Table
« Reply #1 on: February 09, 2009, 07:57:01 AM »
I'd be quite interested to see what people say in reply to this. I'm quite new to MVC style programming, and this in particular is something I've struggled with getting my head around.

FWIW I haven't actually ended up using Zend_Db_Table much recently, but what I've generally ended up doing is having a Model class for each source of data, which might be one table, or could be a combination of tables (using JOIN or whatever), and extending the built in classes, keeping as much of them as possible, but altering any bits which are necessary, to keep the controllers as small as possible - one of the key ideas behind ZF is that you should aim for a 'skinny controller' (ie as little code as possible in the controller - it should basically just get data from the model and pass it to the view (and vice versa if appropriate), but do as little data manipulation as possible).

That probably doesn't help much really... If you give us a better idea of what you're trying to do we might be able to provide more direct guidance!

Offline milesap

  • Enthusiast
  • Posts: 64
    • View Profile
Re: Zend_Db_Table
« Reply #2 on: February 17, 2009, 09:11:35 PM »
Each table would have it's own class, because you want to separate the Model from the Component in the MVC architecture.

Let's say your User table in MySQL has a last modified field. Since the table has it's own class, the Model can update the last modified field everytime you update that row, rather than you having to do it in each query.

Offline JustinM01

  • Irregular
  • Posts: 29
  • Gender: Male
    • View Profile
    • Jmccauli.com - My Blog
Re: Zend_Db_Table
« Reply #3 on: February 24, 2009, 12:18:20 PM »
I usually just extend Zend_Db_Table_Abstract. Like this:

Code: [Select]
class SomeTable extends Zend_Db_Table_Abstract
{
    protected $_name = 'table_in_your_db';
    protected $_primary = 'column_name_of_your_primary_key';
}

Its very simple. This allows me to use Zend_Db_Select very easily along with updates, inserts, and deletes.

Code: [Select]
$tableObj = new SomeTable();
$select = $tableObj->select();
$select->where(array("$id = $someId", "time" => "UNIX_TIMESTAMP(time)"));
$row = $tableObj->fetchRow($select);

Sure, I could make an actual function on the table to do the above seamlessly, but I haven't yet. It works, and it works well. It really is up to you and what you're comfortable with. So, yes, I do make separate classes for each of my tables. It allows me to do the above and get fancy if I so choose.