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

Improve Quick Start Guide #22

Open
matthiasmoritz opened this issue Sep 29, 2015 · 14 comments
Open

Improve Quick Start Guide #22

matthiasmoritz opened this issue Sep 29, 2015 · 14 comments

Comments

@matthiasmoritz
Copy link
Contributor

After following the instructions in the Quick Start Guide, the tags field (e.g. in edit.ctp) is not auto-polulated with the tags.

@jadb
Copy link
Member

jadb commented Oct 1, 2015

How do you suggest improving it?

@acellam
Copy link

acellam commented Dec 30, 2015

@jadb can you add screenshots, a picture is worth 1000 words

@jadb
Copy link
Member

jadb commented Jan 8, 2016

@mistaguy, screenshots of?

@zhunt
Copy link

zhunt commented Jan 30, 2016

Agree with matthiasmoritz I've been spending about 3 hours so far going though the code trying to figure out how to display the tags once they're added.

@rudy1976s
Copy link

@zhunt I have the same problem: I was able to add the tag but was not able to load it : any hint?

@elboletaire
Copy link

I've not tried the plugin but taking a look to the behavior it adds relation to the Tagged model. So retrieving tags should be as easy as using contain:

$this->Posts->find('all', ['contain' => ['Tagged.Tags']]);

@chrmina
Copy link

chrmina commented Aug 10, 2016

This works:

$this->Posts->find('all', ['contain' => ['Tags']]);

and in your view simply loop through the tags:

   <th><?= __('Tags') ?></th>
    <td>
      <?php foreach ($post->tags as $tag): ?>
      <?= h($tag->label) ?>
      <?php endforeach; ?>
    </td>

@virusvn
Copy link

virusvn commented Oct 25, 2016

I used this way to modify tags:

Controller

$delimiter = ','; // same as delimiter at TagBehavior

$tags = [];
foreach ($post->tags as $tag):
$tags[] = $tag->label;
endforeach;
$post->tags = implode($delimiter, $tags);

View
echo $this->Form->input('tags');

@chrmina
Copy link

chrmina commented Dec 2, 2016

How to edit associated tags? In my edit.ctp, in the input for tags instead of seeing the tag name I see the whole object. For example I have 3 tags associated with a specific article: "test1", "test2" and "test3". When the form is rendered, in the input I get this:
{ "id": "789e9a86-a341-4401-a338-629f4f940dea", "namespace": null, "slug": "", "label": "test3", "counter": 2, "created": "2016-10-25T04:38:59+00:00", "modified": "2016-10-25T04:52:31+00:00", "tag_key": "test3", "_joinData": { "id": "7dee31c6-5949-4aed-82a0-cc043d1fd257", "tag_id": "789e9a86-a341-4401-a338-629f4f940dea", "fk_id": "5b2efebb-6823-496c-99f9-61f11fbe8d4a", "fk_table": "documents", "created": "2016-10-25T04:40:47+00:00", "modified": "2016-10-25T04:40:47+00:00" }} { "id": "e04e4b6e-e705-4dc0-8d30-dfdbc3e015d9", "namespace": null, "slug": "", "label": "test2", "counter": 2, "created": "2016-08-10T07:13:12+00:00", "modified": "2016-10-25T04:52:31+00:00", "tag_key": "test2", "_joinData": { "id": "b396dd1b-8ce2-4719-bd80-b8e443512b6b", "tag_id": "e04e4b6e-e705-4dc0-8d30-dfdbc3e015d9", "fk_id": "5b2efebb-6823-496c-99f9-61f11fbe8d4a", "fk_table": "documents", "created": "2016-10-25T04:39:22+00:00", "modified": "2016-10-25T04:39:22+00:00" }} { "id": "ca8bb2ca-ebe2-4ba0-bbb4-b96212cee718", "namespace": null, "slug": "", "label": "test1", "counter": 2, "created": "2016-08-10T07:13:12+00:00", "modified": "2016-10-25T04:52:31+00:00", "tag_key": "test1", "_joinData": { "id": "d9d93a45-0f55-4921-8335-16dec90f6059", "tag_id": "ca8bb2ca-ebe2-4ba0-bbb4-b96212cee718", "fk_id": "5b2efebb-6823-496c-99f9-61f11fbe8d4a", "fk_table": "documents", "created": "2016-10-25T04:39:22+00:00", "modified": "2016-10-25T04:39:22+00:00" }}

