neat-php / object

Neat Object components
MIT License
4 stars 1 forks source link

Relations #1

Closed baukevdw closed 5 years ago

baukevdw commented 6 years ago

For the 1.0 release we'll need relations. It should be functioning something like this:

class User {
    /**
     * The `address` table should have a `user_id` column which matches te primary key of the `user` table
     * The `user_id` column should have a unique constraint
     * 
     * @return HasOne
     */
    public function address() {
        return $this->hasOne(Address::class);
    }

    /**
     * The `preference` table should have a `user_id` column which mathces the primary key of the `user` tale
     * 
     * @return HasMany
     */
    public function preferences() {
        return $this->hasMany(Preference::class);
    }

    /**
     * A user belongs to many roles
     * The junction table `role_user` should be determined by the policy
     * The junction table should have a `user_id` column which matches the primary key of the `user` table
     * The junction table should have a `role_id` column which matches the primary key of the `role` table
     * The foreign keys should be determined by the policy
     * 
     * @return BelongsToMany
     */
    public function roles() {
        return $this->belongsToMany(Role::class)
    }
}

class Address {
    /**
     * The `address` table should have a `user_id` column which matches te primary key of the `user` table
     * The `user_id` column should have a unique constraint
     * 
     * @return BelongsToOne
     */
    public function user() {
        return $this->belongsToOne(User::class);
    }
}

class Preference {
    /**
     * The `preference` table should have a `user_id` column which mathces the primary key of the `user` tale
     * 
     * @return BelongsToOne
     */
    public function user() {
        return $this->belongsToOne(User::class);
    }
}

class Role {
    /**
     * A role belongs to many users
     * The junction table `role_user` should be determined by the policy
     * The junction table should have a `user_id` column which matches the primary key of the `user` table
     * The junction table should have a `role_id` column which matches the primary key of the `role` table
     * The foreign keys should be determined by the policy
     * 
     * @return BelongsToMany
     */
    public function users() {
        return $this->belongsToMany(User::class)
    }
}
baukevdw commented 5 years ago

Released in 0.4.0