12

i am getting the error "Cannot refresh row as parent is missing" when I try to save. Here is my code

abstract class Webapp_Model_Resource_Db_Table_Abstract
extends Zend_Db_Table_Abstract
{
    /**
     * Save a row to the database
     *
     *
     * @param array             $info The data to insert/update
     * @param Zend_DB_Table_Row $row Optional The row to use
     * @return mixed The primary key
     */

    public function saveRow($info, $row = null)
    {         
        if (null === $row) {
            $row = $this->createRow();
        }
        $columns = $this->info('cols');
        foreach ($columns as $column) {
            if (array_key_exists($column, $info)) {
                $row->$column = $info[$column];
            }
        }

        return $row->save();
    }
}

when I call the saveRow() method, I pass in the $_POST values ($form->getValues())

I have reused this class with my other modules in the same application but now I am getting this error and I am not sure why. My table is pretty straight forward:

CREATE TABLE `news` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `headline` varchar(100) DEFAULT NULL,
  `snippet` varchar(500) DEFAULT NULL,
  `full_text` text,
  `author` varchar(100) DEFAULT NULL,
  `publish_from` date DEFAULT NULL COMMENT 'Publish date',
  `publish_to` date DEFAULT NULL COMMENT 'Take it down or mark as draft after this date',
  `datecreated` timestamp NULL DEFAULT NULL COMMENT 'First created on',
  `revised` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Timestamp for the last time it was revised',
  `draft` tinyint(1) DEFAULT '0' COMMENT 'Should not be published',
  `departments_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=214 DEFAULT CHARSET=utf8 COMMENT='Stores news articles';

Anyone know what I am doing wrong?

::::::::::::::ADDTION:::::::::::::

public function saveNews($post,$defaults = array())
    {

       //get the form
        $form = $this->getForm('article' . ucfirst($validator));
        //validate
        if(!$form->isValid($post)) {
            return false;
        }


      //get fitered values
        $data = $form->getValues();
        //apply defaults
        foreach($defaults as $col => $value) {
            $data[$col] = $value;
        }

      //get the article if it exists
        $article = array_key_exists('id', $data) ?
                   $this->getNewsById($data['id']) : null;


        return $this->saveRow($data, $article);
}
Charles
  • 50,943
  • 13
  • 104
  • 142
browndash
  • 123
  • 1
  • 6
  • 1
    Why aren't you using `Zend_Db_Table_Row_Abstract::save()`? Here: http://framework.zend.com/apidoc/core/Zend_Db/Table/Zend_Db_Table_Row_Abstract.html#save – chelmertz Aug 09 '10 at 15:28
  • @chelmertz thats what I am using. See how my class extends Zend_Db_Table_Abstract. The data is successfully inserted in the database but the code Zend_Db_Table_Row_Abstract::save() also calls _refresh() internally and that is when I get the error – browndash Aug 09 '10 at 16:38
  • I still doesn't understanding why are you using saveRow() instead of save(). It's ok, the error persist, but first you sould remove this, right? Also, have you set departments_id when saving? Probably the error is with the relationship. Post the code of your controller too. – Keyne Viana Aug 09 '10 at 17:17
  • But I am using save(). The only thing that saveRow() does it decide if it is an update or insert. My controller is very thin, all it does is pass the post values to the model class above: here is the essential line from the controller: $this->_model->saveNews($this->_request->getPost()). saveNews has been added above. there is no relationship/fk in the table. and yes departments_id is set – browndash Aug 09 '10 at 17:43
  • You're right, there is nothing with relationships and departaments_id. The problem is when inseting the row, it seems that the primarykey is not returned to the _refresh method when he calls $where = $this->_getWhereQuery(); Maybe if you print this $where clausule you can see what's happening. You can temporaly edit Zend_Db_Table_Row_Abstract with that. – Keyne Viana Aug 09 '10 at 18:57

5 Answers5

12

When you pass an empty value for the primary key, Zend seems to return this value instead of the inserted auto-increment value - even though a new row is created properly with an auto-increment value, the inserted value will not be returned.

Maybe your problem is related to this. If so, try unsetting the id field prior to saving.

Ede
  • 136
  • 2
  • I can confirm that this is a cause. I was using Zend in a slightly non-standard way as part of a project. I ended up passing an $array with a null 'id' column. While it would write, it would return an error as described. You actually have to unset() the $array['id'] in order for Zend to not complain. – elb98rm Aug 29 '14 at 10:36
3

You have to tell DbTable that there is a Auto Incrementing Primary Key by setting $_sequence either to true or the Sequence Name.

15.5.4.1. Using a Table with an Auto-incrementing Key

Oli
  • 103
  • 8
Martin Trenker
  • 167
  • 1
  • 8
0

In my case the problem was missing AUTO_INCREMENT.

pmaruszczyk
  • 2,157
  • 2
  • 24
  • 49
0

Check your $info array. Probably you have some empty value for your primary key there. So array_key_exists($column, $info) returns true and you assign an empty primary key to your row. And this causes the error as the row with this key does not exist.

try

if (array_key_exists($column, $info) and $column != 'YOUR_PRIMARY_KEY_NAME') 
{
       $row->$column = $info[$column];
}
Victor
  • 750
  • 9
  • 9
-1

Could you post the function:

$this->getNewsById($id)

There is your problem...

Chris
  • 884
  • 5
  • 8
  • I dont think so, there is no id when I am creating a new row so array_key_exists('id', $data) returns false and getNewsById is not run. Therefore the value for $article =null and that is passed to saveRow(), I have checked this. So I think code for getNewsById is irrelevant since it is not run anywhere – browndash Aug 10 '10 at 06:29
  • The problem with "Cannot refresh row as parent is missing" comes when you update a row that's not found on the Database (either it's failing on insert or the primary key it's not setup correctly). – Chris Aug 10 '10 at 16:06