[SOLVED] how to call a function

how doe i call the loadCity() function located in de CityController.php from out of the actionEdit.php.

if i do an echo in de loadCity() it wil not displays. so the function is never called. the page wil not display an error

thank you

Filestructure:



private_html


--application


----controller


------city


--------actionEdit.php


------CityController.php


--framework


public_html


-- index.php


actionEdit.php



<?php





class actionEdit extends CAction


{


    public function run()


    {


        //grab city


        $row = $this->getController()->loadCity(); //how to call this method





        //render view


        $this->getController()->render('edit', array('city' => $row));


    }


}


CityController.php



<?php





class CityController extends CController


{


    public $_city;


    


	/**


	 * Declares class-based actions.


	 */


	public function actions()


	{


        return array(


            'index' => 'application.controllers.city.actionList',


            'edit' => 'application.controllers.city.actionEdit',


        );


    }


    


    public function loadCity($id = NULL)


    {


        if ($this->_city === NULL)


        {


            if ($id !== NULL || isset($_GET['id']))


            {


                $this->_city = Citi::model()->findbyPk($id !== NULL) ? $id : $_GET['id'];


            }


        }


        return $this->_city;


    }


}


Hi,

it should be:



$this->_city = Citi::model()->findByPk(($id !== NULL) ? $id : $_GET['id']);


Set your PHP to show all errors and warning (you can do that in php.ini)

PS

Citi, this model name is valid (not City)?

What about refactoring your loadCity() and put it say in the model (just an idea!).

but he has also written “if i do an echo in de loadCity() it wil not displays” so I guess the problem could also be another (including the above error).

Whats your url when you try to use the edit action?

it should be something like this:

index.php/city/edit/8

when you set also the approriate settings in the main.php urlManager setting.

Don't know if you have created your classes via yiiconsole with the crud and call the wrong action?

Ok, try this:



public function loadCity($id=null)


{


	if($this->_city===null)


	{


		if($id!==null || isset($_GET['id']))


			$this->_city=Citi::model()->findbyPk($id!==null ? $id : $_GET['id']);


		if($this->_city===null)


			throw new CHttpException(500,'The requested city does not exist.');


	}


	return $this->_city;


} 

thank guys! it was a mix.

the typo Citi, i didn't set the rules in the main.php. i thought that it was not necessary for a simple url like city/edit/11.

i moved the function loadCity() to the model and i call the function with

$row = City::model()->loadCity();