Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using different model besides "User" #8

Closed
riemers opened this issue May 23, 2014 · 13 comments
Closed

Using different model besides "User" #8

riemers opened this issue May 23, 2014 · 13 comments

Comments

@riemers
Copy link

riemers commented May 23, 2014

I have a "Person" model, instead of the default "User" model. My app config is change to the auth model in "Person"

I try to seed some data into in it like:

    $person = Person::where('keyname', 'dj40kl')->firstOrFail();
    $roleAdmin = Role::create(['name' => 'admin']);
    $person->roles()->attach($roleAdmin->id);

I get the following error message:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'toolbox.person_role' doesn't exist (SQL: insert into person_role (person_id, role_id) values (dj40kl, 4))

It looks like it appends the "role" to the person_ name. When using the default "User" model i did not have this issue. Do i need to create those tables instead of the default ones?

@tortuetorche
Copy link
Contributor

Hi Erik-jan,

First of all don't run (or rollback) php artisan migrate --package=machuga/authority-l4 command.
You can copy the Authority-L4 migration files (vendor/machuga\authority-l4/src/migrations) in your migrations directory (app/database/migrations).
Then edit them to replace all User and user words to Person and person.
Finally run in a terminal :

php artisan migrate

If you have still an error, feel free to reopen this issue.

Cheers,
Tortue Torche

@riemers
Copy link
Author

riemers commented May 27, 2014

Thanks for the info, i did as you told me but i already had some stuff done so i just restarted from scratch. I redid the migrations file to point to the "Person" table i have. I see 3 tables created now and after running the same query it does load in the information exactly the same as it did when it created the information in the "User" table.

When i use above lines from my first post, i can see the roles being added, but it does not correctly add the person_role entry's. I did a dump of the data while i did the insert and i can see it does find the correct "id" of the person in the Person table. Yet what it creates in my database is:

 INSERT INTO `person_role` (`id`, `role_id`, `person_id`) VALUES (1, 4, 0), (2, 4, 0);

I did a print_r($person); print_r($roleAdmin);
And i can see person:

    [keyname] => dj40kl
    [name] => Piet hijn
    [email] => Piet.hijn@hijn.nl
    [remember_token] => sometoken
    [id] => 1298

And in the admin:

    [name] => admin
    [updated_at] => 2014-05-27 09:37:59
    [created_at] => 2014-05-27 09:37:59
    [id] => 4

So why does it not take the id of the person as person_id ?

@tortuetorche
Copy link
Contributor

It's weird, your person_id value is 0.
Try this code to attach a role to a person:

// Create a 'admin' role
$role1 = new Role;
$role1->name = "admin";
$role1->save();

// Attach the 'admin' role to a person
$person= Person::where('email', 'Piet.hijn@hijn.nl')->firstOrFail();
$person->roles()->attach($role1->id);

@riemers
Copy link
Author

riemers commented May 27, 2014

Still keeps it at 0 when i try it like that. Any other debug things i can try?

@tortuetorche
Copy link
Contributor

So it's not a bug related to this package...
Are you sure your models have the right associations ?
What's your database driver (mysql, postgresql, sqlite ...) ?

@riemers
Copy link
Author

riemers commented May 27, 2014

eloquent, mysql. Set the auth.php to use table = person and model Person. Perhaps the default models for Permissions / Roles are not enough since i use Person?

@tortuetorche
Copy link
Contributor

Can you show me your Person, Role and Permission models and your migrations files ?

@tortuetorche
Copy link
Contributor

You can create a secret gist, share me the URL, then remove it after I read it.

@riemers
Copy link
Author

riemers commented May 27, 2014

--hide url-- ow or a secret gist could work too, i just removed all non essential stuff.

@tortuetorche
Copy link
Contributor

You have a custom primary key protected $primaryKey = 'corpkey'; is it necessary ? So maybe you can try to use a default id primary key.
And I don't see your person migration file.

@tortuetorche
Copy link
Contributor

To keep your custom primary key, you can try to do this in your Person model:

class Person extends Eloquent implements UserInterface, RemindableInterface {
//...
    public function roles()
    {
        return $this->belongsToMany('Role', 'person_role', 'person_id', 'role_id');
    }

    public function permissions()
    {
        return $this->hasMany('Permission', 'person_id');
    }
//...
}

@tortuetorche
Copy link
Contributor

You can ask for more help on http://laravel.io/forum because now your issue is related to Eloquent associations with a custom primary key.

Have a good day,
Tortue Torche

@riemers
Copy link
Author

riemers commented May 27, 2014

Above snippet didn't resolve the issue. When i removed my primary key it did work as expected so yes indeed this is then a laravel/eloquent issue. Thanks for your help, i'll see if i can work around not using that primary key since your authority package looks the nicest :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants