Skip to content

Relation (in progress)

Arnun edited this page Dec 26, 2016 · 1 revision

Another thing in project.


###Relation Keyword (I May wrong) * Example Dummy is table name

one-to-many: "own"

//Library
$this->dataModel->ownBook = \R::load('book', 1);

many-to-one:

//Library
$this->dataModel->book_id = 2;

many-to-many: "shared"

//Book
$this->dataModel->sharedAuthor = \R::load('author', 1);

Retrieve Relation object

  • Model - SkillType
(little trick: I'm use annotation of column for easier to retrieve properties when foreach)
/**
 * @property int|array id
 * @property string name
 * @property array sharedSkill
 */
class SkillType extends RedBeanController
{
    function __construct($id = 0)
    {
        parent::__construct($id);
    }

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

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->dataModel->name = $name;
    }

    /**
     * @return array
     */
    public function getSkills()
    {
        return $this->dataModel->sharedSkill;
    }

    /**
     * @param $skills array Skill
     */
    public function setSkills($skills)
    {
        unset($this->dataModel->sharedSkill);
        if (is_array($skills)) {
            foreach ($skills as $skill) {
                $this->dataModel->sharedSkill[] = \R::load('skill', $skill);
            }
        }
    }

    public function addSkill($skill)
    {
        $this->dataModel->sharedSkill[] = \R::load('skill', $skill);
        iLog($skill);
    }
}
  • Skill Class is the same but only get, set Name

  • Display

        /**
         * @var $skType SkillType
         */
        foreach ($skillType->readAllAction() as $skType) {

            echo "<p>* {$skType->name}</p>";

            /**
             * @var $sk Skill
             */
            foreach ($skType->sharedSkill as $sk) {

                echo "<p> {$sk->id}--{$sk->name}</p>";
            }
        }

Clone this wiki locally