Search code examples
joomlajoomla-extensions

Joomla 2.5 Trying to get property of non-object error


Possible Duplicate:
Trying to get property of non-object in

I've come upon a problem when developing a custom component for joomla 2.5.

I'm pulling data out of my database through a MVC structure. Everything is working fine, except when the query doesn't return any results.

I'm getting a Trying to get property of non-object error in my view.

Is there someway I can prevend this and show users a nice message that there aren't any results?

Model part:

$this->_gallery = array();
            // Load the data
        if (empty( $this->_gallery )) {
            $query = ' SELECT #__gallery.id, user_id, merk, type, name, bouwjaar, vermogen, #__gallery_kleur.kleur, transmissie, #__gallery_benzine.soort AS brandstof, cilinders, motorinhoud, gewicht, link FROM #__gallery LEFT JOIN #__gallery_merk ON #__gallery.merk_id = #__gallery_merk.merk_id LEFT JOIN #__gallery_type ON #__gallery.type_id = #__gallery_type.type_id LEFT JOIN #__gallery_autoinfo ON #__gallery.id = #__gallery_autoinfo.galleryid LEFT JOIN #__gallery_kleur ON #__gallery_autoinfo.kleur = #__gallery_kleur.kleurid LEFT JOIN #__gallery_benzine ON #__gallery_autoinfo.brandstof = #__gallery_benzine.benzineid LEFT JOIN #__users ON #__gallery.user_id = #__users.id LEFT JOIN (SELECT * FROM #__gallery_foto ORDER BY #__gallery_foto.hoofdfoto ASC) as #__gallery_foto ON #__gallery.id = #__gallery_foto.galleryid GROUP BY #__gallery.id ';
            $this->_db->setQuery( $query );
            $this->_db->query();
            $num_rows = $this->_db->getNumRows();

            $this->_gallery = $this->_db->loadObjectList();
        }
        if (!$this->_gallery) {
            $this->_gallery = new stdClass();
            $this->_gallery->id = 0;
        }
    $this->_total = count($this->_gallery); 

    if ($num_rows == 0) {
    if ($this->getState('limit') > 0) {
 $this->_gallery    = array_splice($this->_gallery, $this->getState('limitstart'), $this->getState('limit') );
    }
    }
     return $this->_gallery;

View:

    // No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla view library
jimport('joomla.application.component.view');
jimport( 'joomla.html.pagination' );

/**
 * HTML View class for the HelloWorld Component
 */
class GalleryViewGallery extends JView
{
    // Overwriting JView display method
    function display($tpl = null) 
    {
        $gallery = $this->get( 'Gallery' );
        $this->assignRef( 'gallery',    $gallery );

        $this->items = $this->get('Gallery');
        $this->pagination = $this->get('Pagination'); 

        // Check for errors.
        if (count($errors = $this->get('Errors'))) 
        {
            JError::raiseError(500, implode('<br />', $errors));
            return false;
        }
        // Display the view
        parent::display($tpl);
    }
}

default.php:

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
//$document = &JFactory::getDocument(); $document->addStyleSheet('components'.DS.'com_gallery'.DS.'css'.DS.'gallery.css');
$document = &JFactory::getDocument(); $document->addStyleSheet('components/com_gallery/css/gallery.css');
?>
<p>
In onderstaande overzicht wordt overzicht van galleries getoond, ben op je zoek naar iets specifieks ?. Gebruik dan het zoekscherm aan de linkerkant van deze pagina om een aangepast overzicht te tonen.
</p>
<?php
$count = '1';

foreach($this->gallery as $gallery){
if($count == '0'){
    echo 'Geen resultaat';
}else{
?>
<div class="gallery_wrapper">
<?php
if($gallery->merk OR $gallery->type > Null){
echo "<div class=\"gallery_merktype\">";
echo "<a href='gallerie?view=gallerydetail&amp;gallery=" . $gallery->id . "' target='_self'>";
echo "<H1>";
echo $gallery->merk;
echo " ";
echo $gallery->type;
echo "</H1>";
echo "</a>";
echo "</div>";
}
//Weergeven afbeelding, bepaling maximale hoogte en breedte gebeurt in CSS.
if($gallery->link > Null){
echo "<div class=\"gallery_foto\">";
echo "<div class=\"gallery_foto_clip\">";
echo "<a href='gallerie?view=gallerydetail&amp;gallery=" . $gallery->id . "' target='_self'><img src='../galleryimage/thumb/" . $gallery->link . "' width='200px' alt='Afbeelding'/></a>";
echo "</div></div>";
}               
?>
<div class="gallery_wrapper_info">
<?php
//Weergeven gallery gegevens.
if($gallery->bouwjaar > Null){
echo "<div class=\"gallery_info_title\">Bouwjaar:</div>";
echo "<div class=\"gallery_info_values\">";
echo $gallery->bouwjaar;
echo "</div>";
}
if($gallery->vermogen > Null){
echo "<div class=\"gallery_info_title\">Vermogen:</div>";
echo "<div class=\"gallery_info_values\">";
echo $gallery->vermogen;
echo "</div>";
}
if($gallery->kleur > Null){
echo "<div class=\"gallery_info_title\">Kleur:</div>";
echo "<div class=\"gallery_info_values\">";
echo $gallery->kleur;
echo "</div>";
}
if($gallery->transmissie > Null){
echo "<div class=\"gallery_info_title\">Transmissie:</div>";
echo "<div class=\"gallery_info_values\">";
echo $gallery->transmissie;
echo "</div>";
}
if($gallery->brandstof > Null){
echo "<div class=\"gallery_info_title\">Brandstof:</div>";
echo "<div class=\"gallery_info_values\">";
echo $gallery->brandstof;
echo "</div>";
}               
if($gallery->cilinders > Null){
echo "<div class=\"gallery_info_title\">Cilinders:</div>";
echo "<div class=\"gallery_info_values\">";
echo $gallery->cilinders;
echo "</div>";
}
if($gallery->motorinhoud > Null){
echo "<div class=\"gallery_info_title\">Motorinhoud:</div>";
echo "<div class=\"gallery_info_values\">";
echo $gallery->motorinhoud;
echo "</div>";
}
if($gallery->gewicht > Null){
echo "<div class=\"gallery_info_title\">Gewicht:</div>";
echo "<div class=\"gallery_info_values\">";
echo $gallery->gewicht;
echo "</div>";
}
?>
<div class="gallery_bekijken">
<H2><a href="index.php/gallerie?view=gallerydetail&amp;gallery=<?php echo $gallery->id; ?>" target="_self">Bekijk Gallery</a></H2>
</div>
</div>

</div>


<?php   
}
}
?>

