Undermine2k
Undermine2k

Reputation: 1491

yii cant create new instance of class

In my view im trying to do :

$accountLogin = new AccountLogin;

which should create a new instance of that class which is defined in my vendor libraries.

class AccountLogin extends CFormModel
{
..
}

But I'm getting unable to open stream('AccountLogin').

What am I doing wrong? Is there somewhere I am supposed to specify where the directory is located at?

Upvotes: 0

Views: 165

Answers (1)

Ahmad Samilo
Ahmad Samilo

Reputation: 914

You have to import your class first .

the beast and easy way to import any class in yii is put it in component like these :

Class:

class RegionSingleton extends CApplicationComponent
{
    private $_model=null;


    public function setModel($id)
    {
        $this->_model=Region::model()->findByPk($id);
    }

    public function getModel()
    {
        if (!$this->_model)
        {
            if (isset($_GET['region']))
                $this->_model=Region::model()->findByAttributes(array('url_name'=> $_GET['region']));
            else
                $this->_model=Region::model()->find();
        }
        return $this->_model;
    }

    public function getId()
    {
        return $this->model->id;
    }

    public function getName()
    {
        return $this->model->name;
    }
}

And include this class in config main then you can call it in fast way in all your app:

'components'=>array(
        'region'=>array('class'=>'RegionSingleton'),
         ...
        )

Now, we can call it like this :

Yii::app()->region->model;

for have the model, or also

Yii::app()->region->id

for retrieve the id.

We can also set the model by using

Yii::app()->region->setModel($id)

Reference : link1

Upvotes: 2

Related Questions