Any advise?

@chrmina
Copy link

chrmina commented Dec 2, 2016

@virusvn your solution gives me the following warnings:

Warning (2): Invalid argument supplied for foreach() [CORE\src\ORM\Marshaller.php, line 760]
Warning (2): Invalid argument supplied for foreach() [CORE\src\ORM\Marshaller.php, line 641]

Suggestions to get rid of these?

@virusvn
Copy link

virusvn commented Dec 2, 2016

@chrmina
I didn't get any problems.
Where did you get that errors (Request Save or Get)?

@chrmina
Copy link

chrmina commented Dec 2, 2016

@virusvn the errors come just after saving. Here is my edit method:

public function edit($id = null)
    {
        $document = $this->Documents->get($id, [
            'contain' => ['Tags']
        ]);

        // This results in 2 warnings. Need to fix
        $delimiter = ','; // same as delimiter at TagBehavior
        $tags = [];
        $alltags = $document->tags;
        foreach ($alltags as $tag):
          $tags[] = $tag->label;
        endforeach;
        $document->tags = implode($delimiter, $tags);

        if ($this->request->is(['patch', 'post', 'put'])) {
            $document = $this->Documents->patchEntity($document, $this->request->data);
            if ($this->Documents->save($document)) {
                $this->Flash->success(__('The document has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The document could not be saved. Please, try again.'));
            }
        }
        $this->set(compact('document'));
    }

@virusvn
Copy link

virusvn commented Dec 7, 2016

@chrmina sorry for late reply,
My code is used for request is GET, not for save action.
For save the document, you don't need to implode tags array, because $document->tags is an array, not a string. So $document->tags = implode($delimiter, $tags); is not correct.

`

public function edit($id = null){

    $document = $this->Documents->get($id, [
        'contain' => ['Tags']
    ]);
    $delimiter = ','; // same as delimiter at TagBehavior
    //$this->request->data['tags'] should be an array, if in case $this->request->data['tags'] is a string, must convert to array: 
     // $this->request->data['tags'] = explode($delimiter, $this->request->data['tags']));
    if ($this->request->is(['patch', 'post', 'put'])) {
        $document = $this->Documents->patchEntity($document, $this->request->data);
        if ($this->Documents->save($document)) {
            $this->Flash->success(__('The document has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The document could not be saved. Please, try again.'));
        }
    }

   // I placed that code at this line, to convert tags array to string, for input box (I used Twitter Typeahead, so when we submit form '$this->request->data['tags']' is an array)

    $tags = [];
    $alltags = $document->tags;
    foreach ($alltags as $tag):
      $tags[] = $tag->label;
    endforeach;
    $document->tags = implode($delimiter, $tags);
    $this->set(compact('document'));
}`

Sorry for have no-good explanation

@chrmina
Copy link

chrmina commented Dec 12, 2016

@virusvn thank you for your reply. The problem was resolved by simply rearranging the code as below:

    public function edit($id = null)
    {
        $document = $this->Documents->get($id, ['contain' => ['Tags']]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $document = $this->Documents->patchEntity($document, $this->request->data);
            if ($this->Documents->save($document)) {
                $this->Flash->success(__('The document has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The document could not be saved. Please, try again.'));
            }
        }
        // Manage Tags
        $delimiter = ','; // same as delimiter at TagBehavior
        $tags = [];
        $alltags = $document->tags;
        foreach ($alltags as $tag):
          $tags[] = $tag->label;
        endforeach;
        $document->tags = implode($delimiter, $tags);
        $this->set(compact('document'));
    }

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

8 participants