PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 8, 2022

[FIXED] How to extend cactiverecord

 March 08, 2022     extends, model, yii     No comments   

Issue

I want add aditional methods in CActiveRecord, but if class Project_Model extends CActiveRecord {} get error

The table "Project_ActiveRecord" for active record class "Project_ActiveRecord" cannot be found in the database.

I want create simple structure: CActiveRecord->Project_ActiveRecord (only extend methods)->Table (real table).

How can do this?


Solution

The error is clear: You don't have a table named Project_ActiveRecord in your DB!

If Project_Model is going to be the base model for your others active record models then you should do something like:

//A base classe example that has a behavior shared by all the inherited class
abstract class Project_Model extends CActiveRecord
{

    public function behaviors(){
        return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'setUpdateOnCreate' => true
            ),
        );
    }

}

And then you can declare the other class that will have a table in the db:

class YourClass extends Project_Model
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Token the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'YourClassTable';
    }
    ...
}

Then you shoudl never call the class Project_Model (this is why I put the keyword abstract) in your code, you have to call the inherited classes that have an existing table in the db!



Answered By - darkheir
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing