Check if Article has tag?

Hii there,

I’m trying to check whether my blog article has a specific tag (to automatically check a checkbox) based on what the value is in the Database.
This is the object I get from my query:

App\Model\Entity\Article Object
(
    [id] => 1
    [user_id] => 1
    [title] => test
    [slug] => test
    [body] => This is the first test message
    [published] => 1
    [created] => Cake\I18n\FrozenTime Object
        (
            [time] => 2018-06-05T00:00:00+00:00
            [timezone] => UTC
            [fixedNowTime] => 
        )

    [modified] => Cake\I18n\FrozenTime Object
        (
            [time] => 2018-06-05T00:00:00+00:00
            [timezone] => UTC
            [fixedNowTime] => 
        )

    [tags] => Array
        (
            [0] => App\Model\Entity\Tag Object
                (
                    [id] => 2
                    [title] => Test Message
                    [created] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2018-06-21T09:28:05+00:00
                            [timezone] => UTC
                            [fixedNowTime] => 
                        )

                    [modified] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2018-06-21T09:28:05+00:00
                            [timezone] => UTC
                            [fixedNowTime] => 
                        )

                    [_joinData] => App\Model\Entity\ArticlesTag Object
                        (
                            [tag_id] => 2
                            [article_id] => 1
                            [[new]] => 
                            [[accessible]] => Array
                                (
                                    [tag_id] => 1
                                    [article_id] => 1
                                )

                            [[dirty]] => Array
                                (
                                )

                            [[original]] => Array
                                (
                                )

                            [[virtual]] => Array
                                (
                                )

                            [[errors]] => Array
                                (
                                )

                            [[invalid]] => Array
                                (
                                )

                            [[repository]] => ArticlesTags
                        )

                    [[new]] => 
                    [[accessible]] => Array
                        (
                            [title] => 1
                            [created] => 1
                            [modified] => 1
                            [articles] => 1
                        )

                    [[dirty]] => Array
                        (
                        )

                    [[original]] => Array
                        (
                        )

                    [[virtual]] => Array
                        (
                        )

                    [[errors]] => Array
                        (
                        )

                    [[invalid]] => Array
                        (
                        )

                    [[repository]] => Tags
                )

            [1] => App\Model\Entity\Tag Object
                (
                    [id] => 3
                    [title] => Development
                    [created] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2018-06-21T09:28:05+00:00
                            [timezone] => UTC
                            [fixedNowTime] => 
                        )

                    [modified] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2018-06-21T09:28:05+00:00
                            [timezone] => UTC
                            [fixedNowTime] => 
                        )

                    [_joinData] => App\Model\Entity\ArticlesTag Object
                        (
                            [tag_id] => 3
                            [article_id] => 1
                            [[new]] => 
                            [[accessible]] => Array
                                (
                                    [tag_id] => 1
                                    [article_id] => 1
                                )

                            [[dirty]] => Array
                                (
                                )

                            [[original]] => Array
                                (
                                )

                            [[virtual]] => Array
                                (
                                )

                            [[errors]] => Array
                                (
                                )

                            [[invalid]] => Array
                                (
                                )

                            [[repository]] => ArticlesTags
                        )

                    [[new]] => 
                    [[accessible]] => Array
                        (
                            [title] => 1
                            [created] => 1
                            [modified] => 1
                            [articles] => 1
                        )

                    [[dirty]] => Array
                        (
                        )

                    [[original]] => Array
                        (
                        )

                    [[virtual]] => Array
                        (
                        )

                    [[errors]] => Array
                        (
                        )

                    [[invalid]] => Array
                        (
                        )

                    [[repository]] => Tags
                )

            [2] => App\Model\Entity\Tag Object
                (
                    [id] => 4
                    [title] => Hidden Message
                    [created] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2018-06-21T09:28:05+00:00
                            [timezone] => UTC
                            [fixedNowTime] => 
                        )

                    [modified] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2018-06-21T09:28:05+00:00
                            [timezone] => UTC
                            [fixedNowTime] => 
                        )

                    [_joinData] => App\Model\Entity\ArticlesTag Object
                        (
                            [tag_id] => 4
                            [article_id] => 1
                            [[new]] => 
                            [[accessible]] => Array
                                (
                                    [tag_id] => 1
                                    [article_id] => 1
                                )

                            [[dirty]] => Array
                                (
                                )

                            [[original]] => Array
                                (
                                )

                            [[virtual]] => Array
                                (
                                )

                            [[errors]] => Array
                                (
                                )

                            [[invalid]] => Array
                                (
                                )

                            [[repository]] => ArticlesTags
                        )

                    [[new]] => 
                    [[accessible]] => Array
                        (
                            [title] => 1
                            [created] => 1
                            [modified] => 1
                            [articles] => 1
                        )

                    [[dirty]] => Array
                        (
                        )

                    [[original]] => Array
                        (
                        )

                    [[virtual]] => Array
                        (
                        )

                    [[errors]] => Array
                        (
                        )

                    [[invalid]] => Array
                        (
                        )

                    [[repository]] => Tags
                )

        )

    [[new]] => 
    [[accessible]] => Array
        (
            [user_id] => 1
            [title] => 1
            [slug] => 1
            [body] => 1
            [published] => 1
            [created] => 1
            [modified] => 1
            [user] => 1
            [tags] => 1
        )

    [[dirty]] => Array
        (
        )

    [[original]] => Array
        (
        )

    [[virtual]] => Array
        (
        )

    [[errors]] => Array
        (
        )

    [[invalid]] => Array
        (
        )

    [[repository]] => Articles
)

Now, I need to know if my article has a specific tag (eg. tag_id => 1).
Does anyone know how I could accomplish this?
I have passed this array to the view as well, so it’s accessible from there.

If I understood well you need this https://book.cakephp.org/3.0/en/core-libraries/collections.html#Cake\Collection\Collection::some

But then the thing is, how do I get it to the view?
I could just make a foreach loop and slam everything into another array, but this might be inefficient when a lot of tags get added to a specific article (which won’t happen for a while, but can happen in the future)

I was able to do this with a Helper really easily…

1 Like