Welcome Guest, Not a member yet? Register   Sign In
DataMapper | Unknown column 'User' in 'field list'
#1

[eluser]thomasjonas[/eluser]
Hello there,

I'm new to Datamapper and I'm straight diving into the advanced relations. Unfortunately it's not working out so great for me... Somehow I keep getting this error:

Code:
Unknown column 'User' in 'field list'

INSERT INTO `posts` (`title`, `creator`, `editor`) VALUES ('testing', User, User)

Filename: ........................../libraries/Datamapper.php

Line Number: 1724

I'm using CI 2.1.0

This is how my User model looks like:
Code:
class User extends DataMapper {
    var $has_many = array(
        'created_post' => array(
            'class' => 'post',
            'other_field' => 'creator'
        ),
        'edited_post' => array(
            'class' => 'post',
            'other_field' => 'editor'
        )
    );

var $validation = array(
        'username' => array(
            'label' => 'Username',
            'rules' => array('required', 'trim', 'unique', 'alpha_dash', 'min_length' => 3, 'max_length' => 20),
        ),
        'password' => array(
            'label' => 'Password',
            'rules' => array('required', 'min_length' => 6, 'encrypt'),
        ),
        'email' => array(
            'label' => 'Email Address',
            'rules' => array('required', 'trim', 'valid_email')
        )
    );
}

Post model
Code:
class Post extends DataMapper {
    var $has_one = array(
        'creator' => array(
            'class' => 'user',
            'other_field' => 'created_post'
        ),
        'editor' => array(
            'class' => 'user',
            'other_field' => 'edited_post'
        )
    );

var $validation = array(
     'title' => array(
         'label' => 'Title',
         'rules' => array('required')
     )
);
}

Controller
Code:
$u = new User(1);

  $p = new Post();
  $p->title = "testing";
  $p->save_creator($u);
  $p->save();

In my database there already is a user with id=1

Database:
users:
-id
-username
-password
-email

posts:
-id
-title
-creator
-editor

posts_users
-id
-creator_id
-created_post_id
-editor_id
-edited_post_id

Hopefully it's a really stupid mistake that can be fixed easily...
Thanks in advance!
#2

[eluser]WanWizard[/eluser]
First make sure you have the latest version of Datamapper, line 1724 is a comment line in that version.
#3

[eluser]thomasjonas[/eluser]
Thanks for your response WanWizard.

I can confirm I have the latest version (1.8.2)
On my version line 1724 says
Code:
$this->db->insert($this->table, $data);
#4

[eluser]WanWizard[/eluser]
The version in the repository has that line on 1635 so I still suggest you get the latest version from bitbucket to rule out any version issues.

It would allow me to debug this problem, now I don't know what could cause it...
#5

[eluser]thomasjonas[/eluser]
Ok, so now I have the latest version cloned from bitbucket and still get the same error... but now it's on line 1635. When I comment out the relations of my post object I'm able to save the post, but with the relation declared I'm not... So I'm guessing it has something to do with 'advanced relations'
#6

[eluser]WanWizard[/eluser]
You have a has_one from posts -> users, and a has_many from users -> posts. Normally, this means the foreign keys are part of the posts table, and no relation table is needed.

Get rid of the posts_users table, and add 'creator_id' and 'editor_id' to the posts table.

Code:
$p = new Post();
$p->title = "Test new post with creator 1 and editor 2";

$u = new User(1);
$p->save_creator($u); // create the post with creator id 1

$u = new User(2);
$p->save_editor($u);  // update the post with editor id 2

Also, the "other_field" value in the advanced relationship is used to be able to map the relation from the other side, if that is defined different from the class name.

You do that correct here, but then you use these as column names in the relationship table, which is not where they are used. If you insist on using a relationship table, you will have to use the 'join_self_as' and 'join_other_as' fields to define the foreign key colums in the relationship table.
#7

[eluser]thomasjonas[/eluser]
Sorry for the late reply.

Thanks a lot for your help. Things are much clearer for me now! I think I just couldn't handle all the new information in de user guide. Thanks for clearing things up for me!




Theme © iAndrew 2016 - Forum software by © MyBB