views:

881

answers:

2

This is my table structure.

http://img6.imageshack.us/img6/8730/articlek.jpg

I want to get a article row object from id with section and category names instead of section_id and category_id ,And user names instead of author_id and modified_by.

Please help me.

+1  A: 

This is outside of the scope of Zend_Db_Table. Zend_Db_Table can make it easy to get those other rows (see here) but it won't do it for automatically. You may want to check out the Zend Framework Quick Start, it talks about using the DataMapper pattern and that might be something you want to look into.

One option you do have to extend Zend_Db_Table_Row and in the init() method query the other tables for the information you need. You can specify a row class to use in your Zend_Db_Table class by overriding the rowClass variable:

protected $_rowClass = 'FooRow';

From comments:

require_once(dirname(realpath(__FILE__) . '/path/relative/to/articles/ArticleRow.php');
smack0007
I already set dependentTables and referenceMaps for tables.But I don't know how to get the appropriate result I want from Zend_Db_Table(article table).Thanks for helping
neobeacon
This is it's sql code.select sec.title as 'section', cat.title as 'category', auth.Name as 'author', modi.Name as 'modifier', art.* from article art left outer join section sec on sec.id = art.section_id left outer join category cat on cat.id = art.category_id left outer join user auth on auth.id = art.author_idleft outer join user modi on modi.id = art.modified_bywhere art.id='1'
neobeacon
I updated my answer with a potential solution. I really feel like though if need this kind of functionality then Zend_Db_Table is the wrong direction.
smack0007
This is the simplest sql code for it.SELECT article. * , section.title, category.title, user.name, u2.nameFROM articleINNER JOIN section ON article.section_id = section.idINNER JOIN category ON article.category_id = category.idINNER JOIN user ON article.author_id = user.idLEFT JOIN user u2 ON article.modified_by = u2.idWHERE article.id = '1'If I create a class extending Zend_Db_Table_Row in init(),I can not understand about protected $_rowClass = 'FooRow';.your information is very helpful for me.If you can give a link for tutorial it will be vey helpful for me.Thanks again
neobeacon
If I create a class extending Zend_Db_Table_Row in init(),I can not understand about protected $_rowClass = 'FooRow';. your information is very helpful for me.If you can give a link for tutorial it will be vey helpful for me.Thanks again
neobeacon
Here: http://framework.zend.com/manual/en/zend.db.table.row.html#zend.db.table.row.extending is an explanation of how to create your own Zend_Db_Table_Row class.
smack0007
Thanks in deep.
neobeacon
I created a class(ArticleRow) by extending Zend_Db_Table_Row_Abstract and in it's init() method I wrote the query.It will return the joined row object.And also I added "protected $_rowClass = 'ArticleRow';" to Article Zend_Db_Table class.But Zend_Loader doesn't load the "ArticleRow".I added my "ArticleRow" class as an answer(for this question) because in comments the code will be collapsed.Am I code wrong way?
neobeacon
That error comes even I call Article Table.
neobeacon
Is your models folder in your include path? If not, you can just require_once the ArticleRow class in your Articles class.
smack0007
I add "require_once 'ArticleRow.php';".But it give the same result.If file not found It must be give a "failed to open stream" warning.I only add "protected $_rowClass = 'ArticleRow';" to Article class.Do I want to add anything else for ArticleRow class.Thanks for your helping.
neobeacon
Is ArticleRow.php in the same folder as Articles.php? Use the absolute path to the file if need be. (See above)
smack0007
I add "require_once(realpath(APPLICATION_PATH . '/models/ArticleRow.php'));".But same result.Thanks in deep.
neobeacon
It's work,the case was I used autoloader for load views,models,layout " in my Article class.Thank you very much in deeeeeeeeeep.I'm really new for zend.Thank you very much again.And also this give worning because in Article_Row, I want to get article details of specifc article(by passing argument of article id).But in here,give worning "Missing argument 1 for App_Model_ArticleRow::init()".So how do I want to pass the article_id for get details of a specific article.
neobeacon
Thank you very much in deep.Zend_Db_Table_Row is really new for me,Thanks again.
neobeacon
+5  A: 

You need to use Zend_Db_Table's setIntegrityCheck() functionality:

Something like this should work:

$articleTable = new Article();
$select = $articleTable->select();
$select->setIntegrityCheck(false);
$select->from('article', 'article.*');
$select->joinLeft('user', 'article.author_id = user.id', array('author_name'=>'author'));
$select->where('article.id = 123');
$articleRecord = $articleTable->fetchRow($select);

You can check the SQL generated using:

Zend_Debug::dump($select->__toString());

Note that the returned Zend_Db_Table_Row object is read only.

Rob Allen
Thank you very much
neobeacon
Shouldn't it be:$select = $articleTable->select()->setIntegrityCheck(false);(according to the manual: http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.advanced.usage).
Stefan Gehrig
Yes, you need the setIntegrityCheck(false); Thanks!I've updated the code so that it's not misleading :)
Rob Allen
I created a class(ArticleRow) by extending Zend_Db_Table_Row_Abstract and in it's init() method I wrote the query(can see in 0 answer).But I have no idea how to get the data I wanted from Controller.I'll very grateful if anyone can give a tip for me.Thank you
neobeacon