Adding where close on Gii generated CRUD - php

I am beginner with Yii and I used GII to create a CURD table. Everything is working fine but I only want to fetch certain record from the database by putting a where clause (for example "where client gender is male). I cannot find where the data is fetched from the database in Gii's generated code and where I need to insert the WHERE clause in the code.
As you know the Gii generated Model, controller and view file. The model file is as below. My view used CGridView to generate the CRUD table.
public static function model($className = __CLASS__) {
return parent::model($className);
}
/**
* #return string the associated database table name
*/
public function tableName() {
return 'test_prefixtbl_client_local';
}
/**
* #return array validation rules for model attributes.
*/
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('client_id', 'required'),
array('client_id', 'length', 'max' => 15),
array('surname, forename', 'length', 'max' => 20),
array('title', 'length', 'max' => 6),
array('status', 'length', 'max' => 8),
array('dob', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('client_id, surname, forename, title, status, dob', 'safe', 'on' => 'search'),
);
}
/**
* #return array relational rules.
*/
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* #return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'client_id' => 'Client ID',
'surname' => 'Surname',
'forename' => 'Forename',
'title' => 'Title',
'status' => 'Status',
'dob' => 'Date of birth (yyyy-mm-dd)',
'actions' => 'Actions',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('client_id', $this->client_id, true);
$criteria->compare('surname', $this->surname, true);
$criteria->compare('forename', $this->forename, true);
$criteria->compare('title', $this->title, true);
$criteria->compare('status', $this->status, true);
$criteria->compare('dob', $this->dob, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 'dob DESC',
),
));
}

You have two ways to do your query, one using Query Builder (tutorial here) like this:
$clientLocalArray = Yii::app()->db->createCommand()
->select()
->from("test_prefixtbl_client_local")
->where("gender = :gender", array(":gender"=>$gender))
->queryAll();
Or you can use the Active Record itself (tutorial here) like this:
$clientLocalArrayObjects = ClientLocal::model()->findAllByAttributes(array(
"gender" => $gender
));
Any doubts, just ask! :)

Related

How to filter a gridview table in yii2?

I have two gridview tables currently, one shows all the data and the other i want it to show only data where id = 2. Is it possible to filter only one table without affecting the other? I know I can filter from the search model but that will affect all tables and i want it to affect only one.
Can i have two dataprovider?
This is the code in my search model:
class JobPlanningSearch extends JobPlanning
{
/**
* #inheritdoc
*/
public function rules()
{
return [
[['id', 'priority', 'employer_id', 'client_id', 'status', 'activity'], 'integer'],
[['job_description', 'impediment', 'date', 'estimated_time', 'due_date'], 'safe'],
];
}
/**
* #inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* #param array $params
*
* #return ActiveDataProvider
*/
public function search($params)
{
$query = JobPlanning::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'priority' => $this->priority,
'client_id' => $this->client_id,
'employer_id' => $this->employer_id,
'estimated_time' => $this->estimated_time,
'status' => $this->status,
'activity' => $this->activity,
//'actual' => $this->actual,
//'actual' => 1,
]);
$query->andFilterWhere(['like', 'job_description', $this->job_description]);
$query->andFilterWhere(['like', 'activity', $this->activity]);
return $dataProvider;
}
You need two dataproviders, like this:
$searchModelOne = new JobPlanningSearch();
$dataProviderOne = $searchModelOne->search(Yii::$app->request->queryParams);
$dataProviderOne->pagination->pageParam = 'dp-one-page'; //set page param for first dataprovider
$searchModelTwo = new JobPlanningSearch();
searchModelTwo->id = 2; // set id = 2 in second dataprovider
$dataProviderTwo = $searchModelTwo->search(Yii::$app->request->queryParams);
$dataProviderTwo->pagination->pageParam = 'dp-two-page'; //set page param for second dataprovider
You need to use two searchModels, one for each GridView and of course two data providers.
Also you need to change the formName attribute of one of the searchModels to avoid filtering to affects both grids.
And the in the controller, when passing parameters to the search() method of the modified searchModel, pass the params in the new name you assigned to formName in this searchModel.
You will have to manipulate the DataProvider in the controller.
$searchModel = new JobPlanningSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere(['=','id',2]);
remove it from the search function in JobPlanningSearch.
$query->andFilterWhere([
/* 'id' => $this->id, */ // you must remove it from here
'priority' => $this->priority,
'client_id' => $this->client_id,
'employer_id' => $this->employer_id,
'estimated_time' => $this->estimated_time,
...
]);
In this case, the ID will always be 2.
But if you want to have a default value (the first time) and allow the user to change it, you should skip step 2. And add a condition in the controller:
...
if(count(Yii::$app->request->queryParams) == 0){
$dataProvider->query->andWhere(['=','id',2]);
}
This is just an example, you should check that the ID field has not been sent in the queryparams.

