Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-01-2007, 11:05 PM
Junior Member
 
Join Date: Oct 2007
Posts: 5
Default zend_db: grasping basic concepts

Just started on the zend framework, and was wondering if every model that requires database access has to extend zend_db_table_row? For instance, if i have an author class, i assume the author finder/ gateway extends zend_db_table? This means that the returned/ found author must extend zend_db_table_row, is that correct? What if the author model contains a collection of books by that author - how is that relationship expressed in terms of zend_db_table and zend_db_table_row? Is it considered good mvc practice to pass models to the view that have a db-accessable interface, or should they be "read-only" objects? Would you always use the zend_db_* interface for db access, or would you ever use pdo directly in a model?



Thanks for any answers, i hope someone can clear this confusion for me
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-05-2007, 01:19 AM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

I'm not sure I completely follow your questions, but here's I deal with it all...

For a database connection I will reuse often or need to access more than just a couple of tables in I create and register an adapter via Zend_Db::factory();

Code:
Zend_registry::set('dbDefault',
                   Zend_Db::factory('Pdo_Mysql', array(
                                    'host'     => '127.0.0.1',
                                    'username' => 'webuser',
                                    'password' => 'xxxxxxxx',
                                    'dbname'   => 'test'
                  )));

then I create a model by extending Zend_Db_Table
Code:
class Bugs extends Zend_Db_Table_Abstract
{
    protected $_name = 'bugs';
}
If that table is in my default adapters db its straight forward
Code:
$table = new Bugs();
If that table is in another adapter:
Code:

$db = Zend_Registry::get('myDbAdapter');
$table = new Bugs(array('db' => $db));
now you have a table object and you can run your queries via the docs...
since its a Zend_Db_Table type object the results will be of type Zend_Db_Table_Rowset.

The rowset can be turned into an array and you can mold it to your needs then feed the array to the view, etc.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-05-2007, 11:41 AM
Junior Member
 
Join Date: Oct 2007
Posts: 5
Default

Thanks for the reply.

How likely are you to have more than one adapter in a project? It seems the only viable use would be for different adapters to point to different databases?

With regards to my original post, i'll try and explain it a little better.

Whereas i'm currently used to creating a model, say Author, and using pdo from within the class to populate it's properties (activerecord i suppose?), after reading the docs on zend_db_table it seems that for any model in the application that requires db access, i need to make it a child of zend_db_table_row, since the gateway returns an object of that type.

eg:
PHP Code:
class Author
{
   function 
load($id){
      
$record $this->db->prepare('select * from authors where id=?'$id)->fetchRow();
      
$this->name $record['forename'] . ' ' $record['surname'];
      
$this->id $record['id'];
      
//etc

See, it's like i used to build the objects following the activerecord approach? Well that way, an author is an author. Now if i use the zf approach, it would be something like:
PHP Code:
class Author extends zend_db_table_row
{} 
is that right? Meaning that the author interface now has a load of data access methods?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-05-2007, 02:47 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

I see what your saying now I think:

Code:
class Authors extends Zend_Db_Table_Abstract {
  //table definition
}

class Author extends Zend_Db_Table_Row {
  //individual record from Authors table
}
I guess there's nothing wrong with that, but it would only really be necessary if you need to alter to default behavior of Zend_Db_Table_Row. Anything you pull out of a Zend_Db_Table object will be of type Zend_Db_Table_Row or Rowset. If you query straight off the db adapter you'll get arrays mostly.

I currently have a project underway that connects to no less than 6 different dbs on 4 diferent servers, and we're just dealing with infrastructure at the moment, no real function pages have been producted... so yes is very possible within the enterprise to have a set of db adapters as apposed to a single adapter.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-08-2007, 04:47 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

Here's a free webinar I found that might help, I know I plan on checking it out!

Events / Company / Zend.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 08:22 PM.