Populate/Load Data In active Record instance

Hi,

Every exemple of fetching data using an active record instance looks like this.




$model = new MyModel();

$myRecords = $model->find('key=?',array($key));


// then use $myRecords 

echo $myRecords->name; 



What is the way to populate the data inside the MyModel instance ?

For exemple in my model i have a method like this




class MyModel extends CActiveRecord  {


  ...


  public function getbyKey($key)

  {

    // and don't want to return the datas , i want to set the data !

    $this->find('key=?',array($key));

  }


}



and i want to use like this




$model = new MyModel();

$model->getByKey($key);


// then use $myRecords 

echo $model->name; 



What is the way in my class method to Set attributes ?

Thanks.

Hi,

you can just use $this->name = ‘abc’ inside methods of model.

I don’t get it.

Do you mean that there is no other way than setting each attribute after fecthing datas ?

yes, I mean you can set/modify each attribute inside model.

From my understanding your approach is not too clear:

if you create new model


$model = new MyModel();

you should not load it from db.

if you are working with existing model, you can get it via:


$model = User::model()->findByPk($key);

Well in fact i don’t want to create and use new model instance from my class i want my class to be the model object

Let me explain more clearly

i have a class called BaseUser that extends CActiveRecord ( generate by gii )




class BaseUser extends CActiveRecord {

  ....

}



now i want to create My own model on top of this i call it User.

And i want to put all my logic methods inside this class

exemple




class User extends BaseUser {

  


  // add some rules

  public function rules()

  {

    $rules = array(

      array('email', 'email'),

      array('email, first_name, last_name, password, password_confirmation', 'required', 'on'=>'signup'),

    );

    return CMap::mergeArray( parent::rules(),$rules);

  }


  // put creation of user logic inside my model

  public function createPowerUser(array $data)

  {

     // here i have complex process to handle 

     ......

     ....

     $this->user_type = POWER_USER;

     $this->user_name = $data['user_name'];


     // then i save 

     return $this->save();

  }




public function getByEmail($email)

{

   // this does not work !

   $this->find('email=?',array($email);

}


public function notify()

{

   Mailer::Send($this->email,'a message');


}




}



No i use my model in a controller




public function actionSignup()

{

  $user = new User('signup');

  if( true === $user->createPowerUser($_POST['user']) ) {

    echo $user->id;

    $user->notify();

  }

  

}



By doing this no problem i can create my user and then access the properties after the creation

Now i want to be able to what i explain in my first post. because by using model static method you loose all method that you created in the child class

This just don’t work at all …

it throw an error as getByemail is not a method of my model




public function actionTest()

{

  $user = new User();

  $toto = User::model()->getByEmail('test@test.com');

  

}



So is it possible to use my methods properly ?




public function actionTest()

{

  $user = new User();

  $user->getByEmail('test@test.com');


  echo $user->id;


  $user->notify();


etc...

  

}



Do you have any ideas ?

I want to have FAT models and skinny controllers.

Maybe it’s a design issue and i must don’t extend those active records .

Any help about this would be very great.

Hmm… I assume I’ve get the problem.

when you extend CActiveRecord you should always keep this method (in User and baseuser):


	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

Ok ! I think i get it now .

Using the model() static method is about manipulate sort of "Collection" of the given model.

So it’s better to instantiate the model using this method than using the common way ( ie $m = new Model() ) when it’s about fetching datas , and when it’s about inserting or about any “scenario” it’s better to use ( $m = new Model(‘myscenario’) )

It’s a bit confusing in the first place.

Thanks a lot for the Help, now it’s more clear to me !