Two Models Displaying to Same View in Yii2

I am trying to get information from two models that are related, displayed in one view.
So what I am trying to accomplish is have the index view to show the list of people, if I then go into detail view of that particular person I want a list of attributes relevant to that person to appear.
I have the database setup so that when I create a new person a default row gets inserted into the attributes table with the id of the person under the column called person_id.
See my two model classes
People:
class People extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'people';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['dob', 'CURDATE'], 'safe'],
[['age'], 'integer'],
[['firstname', 'surname'], 'string', 'max' => 50]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'firstname' => 'Firstname',
'surname' => 'Surname',
'dob' => 'Dob',
'age' => 'Age',
'CURDATE' => 'Curdate',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getId0()
{
return $this->hasOne(Attributes::className(), ['person_id' => 'id']);
}
}
Attributes:
class Attributes extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'attributes';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['haircolor', 'eyecolor', 'weight', 'height', 'person_id'], 'required'],
[['weight', 'height', 'person_id'], 'integer'],
[['haircolor', 'eyecolor'], 'string', 'max' => 50]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'haircolor' => 'Haircolor',
'eyecolor' => 'Eyecolor',
'weight' => 'Weight',
'height' => 'Height',
'person_id' => 'Person ID',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getPeople()
{
return $this->hasOne(People::className(), ['id' => 'person_id']);
}
}
I have generated CRUD through Gii for both of these models.
What I would like to know is how to setup my people controller and people view so that this may work properly.
Just to recap, my index.php view will just show the list of people, if a record exists, you can view that specific record, if you view the record - which will be the view.php file, I want to show the attributes(These will be the default values) of that particular person where the id of the person is the same as the person_id in the attributes table
The user will then be able to update the attributes relating to that person.
Kind Regards.
Here an example :
public function actionCreate()
{
$user = new User;
$profile = new Profile;
if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) && Model::validateMultiple([$user, $profile])) {
$user->save(false); // skip validation as model is already validated
$profile->user_id = $user->id; // no need for validation rule on user_id as you set it yourself
$profile-save(false);
return $this->redirect(['view', 'id' => $user->id]);
} else {
return $this->render('create', [
'user' => $user,
'profile' => $profile,
]);
}
}
More informations :
http://www.yiiframework.com/forum/index.php/topic/53935-solved-subforms/page__p__248184
http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html
To display related information in a view, you get the best performance with eager loading. I'll provide an example:
public function actionView($id)
{
$model = Person::find()
->where(['id' => $id])
->with('id0')
->one();
return $this->render('view', [
'model' => $model,
]);
}
Now i see that your relation in Person Model is called getId0, you can for readability change that to getAttribs(), and change to ->with('attribs') but that is just a digression :)
EDIT: as #soju commented, attributes is not possible to use as a relation name, and that is why gii has given it the name getId0. Attribs or something more informative can be helpful on readability.
If you want to show the results in a widget, like GridView or ListView, you can follow the guide here:
http://www.ramirezcobos.com/2014/04/16/displaying-sorting-and-filtering-model-relations-on-a-gridview-yii2/
EDIT2: as #soju commented, guide is possibly outdated. Read official documents aswell.
http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#working-with-model-relations
If you want to create your own view, you can access the values with $model->id0->haircolor or, if you rename the relation, $model->attribs->haircolor just like you would any other attribute.
Remember: using GridView / ListView requires the table name from the db when displaying, like 'attributes.eyecolor', but the $model->id0 requires the relation name from the model, without the 'get' in front, and with lower case.

