Welcome Guest, Not a member yet? Register   Sign In
model problems
#1

[eluser]vobla[/eluser]
Hi,

I am having some modeling problems. I am new to CI so most likely I missed/messed something Wink

When I use suggested technique (http://ellislab.com/codeigniter/user-gui...odels.html) I get:



An Error Was Encountered

Error Number: 1054

Unknown column '_parent_name' in 'field list'

INSERT INTO video (_parent_name, id, order_id, title, equipment, software, description, _ci_ob_level, _ci_view_path, _ci_is_php5, _ci_is_instance, _ci_scaffolding, _ci_scaff_table) VALUES ('Video_model', '', 7, NULL, NULL, NULL, NULL, 0, 'd:\\projektai\\www\\gi portfolio with code igniter/system/application/views/', 0, 0, 0, 0)


My model class:
Code:
class Video_model extends Model
{
    var $id = "";
    var $order_id = "";
    var $title   = "";
    var $equipment = "";
    var $software = "";
    var $description    = "";

    function Video_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    //    Insert new video entry
    function add($data)
    {
        $this->order_id = $this->get_new_order_id();
        $this->title = $data->title;
        $this->equipment = $data->equipment;
        $this->software = $data->software;
        $this->description = $data->descritption;
        
        $this->db->insert('video', $this); // <<--- I get error here
    }

        ......

Table structure :
id (auto_increasment), order_id, title, equipment, software, description

Seems that AR messes up all the query (?!?)..
When I print the model instance with print_r, I see where those thingies come from, but that is not the way it should work, isn't it ?

Sorry if it's too obvious. As I said before, I'm a newb Wink

Thank you in advance.
#2

[eluser]Armchair Samurai[/eluser]
It's not AR - it's your code.

Try using an array:
Code:
$arr = array(
    'order_id' => $this->get_new_order_id(),
    'title' => $data->title,
    'equipment' => $data->equipment,
    'software' => $data->software,
    'description' => $data->description
);

$this->db->insert('video', $arr);

$this is a reference to the calling object. Check out the Objects section over at PHP.net
#3

[eluser]vobla[/eluser]
Well yes. I am sure this will work, because I used it before I decided to create model.
Check out: http://ellislab.com/codeigniter/user-gui...odels.html (it's wrong then)

I just thought (actualy I could have check that before I asked - my bad ;P ), that AR extracts properties from the model class and then generates query..

Anyway, thank you for the reply.
#4

[eluser]Grahack[/eluser]
According to the docs, AR will try to insert a row in the db with one db field per object field.
It works well with classes that only have fields named according to the db structure. It doesn't work with your model because its class inherits fields from a parent that is a big parent: a CI model.

I guess the faulty fields are:
_parent_name, _ci_ob_level, _ci_view_path, _ci_is_php5, _ci_is_instance, _ci_scaffolding, _ci_scaff_table
#5

[eluser]vobla[/eluser]
Yes, that right! Thanks.

So I suppose I should do something like this:

Video_object.php
Code:
class Video_object
{
    var $id = "";
    var $order_id = "";
    var $title   = "";
    var $equipment = "";
    var $software = "";
    var $description    = "";
    
    function Video_object()
    {
        
    }
}

Video_model.php
Code:
class Video_model extends Model
{
    function Video_model()
    {
        // Call the Model constructor
        parent::Model();
        
        require_once("Video_object.php");
    }
    
    //    Insert new video entry
    function add($data)
    {        
        $video = new Video_object();
        
        $video->order_id = $this->get_new_order_id();
        $video->title = $data->title;
        $video->equipment = $data->equipment;
        $video->software = $data->software;
        $video->description = $data->descritption;
        
        $this->db->insert('video', $video);
    }

Seems that it's working, but is it elegant enought (I am trying to do my best in that stuff Wink ) ?

EDIT: Or instead of extending class to Model and using $this->load->model(..) to load it, I could just load it with require_once(..) and create a new instance, isn't it? But then I dont see the purpose of Model class and why should I extend some class to it?

P.S. Could admins please edit documantation (http://ellislab.com/codeigniter/user-gui....html#what) ?
#6

[eluser]Grahack[/eluser]
My opinion is: even if a framework has powerful features, don't do over-complicated things to try to use them at any price.

Using CI models allows you to forget the 'include' keyword, which is clean. Armchair Samurai was right: try with an array in your model.

My personal strategy is: a field is used to store fields that I like to loop through.
Code:
var fields = array();
function constructor($data){$fields = $data;}
function add(){$this->db->insert('table', $this->fields);}
#7

[eluser]vobla[/eluser]
Hmm.. Good point. Thank you. Wink
#8

[eluser]Archiloque[/eluser]
I had the same problem a dug into the code : it seems it's a bug in CI code, in database/DB_active_rec.php, in the _object_to_array method that transforms an object into an array to generate the query:

Code:
if ( ! is_object($val)
  && ! is_array($val)
  && $key != '_parent_name'
  && $key != '_ci_scaffolding'
  && $key != '_ci_scaff_table')

if you add the missing values that are causing the problem:
Code:
if ( ! is_object($val)
  && ! is_array($val)
  && $key != '_parent_name'
  && $key != '_ci_scaffolding'
  && $key != '_ci_scaff_table'
  && $key != '_ci_ob_level'
  && $key != '_ci_view_path'
  && $key != '_ci_is_php5'
  && $key != '_ci_is_instance')

the insertion works.
#9

[eluser]Archiloque[/eluser]
It's a bug in CI : http://codeigniter.com/bug_tracker/bug/2702/




Theme © iAndrew 2016 - Forum software by © MyBB