JTable $row->store() not working

General questions relating to Joomla! 1.5 There are other boards for more specific help on Joomla! features and extensions.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Locked
rkrkrk
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Oct 07, 2010 2:16 pm

JTable $row->store() not working

Post by rkrkrk » Thu Oct 07, 2010 2:28 pm

This is probably something really stupid that I've done but I'd really appreciate it if anyone could help me out. Inside my model I have the following:

Code: Select all

    /**
    * Gets the display date
    * @return date The display date associated with a particular piece of content
    */
    function getDisplayDate($content_id)
    {
		$db =& JFactory::getDBO();
        $query = 'SELECT displaydate' .
				 ' FROM #__news_display_date' .
				 ' WHERE content_id = '. $content_id .
				 ' ORDER BY displaydate DESC';
		$db->setQuery($query);
		$displaydate = $db->loadResult(); 
		
		// If there is no display date for this particular piece of content
		// insert the publish date and return it
		if (empty($displaydate)) {
			// Get the published date
			$query = 'SELECT publish_up FROM #__content WHERE id = ' . $content_id;
			$db->setQuery($query);
			$publishdate = $db->loadResult();
			
			// Set the display date to the publish date
			$this->setDisplayDate($content_id, $publishdate);
			
			return $publishdate;
		}
		else		
			return $displaydate;
    }

	/**
    * Sets the display date
    * @return bool true if it was entered successfully
    */
	function setDisplayDate($content_id, $date)
	{
		// Include and instantiate the table class
		JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_news'.DS.'tables');
		$row =& JTable::getInstance('NewsDisplayDate', 'Table');
		
		// Put the data into an associative array
		$data = array('content_id' => $content_id, 'displaydate' => $date);
		
		// Match the associated items of the array with member variables of the newsdisplaydate table class
		if (!$row->bind($data)) 
			return JError::raiseWarning( 500, $row->getError() );
		
		// Store the object in the table
		if (!$row->store()) 
			return JError::raiseError(500, $row->getError());
		
		return true;
	}
Now, if I print_r out $row before and after the bind I can see that the data I need is appearing successfully. This is the output:

Code: Select all

Before:
TableNewsDisplayDate Object ( [id] => 0 [displaydate] => 1970-01-01 00:00:00 [content_id] => 0 [_tbl] => #__news_display_date [_tbl_key] => content_id [_db] => JDatabaseMySQL Object ( [name] => mysql [_nullDate] => 0000-00-00 00:00:00 [_nameQuote] => ` [_sql] => SELECT publish_up FROM jos_content WHERE id = 948 [_errorNum] => 0 [_errorMsg] => [_table_prefix] => jos_ [_resource] => Resource id #26 [_cursor] => Resource id #163 [_debug] => 0 [_limit] => 0 [_offset] => 0 [_ticker] => 0 [_log] => Array ( ) [_utf] => 1 [_quoted] => Array ( ) [_hasQuoted] => [_errors] => Array ( ) [debug] => 0 ) [_errors] => Array ( ) ) 

After:
TableNewsDisplayDate Object ( [id] => 0 [displaydate] => 2010-07-19 00:00:00 [content_id] => 948 [_tbl] => #__news_display_date [_tbl_key] => content_id [_db] => JDatabaseMySQL Object ( [name] => mysql [_nullDate] => 0000-00-00 00:00:00 [_nameQuote] => ` [_sql] => SELECT publish_up FROM jos_content WHERE id = 948 [_errorNum] => 0 [_errorMsg] => [_table_prefix] => jos_ [_resource] => Resource id #26 [_cursor] => Resource id #163 [_debug] => 0 [_limit] => 0 [_offset] => 0 [_ticker] => 0 [_log] => Array ( ) [_utf] => 1 [_quoted] => Array ( ) [_hasQuoted] => [_errors] => Array ( ) [debug] => 0 ) [_errors] => Array ( ) )
The default values I put into the table class extending JTable is there before, and afterwards they have been replaced with the values in $data.

About an hour ago this thing was actually storing things into the table too. And then it broke and now nothing is saved into the newsdisplaydate table at all. I think I tried deleting the id row in the newsdisplaydate table and changing the pk to content_id and it stopped working after that, but I might be wrong. Of course I dropped the table and recreated it again with exactly the same fields as before the break and it still didn't work, so perhaps it wasn't that after all. I reset apache/mysql too. Hmm. Yes, I'm not sure why it thinks $row->store is working, it's not raising an error. Nothing is going into that table.

Does anybody know why? Thank you.

rkrkrk
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Oct 07, 2010 2:16 pm

Re: JTable $row->store() not working

Post by rkrkrk » Fri Oct 08, 2010 7:05 am

I didn't manage to find out what was wrong with the above code or what I did to make it stop working. JTable sounds as if it's a bit ropy in general and it's better to query the database directly. So I changed it to this and it worked perfectly:

Code: Select all

    /**
    * Gets the display date
    * @return date The display date associated with a particular piece of content
    */
    function getDisplayDate($content_id)
    {
		$db =& JFactory::getDBO();
        $query = 'SELECT displaydate' .
				 ' FROM #__news_display_date' .
				 ' WHERE content_id = '. $content_id .
				 ' ORDER BY displaydate DESC';
		$db->setQuery($query);
		$displaydate = $db->loadResult(); 
		
		// If there is no display date for this particular piece of content
		// insert the publish date then return it
		if (empty($displaydate)) {
			// Get the published date
			$query = 'SELECT publish_up FROM #__content WHERE id = ' . $content_id;
			$db->setQuery($query);
			$publishdate = $db->loadResult();
			
			// Set the display date to the publish date
			$this->setDisplayDate($content_id, $publishdate);
			
			return $publishdate;
		}
		else		
			return $displaydate;
    }

	/**
    * Sets the display date
    * @return bool true if it was entered successfully
    */
	function setDisplayDate($content_id, $displaydate)
	{
		// Get the database
		$db =& JFactory::getDBO();
		
		// Select the entry from the table
		$query = 'SELECT * FROM #__news_display_date WHERE content_id = ' . $content_id;
		$db->setQuery($query);
		$displayDateRow = $db->loadRow();

		// If it is empty insert it, else update it 
		if(!empty($displayDateRow)) {
			$query = 'UPDATE #__news_display_date SET displaydate = \'' . $displaydate . '\' WHERE content_id = ' . $content_id;
		 else
			$query = 'INSERT INTO #__news_display_date (displaydate, content_id) VALUES (\'' .  $displaydate . '\', ' . $content_id . ')';
		
		// If it doesn't work come back false and print out the error
		$db->setQuery($query); 
		if(!$db->query()) {
			echo $this->_db->getErrorMsg();
			return false;
		}
		
		return true;
	}
I hope that helps somebody.


Locked

Return to “General Questions/New to Joomla! 1.5”