yii CActiveDataProvider or TbGridView do not work with pagination

I'm using Yii Booster TbGridView to show a DataProvider from CActiveDataProvider. It is functioning but not completely. I do not know what is going on with the pagination because when I select the other pages from the Grid, it does not return any record. It is strange because the pagination by default is 10 records, and the data is for example 33 records, the pagination creates 4 pages but I only can see the first 10 records and the others are not shown after I click on the number of the page I want to see. I only can see the first ten records.
Here's my code:
Action (Controller)
public function actionIndex() {
//Verificamos si es la primera vez que se corre la accion y asignamos fechas iniciales
if (!isset($_POST['FormGestionInformacion']['fechaDesde'])
&& !isset($_POST['FormGestionInformacion']['fechaDesde']) ){
$fechaHasta = date('Y-m-d');
$fechaDesde = date('Y-m-d',time()-(60*60*24*15));//Quince dias atras
} else {
$fechaHasta = $_POST['FormGestionInformacion']['fechaHasta'];
$fechaDesde = $_POST['FormGestionInformacion']['fechaDesde'];
}
//Verificamos si el usuario mando filtro, y armamos el WHERE a ver si esa palabra esta en alguna parte
$criteria = new CDbCriteria();
$criteria->alias = 's';
$criteria->select = 'c.fecha_creacion,
c.fecha_cierre,
s.caso_sistema_info,
s.sistema_informacion,
s.documentacion,
s.fecha_documentacion,
s.usuario,
s.tipo_documentacion,
s.tipo_protocolo';
$criteria->join = "INNER JOIN ".DB_USUARIOS.".soporte_casos c ON s.caso_sistema_info=c.caso_sistema_info";
$criteria->condition = "s.caso_sistema_info=c.caso_sistema_info and fecha_cierre>='"
.$fechaDesde." 00:00:00' and fecha_cierre<='".$fechaHasta." 23:59:59'";
$criteria->group = "s.caso_sistema_info";
$criteria->order = " s.caso_sistema_info";
$criteria->offset = 0;
if (isset($_POST['FormGestionInformacion']['filtro'])
&& $_POST['FormGestionInformacion']['filtro']!='') {
$filtro = $_POST['FormGestionInformacion']['filtro'];
$criteria->addCondition ("s.caso_sistema_info like '%$filtro%'
or s.sistema_informacion like '%$filtro%'
or s.usuario like '%$filtro%' or
s.tipo_documentacion like '%$filtro%'
or s.tipo_protocolo LIKE '%$filtro%'");
}
$dataProvider = new CActiveDataProvider('SoporteCasosDocumentacion', array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 5,
),
)
);
$model = new FormGestionInformacion ();
$this->render('index', array(
'dataProvider' => $dataProvider,
'model'=> $model,
'fechaDesde'=>$fechaDesde,
'fechaHasta'=>$fechaHasta,
'filtro'=>$filtro,
)
);
}
Model:
class SoporteCasosDocumentacion extends CActiveRecord {
/**
* #return string the associated database table name
*/
public function tableName() {
return 'soporte_casos_documentacion';
}
/**
* #return array validation rules for model attributes.
*/
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('caso_sistema_info, sistema_informacion, usuario', 'required'),
array('fecha_documentacion, caso_sistema_info, sistema_informacion, usuario', 'length', 'max' => 45),
array('email, celular', 'length', 'max' => 50),
array('informacion_cliente_une', 'length', 'max' => 1),
array('archivo_adjunto, pro_det, sol_dad, ubi_fal, causa, ser_afe, con_aut, cor_ele', 'length', 'max' => 100),
array('tipo_documentacion, tipo_protocolo, tie_sol, tel_con, cel_con', 'length', 'max' => 10),
array('documentacion, error_oci', 'safe'),
// The following rule is used by search().
// #todo Please remove those attributes that should not be searched.
array('id_doc, fecha_documentacion, caso_sistema_info, sistema_informacion, documentacion, email, celular, informacion_cliente_une, usuario, archivo_adjunto, tipo_documentacion, tipo_protocolo, error_oci, pro_det, sol_dad, ubi_fal, causa, ser_afe, tie_sol, con_aut, cor_ele, tel_con, cel_con', 'safe', 'on' => 'search'),
);
}
/**
* #return array relational rules.
*/
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array();
}
/**
* #return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id_doc' => 'Id Doc',
'fecha_documentacion' => 'Fecha Documentacion',
'caso_sistema_info' => 'Caso Sistema Info',
'sistema_informacion' => 'Sistema Informacion',
'documentacion' => 'Documentacion',
'email' => 'Email',
'celular' => 'Celular',
'informacion_cliente_une' => 'Informacion Cliente Une',
'usuario' => 'Usuario',
'archivo_adjunto' => 'Archivo Adjunto',
'tipo_documentacion' => 'Tipo Documentacion',
'tipo_protocolo' => 'Tipo Protocolo',
'error_oci' => 'Error Oci',
'pro_det' => 'Pro Det',
'sol_dad' => 'Sol Dad',
'ubi_fal' => 'Ubi Fal',
'causa' => 'Causa',
'ser_afe' => 'Ser Afe',
'tie_sol' => 'Tie Sol',
'con_aut' => 'Con Aut',
'cor_ele' => 'Cor Ele',
'tel_con' => 'Tel Con',
'cel_con' => 'Cel Con',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* #return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search() {
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id_doc', $this->id_doc, true);
$criteria->compare('fecha_documentacion', $this->fecha_documentacion, true);
$criteria->compare('caso_sistema_info', $this->caso_sistema_info, true);
$criteria->compare('sistema_informacion', $this->sistema_informacion, true);
$criteria->compare('documentacion', $this->documentacion, true);
$criteria->compare('email', $this->email, true);
$criteria->compare('celular', $this->celular, true);
$criteria->compare('informacion_cliente_une', $this->informacion_cliente_une, true);
$criteria->compare('usuario', $this->usuario, true);
$criteria->compare('archivo_adjunto', $this->archivo_adjunto, true);
$criteria->compare('tipo_documentacion', $this->tipo_documentacion, true);
$criteria->compare('tipo_protocolo', $this->tipo_protocolo, true);
$criteria->compare('error_oci', $this->error_oci, true);
$criteria->compare('pro_det', $this->pro_det, true);
$criteria->compare('sol_dad', $this->sol_dad, true);
$criteria->compare('ubi_fal', $this->ubi_fal, true);
$criteria->compare('causa', $this->causa, true);
$criteria->compare('ser_afe', $this->ser_afe, true);
$criteria->compare('tie_sol', $this->tie_sol, true);
$criteria->compare('con_aut', $this->con_aut, true);
$criteria->compare('cor_ele', $this->cor_ele, true);
$criteria->compare('tel_con', $this->tel_con, true);
$criteria->compare('cel_con', $this->cel_con, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
/**
* #return CDbConnection the database connection used for this class
*/
public function getDbConnection() {
return Yii::app()->Usuarios;
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* #param string $className active record class name.
* #return SoporteCasosDocumentacion the static model class
*/
public static function model($className = __CLASS__) {
return parent::model($className);
}
}
The Widget:
$this->widget('bootstrap.widgets.TbButton', array(
'buttonType' => 'button',
'type' => 'primary',
'label' => 'Consultar',
'size' => 'large',
'htmlOptions' => array(
'onClick' => '{ValidacionDatos()}',
'class' => 'btn'
),
));

