Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Foreign keys #2

Open
qurben opened this issue Mar 29, 2017 · 5 comments
Open

Foreign keys #2

qurben opened this issue Mar 29, 2017 · 5 comments

Comments

@qurben
Copy link
Member

qurben commented Mar 29, 2017

Make the framework understand foreign keys.

@qurben
Copy link
Member Author

qurben commented Apr 7, 2017

Notation idea:

Given this class

class Car extends PersistentEntity {
	public $id;
	public $num_wheels;
	public $brand;

        protected $brand_id;

	protected static $persistent_attributes = [
		'id' => array(T::Integer, false, 'auto_increment'),
		'num_wheels' => array(T::Integer),
		'brand_id' => array(T::Timestamp),
	];
	protected static $table_name = 'CiviSaldo';
	protected static $primary_key = ['id'];
}

Define foreign keys as follows:

protected static $foreign_keys = [['brand_id' => CarBrand::class, 'brand']];

I don't know how straightforward reading this would be, but it would be variable containing key -> class referenced, where the reference is stored.
or

protected static $foreign_keys = [['fk' => 'brand_id', 'ref' => CarBrand::class, 'in' => 'brand']];

Same as previous.

Or just

protected static $foreign_keys = [['brand' => CarBrand::class]];

Where a hidden foreign key field is created in the database.

@qurben
Copy link
Member Author

qurben commented Apr 7, 2017

The magic functions __get and __set can be used to only retrieve the referenced objects when needed.

It would also be nice to be able to set a flag to join a referenced object on database level, thus limiting the amount of database queries.

@qurben qurben mentioned this issue Apr 7, 2017
@qurben
Copy link
Member Author

qurben commented Apr 28, 2017

Other way around should also be possible

$relations = [
  'children' => Child::class
];

@brussee
Copy link

brussee commented Apr 30, 2017

If you are going to embark in this: make sure you can model many-to-many relationships nicely.
Example: any person can be a parent and a child; 1 parent can have multiple children; 1 child can have multiple parents.

@qurben
Copy link
Member Author

qurben commented May 1, 2017

My idea for this would be using a through model, which must be explicitely defined. This keeps behaviour transparent because no invisible tables have to be created. The way Django does it.

$relations = [
  'children' = [Child::class, ChildParentThrough::class] 
];

Something like that. Where the through has to fk's an can contain arbritrary data.

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

No branches or pull requests

2 participants