Zend_Db is ZF component responsible for connecting to database. Zend_Db_Table stands for model. :P
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.
Zend_Db is ZF component responsible for connecting to database. Zend_Db_Table stands for model. :P
I agree that,However,which one I should use for quering db.I think Zend-db is easy.
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.
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
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.
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
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.
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.
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:
orCode:<? $bugs = new Bugs(); $bugs = $bugs->fetchAll(); //$bugs now contains the complete table Bugs
if you write a new function in your bugs classCode:$bugs = new Bugs(); $bugs = $bugs->find('1')->current(); // this will return a zend_table_row with id 1 (if it exists)
now you can use this in your function layer (where your business logic should be)Code:private function getUserBugs($userID) { $sql = $this->select()->where('<column> = ?', $userID) ->where(<other where statements>) -> order(.....) ->limitpage(.....) return $this->fetchAll($sql) }
after which $bugs will contain a rowset of 0 - X rows.Code:$bugs = new bugs(); $bugs = $bugs->getUserBugs('1');
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