?
Solved

Proper way to create a instance of Zend_DB_Select in Zend Framework

Posted on 2008-06-19
8
Medium Priority
?
857 Views
Last Modified: 2012-05-05
I need some advice on the "proper" way to create a zend_db_select class.  I use to have the following old code that used Db_Table_Abstract to do a fetch.    I'm trying to use Db_Select so that I can create custom queries at the same time keeping things object oriented.  Right now I'm getting the following error


Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /home/sean/Zend/workspaces/psi-inv-zf/library/Fetchshortview.php on line 4

As you can tell I'm a noob with ZF, any input would be appreciated.
// OLD CODE *************************************************************************************
 
require_once 'Zend/Db/Table/Abstract.php';
 
class Items extends Zend_Db_Table_Abstract {
	/**
	 * The default table name 
	 */
	protected $_name = 't_items';
 
} 
 
public function showallAction()
    {
    	$this->view->title = "Showing All Inventory";
	     	$items = new Items();
		$this->view->items = $items->fetchAll();	
    }
 
 
// NEW CODE ***********************************************************************
 
class Fetchshortview {
	$query = $db->select();
	$query ->from('v_itemshort', array('id','nickname','serial'));
 
}
 
 public function showallAction()
    {
    	$this->view->title = "Showing All Inventory";
	    $items = new Fetchshortview();
		$this->view->items = $items->fetchAll($query);
		
    }

Open in new window

0
Comment
Question by:sean-keys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 21824855
your showAllAction function is not inside your class. You might need to just move the curly brace.
0
 

Author Comment

by:sean-keys
ID: 21825237
I should have been more specific class Fetchshortview exists in Fetchshortview.php  and

public function showallAction()  exists in IndexController.php  

0
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 21825359
There cannot be any code directly inside a class without a function. I am actually ashamed I didn't see that at first glance..

class Fetchshortview {
        $query = $db->select();
        $query ->from('v_itemshort', array('id','nickname','serial'));
}

You could have

class Fetchshortview extends Items {
  function fetchAll() {
    $query = $this->getDefaultAdapter()->select();
    $query ->from('v_itemshort', array('id','nickname','serial'));
    return parent::fetchAll($query);
  }
}

But then you could just easier define a fetchShortList inside your Items class and not use the extra class:

function fetchShortList() {
    $query = $this->getDefaultAdapter()->select();
    $query ->from('v_itemshort', array('id','nickname','serial'));
    return $this->fetchAll($query);
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:sean-keys
ID: 21826793
I took your advice and included my function inside the Items class.

I changed "return $this->fetchAll($query);"   to " $this->fetchAll($query); "    because I have a view script running after it .    Now I get this error, I have a feeling its something simple that I'm over looking.



Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at end of input LINE 1: SELECT "t_items".* FROM "t_items" WHERE ( ^' in /usr/share/php/Zend/Db/Statement/Pdo.php:238 Stack trace: #0 /usr/share/php/Zend/Db/Statement.php(283): Zend_Db_Statement_Pdo->_execute(Array) #1 /usr/share/php/Zend/Db/Adapter/Abstract.php(405): Zend_Db_Statement->execute(Array) #2 /usr/share/php/Zend/Db/Adapter/Pdo/Abstract.php(205): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Table_Select), Array) #3 /usr/share/php/Zend/Db/Table/Abstract.php(1184): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Table_Select)) #4 /usr/share/php/Zend/Db/Table/Abstract.php(1039): Zend_Db_Table_Abstract->_fetch(Object(Zend_Db_Table_Select)) #5 /home/sean/Zend/workspaces/psi-inv-zf/application/default/models/Items.php(21): Zend_Db_Table_Abstract->fetchAll(Object(Zend_Db_Select)) #6 /home/sean/Zend/workspaces/psi-inv-zf/application/defaul in /usr/share/php/Zend/Db/Statement/Pdo.php on line 238


//START VIEW SCRIPT **************************************
 
<p><a>Displaying Short View</a></p>
<table>
<tr>
     <th>nickname</th>
     <th>serial</th>
     <th>&nbsp;</th>
</tr>
<?php foreach($this->items as $item) : ?>
<tr>
     <td><?php echo $this->escape($item->nickname);?></td>
     <td><?php echo $this->escape($item->serial);?></td>
     <td>
          <a href="<?php echo $this->url(array('controller'=>'index',
              'action'=>'edit', 'id'=>$item->id));?>">Edit</a>
          <a href="<?php echo $this->url(array('controller'=>'index',
              'action'=>'delete', 'id'=>$item->id));?>">Delete</a>
     </td>
</tr>
<?php endforeach; ?>
</table>
// END VIEW SCRIPT *************************************
 
New code
 
 public function showallAction()
    {
    	$this->view->title = "Showing All PSI Inventory";
	    $items = new Items();
		$this->view->items = $items->fetchShortList();
    }
 
 
 
 
require_once 'Zend/Db/Table/Abstract.php';
 
class Items extends Zend_Db_Table  {
	/**
	 * Returns ShortView Data
	 */
	protected $_name = 't_items';
	
	function fetchShortList() {
    $query = $this->getDefaultAdapter()->select();
	$query ->from('t_items', array('id','nickname','serial'));
    $this->fetchAll($query);
	//return $this->fetchAll($query);
 
	}
}

Open in new window

0
 

Author Comment

by:sean-keys
ID: 21832710
adding    echo $query->__toString();

Reveals that my query is somehow wrong.

SELECT "t_items".* FROM "t_items"


require_once 'Zend/Db/Table/Abstract.php';
 
class Items extends Zend_Db_Table   {
	/**
	 * Returns ShortView Data
	 */
	protected $_name = 't_items';
	
	function fetchShortList() {
  
    $query = $this->getDefaultAdapter()->select();
    $query ->from('t_items', array('id','nickname','serial'));
    $this->fetchAll($query);
    echo $query->__toString();
 
	}
}

Open in new window

0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21832913
Try using:

$query ->from(array('items' => 't_items'), array('id','nickname','serial'));
0
 

Author Comment

by:sean-keys
ID: 21833302
I ended up with this, but without your guidance I wouldn't have been able to accomplish this.
public function showallAction()
    {
    	$this->view->title = "Showing All Inventory";
	    
    	$items = new Items();
	   	$select = $items->select();
	   	$select->where('id= 6'); //just a test
		$this->view->items = $items->fetchAll($select);
 
}
 
 
require_once 'Zend/Db/Table/Abstract.php';
 
class Items extends Zend_Db_Table_Abstract  {
	/**
	 * Returns ShortView Data
	 */
	protected $_name = 't_items';
	
	
}

Open in new window

0
 

Author Closing Comment

by:sean-keys
ID: 31468922
Thanks for all of your effort.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question