Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Zend_db and Zend_Db_Table

  1. #1
    aniltc is offline 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.

  2. #2
    kuba is offline 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

  3. #3
    aniltc is offline 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.

  4. #4
    jasonw is offline Junior Member
    Join Date
    May 2008
    Posts
    12

    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]
    <?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();

    [/PHP]

    I could go on and on, but I'd just be re-writing the documentation.

  5. #5
    aniltc is offline 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

  6. #6
    Filip is offline 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.

  7. #7
    aniltc is offline 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

  8. #8
    Filip is offline 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.

  9. #9
    aniltc is offline 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.

  10. #10
    Filip is offline 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

Page 1 of 2 12 LastLast

Similar Threads

  1. Zend_Db
    By Miel Peroff in forum General Q&A on Zend Framework
    Replies: 2
    Last Post: 07-09-2009, 05:01 PM
  2. Zend_Db & Interbase
    By sief_bilog in forum Databases
    Replies: 0
    Last Post: 02-09-2009, 02:43 PM
  3. Zend_db Vs PDO
    By justin@kelly.org.au in forum Databases
    Replies: 0
    Last Post: 12-04-2007, 11:33 PM
  4. Zend_Db - insert a set
    By Eagle in forum Databases
    Replies: 1
    Last Post: 10-04-2007, 05:57 PM
  5. zf 1.0.1 Zend_Db and i5 problem?
    By kgenaidy in forum Databases
    Replies: 0
    Last Post: 08-18-2007, 01:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •