我想将这些数据从一个表添加到关系管理器中不是数据透视表的另一表中

时间:2018-07-28 05:07:49

标签: pivot octobercms

我想先将数据从主表克隆到克隆表中,然后再从主表中将数据克隆到插入表中,然后在插入数据透视表中将clone_id和master_id加上为关系管理器中的拖放功能的related_sort_order并查看在克隆表和在创建新记录时,也必须将其保存在克隆表中

例如: 主表:BLOCK 克隆表:ADDBLOCK

codE: 发布进行关系控制的模型

 public $belongsToMany = [
        'categories' => [
            'KajalSingh\Guide\Models\Category',
            'table' => 'kajalsingh_guide_posts_categories',
            'order' => 'name'
        ],

             'blocks' => [
                 'KajalSingh\Guide\Models\Block',
                 'table' => 'kajalsingh_guide_blocks_posts',
                 'pivot' => ['relation_sort_order'],


 'order'=>'kajalsingh_guide_blocks_posts.relation_sort_order'
                 ],
        ];
        public $sortableRelations = [
            'blocks'
        ];

模型块:

public $belongsToMany = [
        'posts' => [
            'KajalSingh\Guide\Models\Post',
            'table' => 'kajalsingh_guide_blocks_posts',
                 ],
    ];

模型addblock:

public $belongsToMany = [
        'posts' => [
            'KajalSingh\Guide\Models\Post',
            'table' => 'kajalsingh_guide_blocks_posts',
            'order' => 'published_at desc',
            'scope' => 'isPublished',

        ]
    ];

关系管理器代码: 在POST控制器中:

_block.htm:

<?= $this->relationRender('blocks') ?>

_title.htm:

<input type="hidden" id="<?= 'Lists-relationViewList-parent-id-'.$record->pivot->post_id ?>"
       value="<?= $record->pivot->post_id ?>">
<input type="hidden" id="<?= 'Lists-relationViewList-related-id-'.$record->pivot->block_id ?>"
       value="<?= $record->pivot->block_id ?>">
<?= $value ?>

congif_relation.yaml

blocks:
  label: Block
  view:
     list:
        columns:
              title:
                  label: Title
                  type: partial
              pivot[relation_sort_order]:
                  label: Order
                  type: number
                  invisible: false
  manage:
    list: $/kajalsingh/guide/models/block/columns.yaml
    form: $/kajalsingh/guide/models/block/fields.yaml
    showCheckboxes: true

config_sortable_ralation.yaml

modelClass: KajalSingh\Guide\Models\Post
relationName: blocks

我在RelationController中进行了更改

 public function onRelationManageAdd()
    {
        $this->beforeAjax();

        $recordId = post('record_id');
        $sessionKey = $this->deferredBinding ? $this->relationGetSessionKey() : null;

        /*
         * Add
         */
        if ($this->viewMode == 'multi') {

            $checkedIds = $recordId ? [$recordId] : post('checked');

            if (is_array($checkedIds)) {
                /*
                 * Remove existing relations from the array
                 */
                $existingIds = $this->findExistingRelationIds($checkedIds);
                log::info($existingIds);

                $checkedIds = array_diff($checkedIds, $existingIds);

                $foreignKeyName = $this->relationModel->getKeyName();

                $models = $this->relationModel->whereIn($foreignKeyName, $checkedIds)->get();





                foreach ($models as $model) {

                    $addblock=new AddBlock;
                    $addblock->title=$model['title'];
                    $addblock->slug=$model['slug'];
                    $addblock->content=$model['content'];
                    $addblock->content_html=$model['content_html'];
                    $addblock->published_at=$model['published_at'];
                    $addblock->published=$model['published'];
//                  $addblock->created_at=$model['created_at'];
////                $addblock->updated_at=$model['updated_at'];
                    $addblock->excerpt=$model['excerpt'];
                    $addblock->save();
                   // log::info($addblock);
                    $this->relationObject->add($model, $sessionKey);
                }
            }

        }

,我也使用了此软件包 fico7489 / laravel-pivot 进行排序/重新排序

有人帮助吗?

0 个答案:

没有答案
相关问题