Welcome Guest, Not a member yet? Register   Sign In
proper conception
#1

[eluser]DarkManX[/eluser]
hi guys,

i have a page which got to generate items and insert them to the db. if i create users i just validate the form input and insert the data row to the db via the user-model. should look something like that:

Code:
$this->User_model->insert(array(
'username'=>$name,
'password'=>$password,
'random_stuff'=>$random_stuff
));

so i generate the 'random stuff' in the controller. now i need to create new user beside the 'user creating form'. do i need to code a function in the user-model, that will insert the user and and make the random stuff going.
or should i just create a library, which will generate the random stuff and just use the insert-function of the model.
summed up: should i just code model-function that comunicate with the db like insert($data) or should i also add some content generating code to the model without adding a library.

hope you understood my issue although the lack of eloquence in english! Smile
ty
#2

[eluser]TWP Marketing[/eluser]
I would just make a new method in your User model, perhaps called "create_user($data)"
You have the information from your form, just pass it to the model function.

I don't know exactly what you have in your variable $random_stuff,
but you ought to be able to define a default set of that var content
when you create a new user record.

You can make a model method to generate the default content for a new user
record, which would include the $random_stuff var.

For instance, in your model:

Code:
public function default_user()
{
  $user_data = array();
  $user_data['name'] = "";
  $user_data['type'] = "s"; // standard or default user type
  $user_data['random_stuff'] = $this->set_random_stuff();
  return($user_data);
}
public function set_random_stuff()
{
$stuff = array();
$stuff['var_name'] = 'some name';
$stuff['some_other_stuff'] = 'more stuff here';
return( $stuff );
}

Does this answer your question? Please ask for again if I missed your point.
#3

[eluser]DarkManX[/eluser]
Hi,

its not about that exact user-issue, its the general idea which i didnt understand right i guess. whats the best place to put the methods which generate content.
first option - all in model:
Code:
class User_model extends CI_Model[
public function create_random_user(){
  $this->insert(array(
   'name'=>$this->random_name();
   'password'=>$this->random_pw();
}
private function random_name(){/* returns random name*/}
private function random_pw(){/* returns random password*/}
public function insert($data){
  $this->db->insert($data);
}
}

second option - using library:
Code:
class User_model extends CI_Model{
public function insert($data){
  $this->db->insert($data);
}
}
class User_lib{
public function create_random_user(){
  $this->User_model->insert(array(
   'name'=>$this->random_name();
   'password'=>$this->random_pw();
}
private function random_name(){/* returns random name*/}
private function random_pw(){/* returns random password*/}
}

what is the best & optimal way to do that?
grtz
#4

[eluser]TWP Marketing[/eluser]
Since you're generating data which will be stored in your DB, I think MVC says to do that in your model.
You "could" do in the controller, but data really should be massaged in the model and presented in 'ready-to-use format to the controller.

By creating a separate library, you're adding another layer to the process.




Theme © iAndrew 2016 - Forum software by © MyBB