serverfireteam / rapyd-laravel

Clone of rapyd for laravelpanel
MIT License
4 stars 12 forks source link

extended tags input with belongsToMany relationships #8

Open jbajou opened 8 years ago

jbajou commented 8 years ago

Hello,

I extended the Tags field because I needed it not to request data from database. This works just fine. I'm trying now to save the entered tags to database.

I have a blog_posts table and a blog_post_tags. A pivot table blog_post_blog_post_tag has been created containing the id of the post and the id of the tag.

The BlogPost model has a method as follow:

public function blogPostTag() { return $this->belongsToMany('App\BlogPostTag', 'blog_post_blog_post_tag', 'blog_post_id', 'blog_post_tag_id'); }

The BlogPostTag model has a method as follow: public function blogPost() { return $this->belongsToMany('App\BlogPost', 'blog_post_blog_post_tag', 'blog_post_tag_id', 'blog_post_id'); }

When I try to save data, the content of the input is sent to the query, so Laravel tries to save both the blog_post_id and the content of the input in the pivot table. Here is the error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (dbname.blog_post_blog_post_tag, CONSTRAINTblog_post_blog_post_tag_blog_post_tag_id_foreignFOREIGN KEY (blog_post_tag_id) REFERENCESblog_post_tags(id)) (SQL: insert intoblog_post_blog_post_tag(blog_post_id,blog_post_tag_id) values (1, super,test))

What can I do so it works as expected, i.e. add a line in pivot table for each tag, and save the tag in its table if needed. I think I miss something in my extended TagField... Here is the code so far:

`class TagsField extends \Zofe\Rapyd\DataForm\Field\Field {

public $type = 'tags_field';
public $record_id;
public $record_label;

public $output;
public $extra_output;

public $attributes;

public $local_options;

public function options($options) {
    parent::options($options);
    foreach ($options as $key=>$value) {
        $row = new \stdClass();
        $row->key = $key;
        $row->value = $value;
        $this->local_options[] =$row;
    }
    return $this;
}

public function getValue() {
    parent::getValue(); 
}

public function build() {
    $output = '';
    Rapyd::css('autocomplete/bootstrap-tagsinput.css');
    Rapyd::js('autocomplete/bootstrap-tagsinput.min.js');

    unset($this->attributes['type']);
    if (parent::build() === false) return;

    switch ($this->status) {
        case "disabled":
        case "show":
            if ( (!isset($this->value)) ) {
                $output = $this->layout['null_label'];
            } else {
                $output = $this->description;
            }
            $output = "<div class='help-block'>".$output."&nbsp;</div>";
        break;
        case "create":
        case "modify":
            $output  =  \Form::text($this->name, '', array_merge($this->attributes, array('id'=>"".$this->name)))."\n";

            $script = <<<acp

            $('#{$this->name}').tagsinput({
                confirmKeys: [9, 13, 188],
                trimValue: true
            });

acp;

            Rapyd::script($script);
        break;
        case "hidden":
            $output = Form::hidden($this->db_name, $this->value);
            break;
        default:;
    }
    $this->output = "\n".$output."\n". $this->extra_output."\n";
}

}`

Thanks in advance for your help

jbajou commented 8 years ago

Any idea anybody ? Still stuck there...

AlirezaAlgo commented 7 years ago

did you still need help ?

jbajou commented 7 years ago

Hi

We got that sorted out: I'll try to post some snippets asap

On Nov 26, 2016, at 11:43 AM, Serverfire notifications@github.com wrote:

did you still need help ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

AlirezaAlgo commented 7 years ago

Please send a pull request so we can add that filed type

AlirezaAlgo commented 7 years ago

@jbajou still waiting