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


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-24-2008, 08:37 PM
mgordon's Avatar
Junior Member
 
Join Date: Sep 2007
Location: Stockholm, Sweden
Posts: 16
Send a message via Skype™ to mgordon
Post Zend_Db_Table queries containing the table name preparing for joins

I would like to see Zend_Db_Table to query everything with the table name. I want a find($my_id) result in a: "SELECT `table_name`.* FROM `table_name` WHERE `table_name`.`primary_key_id` = '$my_id'"

Is there a reason not to incorporate the table name into the default queries? This allows for flexibility in future joins where there might be a second table with the same column name as the primary_key_id in the main table.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 03:35 AM
Member
 
Join Date: Jan 2008
Location: chicago
Posts: 98
Default

Zend_Db_Table already allows you to make joins.

Code:
<?php

$table = new Bugs();

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

$rows = $table->fetchAll($select);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 06:00 AM
Member
 
Join Date: Feb 2008
Posts: 46
Question

How to print the mysql error ? If direct php , we can use mysql_error(). But i dont know how to find here.?? plz reply as soon as possible
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 06:42 PM
Member
 
Join Date: Jan 2008
Location: chicago
Posts: 98
Default

Quote:
Originally Posted by murugesanme View Post
How to print the mysql error ? If direct php , we can use mysql_error(). But i dont know how to find here.?? plz reply as soon as possible
You need to enable error reporting in your bootstrap file. This can be done with the following code:

PHP Code:
// Error Reporting
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors''on');

// format error messages for better readability. Next 2 lines are optional
ini_set('error_prepend_string''<pre>');
ini_set('error_append_string''</pre>'); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 05:40 PM
mgordon's Avatar
Junior Member
 
Join Date: Sep 2007
Location: Stockholm, Sweden
Posts: 16
Send a message via Skype™ to mgordon
Default

Quote:
Originally Posted by notrub225 View Post
I know Zend_Db_Table allows you to make joins but in the Zend_Db_Table::find() and Zend_Db_Table_Row::_getWhereQuery() function it would be good to have the primarys with the table name so that you don't an error when you have two tables with the same key.

Example:
Code:
"SELECT * FROM table_1 LEFT JOIN table_2 ON table_1.table_2_id = table_2.id WHERE id = '123'"
If table_2 has id as primary key then this will cause an error
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 08:16 PM
xentek's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 111
Default

The methods to create your queries are more convenience functions and to give you a standard interface so that changes in syntax per DB Brand can be normalized and make your app more portable, if you wanted to say, switch from MySQL to Oracle.

However, since no DB is going to let you get away with amigious column names, such as what you described, the onnus is on the developer to pass the table name with the column name when getting into situations where the field could be ambigious, which with the ZF is going to be through the fetch* style, and not find. Find is only for single table look ups by primary key... in other words not for joined tables and other complex queries.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 08:21 PM
mgordon's Avatar
Junior Member
 
Join Date: Sep 2007
Location: Stockholm, Sweden
Posts: 16
Send a message via Skype™ to mgordon
Default

Yeah, I get that... but why not add the table name into the query? What's the catch? Why not?

Sometimes you create an app and u want to make it slightly more complex and a join can add valuable data in a rather seemless way. This would enable that functionality in a fast and easy way that's very cost-effective.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 05:58 AM
Member
 
Join Date: Feb 2008
Posts: 46
Question

Thanks. I have done that already. But what i want is, how to display MYSQL ERRORS.? While getting error, ZF send the page to Its error.phtml page and calles the Error controller automatically. So, i could not able to find the mysql error and to debug it.

-- Mugesh
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-29-2008, 01:55 PM
xentek's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 111
Default

@mgordon

Well, if you need to move from find to fetchAll, the amount of refactoring would be kind of low, since you would already have to modify it a bit to do the join anyway.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-29-2008, 01:59 PM
xentek's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 111
Default

@mugesh

Which errors are you looking for? Connection issues can be caught and displayed with a try/catch block around that code (we do it in our bootstrap).

As for other DB errors, if you catch the right Exception object (Zend_DB_Exception, etc) you should be able to get the right error. Zend_Exception should be your last (default) catch block, so that general/other errors can be caught at that point.

HTH,
-e
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 07:08 PM.