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?
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.
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.
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.
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.
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',
);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With