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


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-19-2008, 04:39 AM
Junior Member
 
Join Date: May 2008
Posts: 9
Question Zend_db and Zend_Db_Table

hi all

I am new to ZF.i have so many doubts in Zend db (I mean model part).

can anyone tell me what is the real difference between Zend_db and Zend_Db_Table.Can i create a website using zend_db rather than Zend_Db_Table.

what is the advantage of Zend_Db_Table.? which one I should use.

I am totaly confused.please help me.

Thanks
PHP Interview Questions AJAX Interview Questions MySQL Interview Questions PHP web Development questions PHP FAQs Tutoring Online PHP PHP Programming Questions PHP Freshers Job Questions Testing Interview Questions PHP OOPs interview questions *Techn

Last edited by aniltc : 05-27-2008 at 04:16 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-19-2008, 06:27 PM
Junior Member
 
Join Date: Mar 2008
Posts: 26
Default

Zend_Db is ZF component responsible for connecting to database. Zend_Db_Table stands for model. :P
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-21-2008, 05:35 AM
Junior Member
 
Join Date: May 2008
Posts: 9
Default

I agree that,However,which one I should use for quering db.I think Zend-db is easy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-22-2008, 03:35 AM
Junior Member
 
Join Date: May 2008
Posts: 10
Default

They are equally easy. In fact, if you are using Zend_Db_Select objects to build your queries (You are, aren't you?), they are exactly the same.

It is worthwhile to mention that Zend_Db_Table queries return either Zend_Db_Row or Zend_Db_Rowset objects, which allow you to some very nice things such as:

PHP Code:
<?php
$bugs 
= new Bugs();
$row $bugs->fetchRow($bugs->select()->where('bug_id = ?'1));

// Change the value of one or more columns
$row->bug_status 'FIXED';

// UPDATE the row in the database with new values
$row->save();
I could go on and on, but I'd just be re-writing the documentation.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-22-2008, 04:30 AM
Junior Member
 
Join Date: May 2008
Posts: 9
Default

i am totaly confused.in documentaion Zend mentioned both zend_db and zend_db_table.so i can use any one to intract with db.it doesn't matter. does it? so when i use model,is it must to use zend_db_table.Can i use zend_db in model for writting quires instead of zend_db_table?
please clear my all doubts.

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-22-2008, 10:02 AM
Junior Member
 
Join Date: Apr 2008
Posts: 28
Default

If I recall correctly you're supposed to use Zend_db_table. Use your models to call info from that DB. Otherwise you're ignoring the MVC pattern (I think). The way I see it (and do it), you always use your model to build queries. It can be a pain if you've got 6 tables you need in a query. But you can always concider creating a view and use that rather then a huge query..


Edit: As you can see in the examples in the documentation, they instantiate the Model class of a table (zend_db_table) and use that to build queries.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-22-2008, 01:45 PM
Junior Member
 
Join Date: May 2008
Posts: 9
Default

Thanks to all

I have one more doubt.I have 2 tables which is having relation ships,my doubt here,how can i join my two tables usimng zend_db_table method ( I mean in MVC pattern)

I have given $_protected as said in the documentation.but how can i use join keyword in model.can i use join keyword in model.I don't know whether iam right or not.please help me.

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-22-2008, 06:55 PM
Junior Member
 
Join Date: Apr 2008
Posts: 28
Default

Zend Framework: Documentation

referenceMap => an associative array, with information about references from this table to any parent tables. See Section 10.8.2, “Defining Relationships”.

dependentTables => an array of class names of tables that reference this table. See Section 10.8.2, “Defining Relationships”.

This'll probably help to find what your looking for. Thats what defines one-to-many relationships.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-23-2008, 01:59 PM
Junior Member
 
Join Date: May 2008
Posts: 9
Default

that means no need to write joins in the model.This will be equallent to join.
is it?
suppose if i want some columns from a relation table,how can i take


can i use like this in my model


<?php
$table = new Bugs();

$select = $table->select();
$select->where('bug_status = ?', 'NEW')
->join('accounts', 'accounts.account_name = bugs.reported_by')
->where('accounts.account_name = ?', 'Bob');

$rows = $table->fetchAll($select);

?>

PHP Interview Questions AJAX Interview Questions MySQL Interview Questions PHP web Development questions PHP FAQs Tutoring Online PHP PHP Programming Questions PHP Freshers Job Questions Testing Interview Questions PHP OOPs interview questions *Techn
Thanks

Last edited by aniltc : 05-27-2008 at 04:13 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-26-2008, 02:31 PM
Junior Member
 
Join Date: Apr 2008
Posts: 28
Default

Fetching data from your table.

Either you write a function in your table class (the class Bugs in the example) or you use a predefined method (such as $table->find(<primary key value to look for>) )

Predefined method:

Code:
<?

 $bugs = new Bugs();
 $bugs = $bugs->fetchAll();
 
//$bugs now contains the complete table Bugs
or
Code:
$bugs = new Bugs();
       $bugs = $bugs->find('1')->current();

// this will return a zend_table_row with id 1 (if it exists)
if you write a new function in your bugs class

Code:
private function getUserBugs($userID)
{
    $sql = $this->select()->where('<column> = ?', $userID)
                                 ->where(<other where statements>)
                                 -> order(.....)
                                 ->limitpage(.....)
    return $this->fetchAll($sql)
}
now you can use this in your function layer (where your business logic should be)


Code:
$bugs = new bugs();

   $bugs = $bugs->getUserBugs('1');
after which $bugs will contain a rowset of 0 - X rows.

Iterating between them is nice & easy with a foreach.


and last & not least. If you set the referencemap & the dependentTables like I said in my previous post, there's no need to use joins in your syntax. You can just as easily use the method findparentRow // findDependentRowset

(see more in Zend Documentation on the 2 methods...)

Last edited by Filip : 05-26-2008 at 02:33 PM. Reason: code tags
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 11:46 PM.