Problem With Custom Class

Hi all,

I found the yii framework three months ago, and I must say it has been a pleasure to work with, not only for it’s power and ease of use, but for it’s community.

Recently, I started my first serious project with yii, and I’m facing a problem I could not solve.

I’ve created a portlet, and have located it in /protected/components. The portlet is named “Apercibimientos”.

Then in a view I use:




<?php

    $this->widget('Apercibimientos');

?>



Everything works great when I’m in my local environment, but when the code runs on the server, and visit the corresponding action, I get a generic error “500 Internal Server Error”.

The funny thing is, that if instead of calling the portlet, in the view I use the following:




<?php

    $this->beginWidget('zii.widgets.CPortlet', array(

        'title'=>'Apercibimientos',

        'titleCssClass'=>''

    ));

?>


<?php // here I put the code that renders the content ?>


<?php $this->endWidget() ?>



Then it works as expected.

First I thought it would be a problem caused by using a class that extends CPortlet, so I changed "Apercibimientos" to be a simple class. Then in my controller I used:




$apercibimientos = new Apercibimientos();

$this->render('dashboard', array('apercibimientos'=>$apercibimientos));



And in my view:




<?php

    $this->beginWidget('zii.widgets.CPortlet', array(

        'title'=>'Apercibimientos',

        'titleCssClass'=>''

    ));

?>


<?php $apercibimientos->renderContent(); ?>


<?php $this->endWidget() ?>



Again, it worked locally but not in the production server.

The only thing I can think that is different on the server, is that modrewrite is not enabled and AllowOverride is set to none, so the .htaccess file is being ignored (I have no access to change this).

Any help would be greatly appreciated.

Thanks, Victor.

Seems your implementation of your Apercibimientos portlet is not correct, but the one of the ‘simple class’ is ok.

The internal server error should be logged somewhere (apache log) - depends on php.ini settings.

What’s the error message?

Try to add




ini_set('display_errors',1);

ini_set('display_startup_errors',1);

error_reporting(E_ALL);



at the top of your index.php

Hi, thanks for your prompt reply!

At first I thought it might be a mistake in implementing the portlet "Apecibimientos" but, as I wrote in my first post, both the implementation of the portlet, and the simple class, work both in the local environment. But any work on the server.

The portlet code is as follows:




<?php

Yii::import('zii.widgets.CPortlet');


class Apercibimientos extends CPortlet

{


    public function init()

    {

        $this->title='Apercibimientos';

        parent::init();

    }


    private function getData()

    {

        $opApercibimientos = Yii::app()->db->createCommand()

            ->select('IF( estado = "I", "INFORMADO", "RESUELTO" ) "estado", COUNT( * ) "subtotal", (

              SELECT COUNT( * )

                FROM roc_apercibimiento

              ) "total"')

            ->from('roc_apercibimiento')

            ->group('estado')

            ->queryAll();


        return $opApercibimientos;

    }


    public function renderContent()

    {

        $aper = array();

        $form = '';

        $content = "";

        $table = "<table class='table table-striped table-bordered table-condensed'>";

        $table.= "<thead>";

        $table.= "<th>Estado</th>";

        $table.= "<th>Total</th>";

        $table.= "</thead>";

        $table.= "<tbody>";

        if ( count($this->getData()) > 0 )

        {

            foreach ($this->getData() as $ap)  {

                $aper[$ap['estado']] = $ap['subtotal'];

                $table.= "<tr>";

                $table.= "<td>".$ap['estado']."</td>";

                $table.= "<td>".$ap['subtotal']."</td>";

                $table.= "</tr>";

            }

        }

        else

        {

            $table.= "<tr>";

            $table.= "<td>INORMADO</td>";

            $table.= "<td>0</td>";

            $table.= "</tr>";

            $table.= "<tr>";

            $table.= "<td>RESUELTO</td>";

            $table.= "<td>0</td>";

            $table.= "</tr>";

        }

        $table.= "</tbody>";

        $table.= "</table>";


        $content.= $table;

		$content.= ( count($this->getData()) > 0 ) ? "TOTAL: " . $this->getData()[0]['total'] : "TOTAL: 0";

        if ( count($this->getData()) > 0 )

        {

            $widget = $this->widget('ext.widgets.google.XGoogleChart',array(

                'type'=>'pie',

                'title'=>'Apercibimientos',

                'data'=>$aper,

                'size'=>array(400,300), // width and height of the chart image

                'color'=>array('6f8a09', '3285ce'), // if there are fewer color than slices, then colors are interpolated.

            ));

            $content .= $widget;

        }

        echo $content;

    }


    protected function getTitle()

    {

        return $this->title;

    }


    protected function getClassName()

    {

        return __CLASS__;

    }

}



This works on localhost, but not in the server. I’ll try:




ini_set('display_errors',1);

ini_set('display_startup_errors',1);

error_reporting(E_ALL);



in index.php, as you suggest, and see what error messages I get. Unfortunately, I have no access to apache error log. Once again, thank you very much!!

Victor

Check capitalization on the file/call to the widget. If development on Windows and production on Linux, caps is often a problem. Linux is picky about it, windows not so much.

Hello everyone!

I finally found the solution

Joblo, I took your advice and put the following in my index.php:




ini_set('display_errors',1);

ini_set('display_startup_errors',1);

error_reporting(E_ALL);



Instead of the generic error I saw before, I got the following:

Parse error: syntax error, unexpected ‘[’ in /var/www/procarne/sroc/protected/components/Apercibimientos.php on line 63

The line in question was this:




$content.= ( count($this->getData()) > 0 ) ? "TOTAL: " . $this->getData()[0]['total'] : "TOTAL: 0";



The problem was caused by: $this->getData()[0][‘total’]

I solved it by assigning the result of the getData function to a variable, like this: $aper = $this->getData (), and then by replacing the line as follows:




$content.= ( count($aper) > 0 ) ? "TOTAL: " . $aper()[0]['total'] : "TOTAL: 0";



What I still do not understand is why it worked locally and was interpreted as a syntax error on the production server

Thank you very much to both for answering!

Victor