Part of the EllisLab Network
   
 
Model update problem
Posted: 09 November 2006 08:50 AM   [ Ignore ]  
Summer Student
Total Posts:  11
Joined  05-18-2006

Hi there
I am a beginner in using models in CI with the new 1.5.01 Version.
When I update my model, I got this error:

An Error Was Encountered
Error Number
: 1054
Unknown column
'_parent_name' in 'field list' etc..

My Code is like this:

class ReportModel extends Model {

    
var $typ      = '';
    var
$title    = '';
    var
$text     = '';

    function
ReportModel()
    
{
        
// Call the Model constructor
        
parent::Model();
    
}

    
function updateEntry()
    
{    
        $this
->db->where('typ',$_POST['typ']);
        
$this->db->update('reports',$this);
    
}
}

I found another thread with the same?
problem but without a solution.

I noticed that the _parent_name was not in earlier versions of CI (I just looked in 1.3.2)
Question for Rick is also, for what is that _parent_name?

Thanks,

Profile
 
 
Posted: 09 November 2006 09:10 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  26
Joined  09-20-2006

<edited original posts>
Hi,

Your code says:
$this->db->update(‘reports’,$this);

where $this would be the classinstance itself, instead of the $_POST array.

Profile
 
 
Posted: 09 November 2006 09:14 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  11
Joined  05-18-2006

Thanks, but the _parent_name is in the Model base class. See source code libraries, Model.

I dont want to insert a column in my db-tabel just for this _parent_name expecially
if I dont see any reason for..
It should be a possibility to exclude it.

Profile
 
 
Posted: 13 November 2006 10:53 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  11
Joined  05-18-2006

does anyone else know for what the _parent_name member variable
is in the base model class?

Profile
 
 
Posted: 13 November 2006 03:51 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  144
Joined  09-08-2006

your problem is as ruud said…

also

// We don't want to assign the model object to itself when using the
// assign_libraries function below so we'll grab the name of the model parent
$this->_parent_name = ucfirst(get_class($this));

Profile
 
 
Posted: 13 November 2006 04:14 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  11
Joined  05-18-2006

oh, now I see, that Ruud reedited his post.
But it would be nicer, if we could use the $this for updating.
In CI Docs ->Model there is an example with the insert like this

function insert_entry()
  {
      $this->title   = $_POST[‘title’];
      $this->content = $_POST[‘content’];
      $this->date   = time();

      $this->db->insert(‘entries’, $this);
  }

without the parent_name my update method would work,
is there a php possibility to take away that parent_name entry?

Profile
 
 
Posted: 04 December 2006 12:55 PM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  47
Joined  03-24-2006

It seems that

$this->db->update('reports',$this);
//or
$this->db->insert('reports',$this);


would work in previous versions, but since “_parent_name” appeared in 1.5 it’s no longer valid. 

It is possible, however, to modify “CI_DB_active_record::_object_to_array” method to check for “_parent_name” variable and simply not assign it to the returned array.

Profile
 
 
Posted: 09 August 2007 02:43 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  7
Joined  06-12-2007

Rewrite the function _object_to_array($object beginning on line 830 of DB_active_rec.php in the system/database folder to the following.  This will allow the code written in the usage guide to work correctly, eg.

$this->db->insert('stories', $this);

If anybody sees any side effects to this change, let me know.  Otherwise, can this be submitted to the baseline code?

/**
* Object to Array
*
* Takes an object as input and converts the class variables to array key/vals
*
* @access    public
* @param    object
* @return    array
*/
function _object_to_array($object)
{
    
if ( ! is_object($object))
    
{
        
return $object;
    
}
    
    $array
= array();
    foreach (
get_object_vars($object) as $key => $val)
    
{
        
if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
        
{
            $array[$key]
= $val;
        
}
    }

    
return $array;
}

Profile
 
 
Posted: 31 December 2007 10:25 AM   [ Ignore ]   [ # 8 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6762
Joined  03-23-2006

Fixed up.  Thanks all.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 24 March 2008 09:57 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Avatar
Total Posts:  11
Joined  02-21-2008

HI,
I’m using CI - 1.5.4
I’m still having the same problem with:

<code>

  function insert_entry()
  {
      $this->title   = $_POST[‘title’];
      $this->content = $_POST[‘content’];
      $this->date   = time();

      $this->db->insert(‘entries’, $this);
  }

</code>

to work, I always must to put into array…

Can you help me?

Profile
 
 
Posted: 24 March 2008 11:01 AM   [ Ignore ]   [ # 10 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6762
Joined  03-23-2006

Saymont, if you update to CI 1.6.1, does that still happen?

Upgrading from a previous version

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 22 May 2008 03:25 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  2
Joined  05-22-2008

Doesn’t seem like this has been resolved in the latest 1.6.2 release.

I just downloaded 1.6.2 and I’m getting this same problem when I insert using the $this statement.

Has this ever been resolved in a stable package release?

Profile
 
 
Posted: 22 May 2008 03:34 PM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  2
Joined  05-22-2008

Ok to verify that the fix is not in 1.6.2, I have to modify system/database/DB_active_rec.php in the _object_to_array method to get my app to work as per the “Models” documentation on the codeigniter userguide. I had to change the if statement code to look like this

// There are some built in keys we need to ignore for this conversion

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’)
                {
                      $array[$key] = $val;
                }

Profile
 
 
Posted: 22 May 2008 03:41 PM   [ Ignore ]   [ # 13 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6762
Joined  03-23-2006

This thread is kind of old and rambling, so I’m not sure what you are referring to.  I think you mean inserting via an object?  Could you provide some code?

$this->city = 'Toronto';
        
$this->province = 'Ontraio';
        
$this->country = 'Canada';

        
$this->db->insert('places', $this);

gives me

INSERT INTO `places` (`city`, `province`, `country`) VALUES ('Toronto', 'Ontraio', 'Canada')

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 66431 Total Logged-in Users: 25
Total Topics: 84796 Total Anonymous Users: 1
Total Replies: 455084 Total Guests: 230
Total Posts: 539880    
Members ( View Memberlist )
Newest Members:  GlennJDylan1978X_franbaguasllogocsaturkeyPeter BryanttherendStudioGeorgiaJZeer