Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a yii model without a database table

Tags:

php

yii

i created a yii model that has no database table, but the problem now is, whenever I tried to call the model function within a view file, it doesn't get recognize

class blah extends CActiveRecord()

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return '{{UNVETTED}}';
    }

    public function sayHello()
    { 
         echo "hello world";
    }
}

while inside the view i called it like this

blah::model()->sayHello() , and then I got a white page of death, why is that?

like image 704
sasori Avatar asked Aug 24 '12 03:08

sasori


People also ask

What is model yii?

Yii implements two kinds of models: Form models and active records. They both extend from the same base class, CModel. A form model is an instance of CFormModel. Form models are used to store data collected from user input. Such data is often collected, used and then discarded.

What is ORM in yii?

Yii Query Builder offers an object-oriented method for building SQL queries, which helps reduce risk of SQL injection attacks. And Yii Active Record (AR), implemented as a widely adopted Object-Relational Mapping (ORM) approach, further simplifies database programming.

How does Yii Framework work?

Yii framework uses “Gii” tool, which is a web-based code scaffolding tool that is used to create code quickly. Using this, we can create templates in models, controllers, forums, modules, extensions, CRUD controlled actions, and views.

What is controller in yii?

Controllers are part of the MVC architecture. They are objects of classes extending from yii\base\Controller and are responsible for processing requests and generating responses.


2 Answers

If you want to create Yii model without a table it can be created using CFormModel. You have define model variables, their rules and attribute label.

Example code of a Change Password Form created using CFormModel.

<?php

class ChangePassword extends CFormModel 
{

    public $password;
    public $verifyPassword;
    public $currentPassword;    

    public function rules() {

            $rules[] = array('currentPassword', 'safe');
            $rules[] = array('currentPassword', 'required');            
            $rules[] = array('password, verifyPassword', 'required');
            $rules[] = array('verifyPassword', 'compare', 'compareAttribute' =>'password', 'message' => 'Retyped password is incorrect');

            return $rules;
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
            return array(
                    'password'=>'New password',
                    'verifyPassword'=>'Retype your new password',
                    'currentPassword'=>'Your actual password',
            );
    }

}
like image 56
Onkar Janwa Avatar answered Oct 18 '22 03:10

Onkar Janwa


You dont need to use CActiveRecord if your model is not associated with a database table. Use CModel or CFormModel instead

BTW, fix this:

class blah extends CActiveRecord()

to

class blah extends CActiveRecord {

I think that's why you are getting a white page.

like image 45
Nemoden Avatar answered Oct 18 '22 03:10

Nemoden