Пример #1
0
 /**
  * Creates an intermediary model.
  *
  * @param ManyToManyField $field
  * @param Model           $model
  *
  * @return Model
  *
  * @since 1.1.0
  *
  * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**>
  */
 public function createManyToManyIntermediaryModel($field, $model)
 {
     $modelName = $model->meta->modelName;
     if (is_string($field->relation->toModel)) {
         $toModelName = Tools::resolveRelation($model, $field->relation->toModel);
     } else {
         $toModelName = $field->relation->toModel->meta->modelName;
     }
     $className = sprintf('%1$s_%2$s', $modelName, $field->name);
     $intermediaryClass = 'class %1$s extends %2$s{
         public function fields(){}
     }';
     $intermediaryClass = sprintf($intermediaryClass, $className, Model::getFullClassName());
     if (!class_exists($className, false)) {
         eval($intermediaryClass);
     }
     $from = strtolower($modelName);
     $to = strtolower($toModelName);
     if ($from == $to) {
         $to = sprintf('to_%s', $to);
         $from = sprintf('from_%s', $from);
     }
     $fields = [$from => ForeignKey::createObject(['to' => $modelName, 'dbConstraint' => $field->relation->dbConstraint, 'onDelete' => Delete::CASCADE]), $to => ForeignKey::createObject(['to' => $toModelName, 'dbConstraint' => $field->relation->dbConstraint, 'onDelete' => Delete::CASCADE])];
     $meta = ['dbTable' => $field->_getM2MDbTable($model->meta), 'verboseName' => sprintf('%s-%s relationship', $from, $to), 'uniqueTogether' => [$from, $to], 'autoCreated' => true];
     $className = '\\' . $className;
     /** @var $intermediaryObj Model */
     $intermediaryObj = new $className();
     $intermediaryObj->init($fields, ['meta' => $meta, 'registry' => $field->scopeModel->meta->registry]);
     return $intermediaryObj;
 }