【问题标题】:Joomla 2.5 Trying to get property of non-object error [duplicate]Joomla 2.5试图获取非对象错误的属性[重复]
【发布时间】:2012-10-22 05:59:30
【问题描述】:

可能重复:
Trying to get property of non-object in

我在为 joomla 2.5 开发自定义组件时遇到了一个问题。

我通过 MVC 结构从我的数据库中提取数据。一切正常,除非查询没有返回任何结果。

我的视图中出现Trying to get property of non-object 错误。

有没有什么办法可以阻止这种情况并向用户显示没有任何结果的好消息?

模型部分:

$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;

查看:

    // 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>

太棒了!我用下面的方法试过了,对我有用:

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

//DATA

}

记录在案:

填充数组的 print_r:

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 ) ) 

还有一个空的:

stdClass Object ( [id] => 0 ) 

虽然我之前错过了一些东西,但在模型中我将限制变量调整为“0”而不是默认的“5”进行测试。

如果我把它放回“5”,当查询没有返回任何结果时,我会得到一个 array_splice 错误。在有数据的情况下完美运行。

也许我在这里也做错了什么?

我的模型中的一段代码:

$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;
}

当没有结果和限制设置为5时我认为的错误:

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

Print_R 没有返回结果,但我认为这很明显。

【问题讨论】:

  • 当没有结果时,default.php 中的哪一行(或者更具体的说是哪一行)实际上抛出了Trying to get property of non-object 错误?
  • 使用该死的SEARCH

标签: joomla joomla-extensions


【解决方案1】:

检查对象是否有数据。您可以使用以下组合

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

您可以使用类似的东西(如果行数为0,则将对象设置为null)。

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

你可以检查$this-&gt;gallery是否是一个对象。

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

根据结果集明智地选择上述内容。如果您可以发布print_r,我可以确切地告诉您您必须使用什么。

关于问题的更新

警告:array_splice() 期望参数 1 是数组,给定对象 在

它可能不起作用的原因是因为$this-&gt;_gallery 可能不是一个数组。

stdClass Object ( [id] => 0 )

要检查某个东西是否是一个数组,你可以这样做:

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

如果是数组,则可以调用array_slice()

希望这会有所帮助。如果您有任何问题,请在此处提问。

【讨论】:

  • 太棒了!我已经尝试过了,它可以正常工作,尽管还有一个额外的错误。您可以在上面我编辑的问题的底部找到它。
  • 检查更新的答案
  • 感谢您的回复,我现在明白出了什么问题。但问题出在我猜的模型代码中。当查询返回数据时,它会从中生成一个数组,当它不返回数据时,它不会从中生成一个数组。有没有办法在我的代码的模型部分解决这个问题?
  • 正如我所说,如果 $this->_gallery 不是数组,请跳过调用 array_slice() 的位置
  • 搞定了!非常感谢您的帮助!
猜你喜欢
  • 2013-01-21
  • 1970-01-01
  • 2016-07-16
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多