コード例 #1
0
    /**
     * Get Pivot model
     *
     * @return \ORM\Model
     */
    function pivot()
    {
        list($t1, $t2) = explode('_', $this->tableName);
        $t1 = Inflector::singularize($t1);
        $t2 = Inflector::singularize($t2);
        $className = Inflector::classify(implode('_', [$t1, $t2]));
        $fk1 = $this->getForeignKey();
        $fk2 = $this->getForeignKeyRelated();
        if (!class_exists($className, false)) {
            $code = <<<PIVOT
                class {$className} extends \\ORM\\Model
                {
                    static function getTable()
                    {
                        return '{$this->tableName}';
                    }

                    function validate()
                    {
                        if(!is_numeric(\$this->{$fk1})) {
                            \$this->addError('{$fk1} must be numeric');
                        }

                        if(!is_numeric(\$this->{$fk2})) {
                            \$this->addError('{$fk2} must be numeric');
                        }
                    }
                }
PIVOT;
            eval($code);
        }
        return new $className();
    }