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

[Feature]: Auto generate multiple relationship in the models and relationships #21

Open
edster opened this issue Oct 20, 2016 · 0 comments

Comments

@edster
Copy link
Contributor

edster commented Oct 20, 2016

Would be good to generate the reverse relationship for fields automatically rather then having to enter them all the time.

In the below you have staff members that are assigned to groups, you can get the group from the staff no problem, however the reverse needs to be added in so you can say give me all the members that belong to this group.

See below example.

<?php namespace Emange\StaffModule\Staff\Group;

use Emange\StaffModule\Staff\Group\Contract\GroupInterface;
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
use Emange\StaffModule\Staff\Group\Command\GetGroupStream;
use Anomaly\Streams\Platform\Model\Staff\StaffGroupsEntryModel;

/**
 * Class GroupModel
 *
 * @link      http://edi.mange.biz
 * @author    Emange. <edi@mange.biz>
 * @package   Emange\StaffModule\Staff\Group
 */

class GroupModel extends StaffGroupsEntryModel implements GroupInterface
{
    /**
     * Disable timestamps for this model.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The number of minutes to cache query results.
     *
     * @var null|false|int
     */
    protected $ttl = false;

    /**
     * The attributes that are
     * not mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * Runtime cache.
     *
     * @var array
     */
    protected $cache = [];

    /**
     * The cache time.
     *
     * @var int
     */
    protected $cacheMinutes = 99999;

    /**
     * Get the name.
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Get the slug.
     *
     * @return string
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * Get the description.
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->desciption;
    }

    /**
     * Get the related entry stream.
     *
     * @return StreamInterface
     */
    public function getEntryStream()
    {
        return $this->dispatch(new GetGroupStream($this));
    }

    /**
     * Get the related entry model name.
     *
     * @return string
     */
    public function getEntryModelName()
    {
        $stream = $this->getEntryStream();

        return $stream->getEntryModelName();
    }


    /**
     * Get related staff.
     *
     */
    public function getStaff()
    {
        return $this->staff;
    }

    /**
     * Return the staff relationship.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function staff()
    {
        return $this->hasMany('Emange\StaffModule\Staff\Staff\StaffModel', 'group_id');
    }

}

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

1 participant