Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extended tags input with belongsToMany relationships #8

Open
jbajou opened this issue Feb 25, 2016 · 5 comments
Open

extended tags input with belongsToMany relationships #8

jbajou opened this issue Feb 25, 2016 · 5 comments

Comments

@jbajou
Copy link

jbajou commented Feb 25, 2016

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
Copy link
Author

jbajou commented Feb 29, 2016

Any idea anybody ? Still stuck there...

@AlirezaAlgo
Copy link

did you still need help ?

@jbajou
Copy link
Author

jbajou commented Nov 26, 2016 via email

@AlirezaAlgo
Copy link

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

@AlirezaAlgo
Copy link

@jbajou still waiting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants