tattersoftware / codeigniter4-schemas

Database schema management, for CodeIgniter 4
MIT License
23 stars 10 forks source link

how to override schema for few fields #19

Closed eafarooqi closed 3 years ago

eafarooqi commented 3 years ago

hi,

cannot rename some fields, can you please provide examples how to override the default schema.

MGatner commented 3 years ago

Full docs are not done for this library. Most likely what you want to do is use the PHP drafter (src/Drafter/Handlers/PhpHandler.php) to create that portion of the Schema manually. Note that this will still merge with whatever other drafters you use. See an example of a manual schema in the tests: (tests/_support/Schemas/Good/Products.php).

eafarooqi commented 3 years ago

its very difficult to create a schema without documentation. Tool is great but without documentation, i am finding it almost impossible to use it.

may be if you can provide some examples, i can try to write the proper documentation. Right now i am tyring with the following db structure to override the schema,

Tables: users: id name

meetings: id title

meetings_users: id meeting_id member_id (this works with 'user_id' but i cannot change it.)

MGatner commented 3 years ago

I would love some docs, but my time is very tight and it isn't something I can work on right now. This library and Relations are both a bit behind on docs and tests because they are due for a dev toolkit pass and likely new major versions which could require doc changes.

In the meantime you can reference the files I linked above to make your overrides. In your case the Database Drafter should catch everything except the mismatched names between users and member_id, so you could try overriding that with something like this:

$schema = new Schema();
$schema->tables->users  = new Table('users');

$relation = new Relation();
$relation->type   = 'manyToMany';
$relation->table  = 'meetings';
$relation->pivots = [
    ['users', 'id', 'meetings_users', 'member_id'],
    ['meetings_users', 'meeting_id', 'meetings', 'id'],
];
$schema->tables->users->relations->meetings = $relation;

You might have to create the reverse relationship as well.

eafarooqi commented 3 years ago

Thanks, i am still getting the error " Table meetings is not known to be related to users "

may be because of the reverse relation missing ?

MGatner commented 3 years ago

Possible, or your schema might also be cached.

eafarooqi commented 3 years ago

cache is not enabled, so i think its the reverse relation

eafarooqi commented 3 years ago

this seems to work, data is getting loaded. i hope this is the correct way to create the reverse relation

// Users->meetings
$schema->tables->users  = new Table('users');

$relation = new Relation();
$relation->type   = 'manyToMany';
$relation->table  = 'meetings';
$relation->pivots = [
    ['users', 'id', 'meetings_users', 'member_id'],
    ['meetings_users', 'meeting_id', 'meetings', 'id'],
];
$schema->tables->users->relations->meetings = $relation;

// meetings->users
$schema->tables->meetings  = new Table('meetings');

$relation = new Relation();
$relation->type   = 'manyToMany';
$relation->table  = 'users';
$relation->pivots = [
    ['meetings', 'id', 'meetings_users', 'meeting_id'],
    ['meetings_users', 'member_id', 'users', 'id'],
];
$schema->tables->meetings->relations->users = $relation;
MGatner commented 3 years ago

If it is working then you're probably good! Your code would be more readable with triple back-ticks.

In the future please take support questions to the CodeIgniter Forums for community support, or try the sponsorship.