<form method="post" name="limiet">
<?php echo $this->pagination->getPagesLinks(); ?>
<center>
<?php echo $this->pagination->getLimitbox(); ?>
</center>
</form>

Great!, I've tried it in the way below and it worked for me:

if(is_object($this->gallery)){
echo "No result";
}else{

//DATA

}

For the record:

A print_r of a filled array:

Array ( [0] => stdClass Object ( [id] => 146 [user_id] => 861 [merk] => Seat [type] => Leon [name] => Danny [bouwjaar] => 2004 [vermogen] => 190 [kleur] => Geel [transmissie] => Handgeschakeld [brandstof] => Diesel [cilinders] => 4 [motorinhoud] => 1900 [gewicht] => 1210 [link] => p1793sjnq51u373r12qdobs1d2b2.jpg ) ) 

And a empty one:

stdClass Object ( [id] => 0 ) 

Although there was something I missed earlier, in the model I adjusted the limit variable to '0' instead of the default '5' for testing.

If I put it back to '5' there is a array_splice error I'm getting when the query doesn't return any results. It works perfectly when there is data.

Maybe I'm doin somethin wrong here also ?

The piece of code in my model:

$this->_gallery = array();
            // Load the data
        if (empty( $this->_gallery )) {
            $query = ' SELECT #__gallery.id, user_id, merk, type, name, bouwjaar, vermogen, #__gallery_kleur.kleur, transmissie, #__gallery_benzine.soort AS brandstof, cilinders, motorinhoud, gewicht, link FROM #__gallery LEFT JOIN #__gallery_merk ON #__gallery.merk_id = #__gallery_merk.merk_id LEFT JOIN #__gallery_type ON #__gallery.type_id = #__gallery_type.type_id LEFT JOIN #__gallery_autoinfo ON #__gallery.id = #__gallery_autoinfo.galleryid LEFT JOIN #__gallery_kleur ON #__gallery_autoinfo.kleur = #__gallery_kleur.kleurid LEFT JOIN #__gallery_benzine ON #__gallery_autoinfo.brandstof = #__gallery_benzine.benzineid LEFT JOIN #__users ON #__gallery.user_id = #__users.id LEFT JOIN (SELECT * FROM #__gallery_foto ORDER BY #__gallery_foto.hoofdfoto ASC) as #__gallery_foto ON #__gallery.id = #__gallery_foto.galleryid GROUP BY #__gallery.id ';
            $this->_db->setQuery( $query );
            $this->_gallery = $this->_db->loadObjectList();
        }
        if (!$this->_gallery) {
            $this->_gallery = new stdClass();
            $this->_gallery->id = 0;
        }
    $this->_total = count($this->_gallery); 

    if ($this->getState('limit') > 0) {
     $this->_gallery    = array_splice($this->_gallery, $this->getState('limitstart'), $this->getState('limit') );
    }
     return $this->_gallery;
}

The error in my view when there are no results and limit set to 5:

Warning: array_splice() expects parameter 1 to be array, object given in 

Print_R isn't giving back results, but this is obvious I quess.


Solution

  • check the object has data. you can use a combination of following

    isset- Determine if a variable is set and is not NULL
    
    is_null - Finds whether a variable is NULL
    
    empty — Determine whether a variable is empty
    
    is_array — Finds whether a variable is an array 
    
    is_object — Finds whether a variable is an object
    

    You can use something like(if the number of rows are 0, set the object to null).

    If(!is_null($this->gallery))
    {
    //show data
    }
    else
    {
    //show a msg
    } 
    

    You can just check if the $this->gallery is an object.

    If(is_object($this->gallery))
    {
    //show data
    }
    else
    {
    //show a msg
    }
    

    Depending on the result set choose the above wisely. If you can post a print_r I can tell you exactly what you have to use.

    UPDATE WITH RESPECT TO THE QUESTION

    Warning: array_splice() expects parameter 1 to be array, object given in

    The reason it may not work is because $this->_gallery may not be an array.

    stdClass Object ( [id] => 0 ) <-- this is an object not an array. if you are to call array_slice() function, 1st parameter should be an array.

    To check if something is an array, you can do:

    is_array($this->_gallery); // returns true or false
    

    If it is an array, you can then call array_slice().

    Hope this helps. If you have any problems ask here.