Css is not loading after changed the GetDbConnection() in Yii

I'm new to Yii Framwork, I have used the steps from Multiple-database support in Yii to connect different database , its helped me lot.
but Css is not loaded, normal HTML content is displaying in browser when I'm opening the index.php
What changes is require to load the CSS after changing the GetDbConnection() in modules.
my Ad.php code from models
<?php
class Ad extends MyActiveRecord
{
public $password;
public $repassword;
public function getDbConnection()
{
return self::getCCDbConnection();
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
....
}
Thanks in Advance
This does not solve your css problem, however, this is the right way to use multiple dbs in yii.
This is the right way to use multiple db's in yii mvc:
Let's say that i have multiple db's and I use them to store urls.
From time to time I need to change the db.
So, I have the model generated by using gii and on top of that I have class that extends and overwrites some of methods/functions.
UrlSlaveM extends UrlSlave wich extends CActiveRecord
as default, in UrlSlave I will connect to my first db
I always use UrlSlaveM when I insert new data, so that I can overwrite the following function:
public function getDbConnection() {
return Yii::app()->db1;
}
here is a full SlaveUrl model:
<?php
/**
* This is the model class for table "url".
*
* The followings are the available columns in table 'url':
* #property string $id
* #property integer $instance_id
* #property integer $website_id
* #property string $link
* #property string $title
* #property integer $created
* #property integer $updated
* #property integer $status
*/
class UrlSlave extends CActiveRecord {
/**
* Returns the static model of the specified AR class.
* #param string $className active record class name.
* #return UrlSlave the static model class
*/
public static function model($className = __CLASS__) {
return parent::model($className);
}
/**
* #return CDbConnection database connection
*/
public function getDbConnection() {
return Yii::app()->db1;
}
/**
* #return string the associated database table name
*/
public function tableName() {
return 'url';
}
/**
* #return array validation rules for model attributes.
*/
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('instance_id, website_id, link, title, created, updated, status', 'required'),
array('instance_id, website_id, created, updated, status', 'numerical', 'integerOnly' => true),
array('link, title', 'length', 'max' => 255),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, instance_id, website_id, link, title, created, updated, status', 'safe', 'on' => 'search'),
);
}
/**
* #return array relational rules.
*/
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* #return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id' => 'ID',
'instance_id' => 'Instance',
'website_id' => 'Website',
'link' => 'Link',
'title' => 'Title',
'created' => 'Created',
'updated' => 'Updated',
'status' => 'Status',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('instance_id', $this->instance_id);
$criteria->compare('website_id', $this->website_id);
$criteria->compare('link', $this->link, true);
$criteria->compare('title', $this->title, true);
$criteria->compare('created', $this->created);
$criteria->compare('updated', $this->updated);
$criteria->compare('status', $this->status);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
}
and here is the full UrlSlaveM model:
<?php
class UrlSlaveM extends UrlSlave {
const ACTIVE = 1;
const INACTIVE = 0;
const BANNED = -1;
public static function model($className = __CLASS__) {
return parent::model($className);
}
public function rules() {
$parent_rules = parent::rules();
$rules = array_merge(
$parent_rules, array(
array('link', 'unique'),
));
return $rules;
}
public static $server_id = 1;
public static $master_db;
public function getDbConnection() {
//echo __FUNCTION__;
//die;
//echo 111;
self::$master_db = Yii::app()->{"db" . self::$server_id};
if (self::$master_db instanceof CDbConnection) {
self::$master_db->setActive(true);
return self::$master_db;
}
else
throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.'));
}
}
now, by setting $server_id to 1 or 2 or 3 ... you are able to connect to another db
please set the value of $server_id as UrlSlaveM::$server_id = 2; before you add data!
public static $server_id = 1;
public static $master_db;
also, in the main config file, set like this:
'db' => array(
'connectionString' => 'mysql:host=localhost;dbname=dvc',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
'db2' => array(
'connectionString' => 'mysql:host=localhost;dbname=dvc2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => '',
'class' => 'CDbConnection' // DO NOT FORGET THIS!
),
'db1' => array(
'connectionString' => 'mysql:host=localhost;dbname=dvc1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => '',
'class' => 'CDbConnection' // DO NOT FORGET THIS!
),

Yii Model extending / overloading

I have an Account Model which has some properties and relations, and i have an Reseller model.
The point is that a reseller is actually an account only it has the possibility to have accounts beneath it.
What is the best approach to implement this,
at first i had a special Reseller class with relations between them, but actually i just want an accounts class which if the account is a reseller uses the reseller class.
Account model
<?php
/**
* This is the model class for table "account".
*
* The followings are the available columns in table 'account':
* #property string $id
* #property string $reseller_id
* #property string $name
* #property string $invoice_id
* #property boolean $is_reseller
*
* The followings are the available model relations:
* #property Reseller $reseller
* #property Contact[] $contacts
* #property Domain[] $domains
* #property HostingAccount[] $hostingAccounts
* #property User[] $users
*/
class Account extends CActiveRecord {
/**
* Returns the static model of the specified AR class.
* #param string $className active record class name.
* #return Account 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 'account';
}
/**
* #return array validation rules for model attributes.
*/
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name', 'required'),
array('id, reseller_id', 'length', 'max' => 40),
array('name', 'length', 'max' => 45),
array('invoice_id', 'length', 'max' => 10),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, reseller_id, name, invoice_id', 'safe', 'on' => 'search'),
);
}
/**
* #return array relational rules.
*/
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'reseller' => array(self::BELONGS_TO, 'Reseller', 'reseller_id'),
'contacts' => array(self::HAS_MANY, 'Contact', 'account_id'),
'domains' => array(self::HAS_MANY, 'Domain', 'account_id'),
'hostingAccounts' => array(self::HAS_MANY, 'HostingAccount', 'account_id'),
'users' => array(self::HAS_MANY, 'User', 'account_id'),
);
}
/**
* #return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id' => 'ID',
'reseller_id' => 'Reseller',
'name' => 'Name',
'invoice_id' => 'Invoice',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('reseller_id', $this->reseller_id, true);
$criteria->compare('name', $this->name, true);
$criteria->compare('invoice_id', $this->invoice_id, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
/**
* Adds UUID before the item is saved
*
*/
public function beforeSave() {
if ($this->isNewRecord)
$this->id = new CDbExpression('UUID()');
return parent::beforeSave();
}
}
Reseller Model
<?php
class Reseller extends Account
{
/**
* #return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'reseller' => array(self::BELONGS_TO, 'Reseller', 'reseller_id'),
'contacts' => array(self::HAS_MANY, 'Contact', 'account_id'),
'domains' => array(self::HAS_MANY, 'Domain', 'account_id'),
'hostingAccounts' => array(self::HAS_MANY, 'HostingAccount', 'account_id'),
'users' => array(self::HAS_MANY, 'User', 'account_id'),
'accounts' => array(self::HAS_MANY, 'Account', 'reseller_id'),
'account' => array(self::BELONGS_TO, 'Account', 'account_id'),
);
}
}
First keep in mind that ActiveRecord != models its easy to get confused, beware!
Also check this post
Ok now, you can have some factory method that gives you the class you need, Account or Reseller, depending on your logic, if not all acounts can be resellers you may also need some way to determine this. like a "is_reseller" column or similar.
In the end i made a relation from accounts to itself and solved it.
/**
* #return array relational rules.
*/
public function relations() {
return array(
'reseller' => array(self::BELONGS_TO, 'Account', 'account_id'),
'users' => array(self::HAS_MANY, 'User', 'account_id'),
'accounts' => array(self::HAS_MANY, 'Account', 'account_id'),
);
}
I used a extended classes to a models to implemented a diferents methos and a unique prerequisite is add this function in a new class:
public static function model($className=__CLASS__){
return parent::model($className);
}

Categories