views:

33

answers:

1

I have a static method 'findAll' on a model which basically gets all rows with certain criteria. This method works fine and I can call it using:

$m::findAll();

Where $m is the model name as a variable. I can output this and it returns correct results. However, when assigning this to a variable in the Zend_View object, as:

$this->view->viewvariable = $m::findAll();

I get the error:

Zend_Db_Table_Exception: Too many columns for the primary key

Any ideas why?

Find all function:

final public static function findAll($where = false, array $options = array()) {
  $object = new static();

  if (!empty($options)) $options = array_merge($object->options, $options);
  else $options = $object->options;

  $run = $object->buildDefaultSelect($where, $options);
  $rows = $run->fetchAll();
  if ($options['asObject'] == true) {
   $result = array();
   foreach ($rows as $r) {
    $class = new static();
    $class->setInfo($r);
    $result[] = $class;
   }
   return $result;
  } else {
   if (count($rows) > 0) return $rows;
   else return array();
  }
 }

Note: This function works fine everywhere apart from when assigning to a view variable. If I run the below (not assigning it to a view variable), it shows the correct array data.

   var_dump($m::findAll($module['where'], $module['options']));
   exit;

In my view (I have replaced the actual name with viewvariable for the sake of this post):

<?php foreach($this->viewvariable as $item) { ?>
//Do some echoing of data in $item
//Close foreach
+1  A: 

I doubt the issue is with Zend_View. It's hard to tell without seeing your code, but my guess is that findAll() is using the Zend_Table_Db find() function incorrectly.

To my knowledge, the only place that throws that exception is the find() function on Zend_Db_Table_Abstract.

Perhaps, inside the findAll() function (or in a function it calls) you're doing one of these:

$zendDbTable->find(1,2) //is looking for a compound key
$zendDbTable->find(array(1,2)) //is looking for two rows

When you really want the opposite.

Tim Lytle
I don't use the find function anywhere in my code.... see updated post for function
Ashley
But something is calling it, possibly, `$run->fetchAll()`. `Zend_View` won't be throwing a `Zend_Db_Table_Exception`. My guess is you can't run `$m::findAll()` without any arguments (your sample working code adds arguments).
Tim Lytle
Thanks for the update but it produces a parse error. Running findAll without vars is ok.
Ashley
Also, updated the answer to reference the only place that exception is thrown (at least in the current version of Zend framework) - it's `Zend_Table_Db` `find()`
Tim Lytle
I have found the problem, kinda, it's due to several functions automatically being called from my created objects. I commented out the find function in the abstract and tracked it down. As you're answer pointed me to this file, you have the answer :) Thanks for the help
Ashley
@Ashley - Yup, knew it wasn't something in `Zend_View` - aren't detailed exceptions informative? :-)
Tim Lytle
The problem was that the exception was killing the actual error that was causing the problem (it was only a notice.. not sure why). Not so handy exception handling from Zend... :(
Ashley