I'm trying to use an object and persist it into the data base.

I created a class in the library's folder.

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Solicitud {

    private $categoria;
    private $consecutivo;
    private $id;
    private $mensaje;
    private $solicitante;

    function __construct(){
        $this->categoria = 1;
        $this->consecutivo = 1;
        $this->mensaje = "Mensaje por defecto";

    public function escalarSolicitud( User $actual, User $nuevo){

    # Getters and Setters

    public function setCategoria( $categoria ){
        $this->categoria = $categoria; 

    public function getCategoria(){
        return $this->categoria;

Here is the Controller:


if (!defined('BASEPATH')) exit('No direct script access allowed');

class Administrador extends Ci_Controller {

    function __construct(){ 

    public function crearSolicitud(){

        $session_data = $this->session->userdata('session_user');   
        $usuario      = $session_data['usuario']; 
        $categoria    = $this->input->post('category');
        $mensaje      = $this->input->post('description');

        $solicitud = new solicitud;


        $this->helpdesk_model->createSupport( $solicitud );

And in the Model how can I use the object properties:

public function createSupport( $soporte ){


        $data = array(
            'id' => null,  
            'id_user' => $soporte[usuario], ???
            'id_category' => $soporte[categoria], ???
            'message' => $soporte[mensaje] ???

        $this->db->insert('supports', $data); 


    return $this->db->trans_status(); 


I need to know if I'm in the right direction or if the use of the 'Solicitud' class is not necessary. If I'm right, how should I call in the model the object attributes?

  • \$\begingroup\$ I realized that int the model I can use the get functions like 'id_category' => $soporte->getCategoria(); \$\endgroup\$ Feb 5, 2014 at 14:51
  • 1
    \$\begingroup\$ yep in your case since you don't modify anything with the data i would pass them just as an array \$\endgroup\$ Apr 7, 2014 at 12:04

1 Answer 1


I would like to add something to @Dukeatcoding comment in the question. What is the point of passing the whole object? Thinking on it as a part of the main controller, you should only pass the data as an array to the model:

$data = array(
    'id_user'     => $usuario,
    'id_category' => $soporte->getCategoria
    'message'     => $soporte->getMensaje
$this->helpdesk_model->createSupport( $solicitud );

Although, as you are getting all these properties so often, what about a general getter in your soporte class?

public function getValue( $var ){
    return $this->{$var};

And you'd have something like:

$data = array(
    'id_user'     => $soporte->getVar( 'usuario'),
    'id_category' => $soporte->getVar( 'categoria'),
    'message'     => $soporte->getVar( 'mensaje' )

From your code, I would take care of the validation of your values, as it looks like you don't validate if they're numeric, if they exists on your DDBB, etc. Maybe you're doing it, because I suppose the code you posted is just a excerpt (due to some method are missed in Solicitud, as setSolicitante($nuevo) ), but I prefer to write it, just in case.

Also, I won't assign to a var the values you take directly, just call them an take the value:

//Instead of:
$session_data = $this->session->userdata('session_user');   
$usuario      = $session_data['usuario']; 
// Better 
$usuario      = $this->session->userdata('session_user');  

Off topic, you could check Doctrine, http://www.doctrine-project.org/projects/orm.html, is an ORM that maybe could help you, and it has its own CI recipe: http://docs.doctrine-project.org/en/2.0.x/cookbook/integrating-with-codeigniter.html

Have fun!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.