Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6091123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:17:30+00:00 2026-05-23T12:17:30+00:00

I’ve got three tables posts tags posts_tags I need to write some controller logic

  • 0

I’ve got three tables

  • posts
  • tags
  • posts_tags

I need to write some controller logic that selects and sets all posts that have the tag work

This means querying the tags to find the id for the queried tag, checking in the associated posts_tags table to find matches, using the post_id to return the correct posts from the posts table.

I’m not sure how to even begin this query, I’m new to CakePHP and could use a hand… Please?

If it helps, here’s my posts model relationship:

var $hasAndBelongsToMany = array(
        'Tag' => array(
            'className' => 'Tag',
            'joinTable' => 'posts_tags',
            'foreignKey' => 'posts_id',
            'associationForeignKey' => 'tag_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

My Tag table is made up of two fields: id and tag
My PostsTag table is made up of three fields: id, tag_id and post_id
My Posts table is made up of four fields: id, title, body and created

I found some code on the CakePHP Book that shows the below code:

$this->Recipe->Tag->find('all', array('conditions'=>array('Tag.name'=>'Dessert')));

They suggested that it was a similar idea, so I attempted to adapt:

$this->Post->Tag->find('all', array('conditions'=>array('Tag.tag'=>'work')));

However, this has not worked. This returns all Posts without filtering.

I took the advice of @Leo and tried to adapt his code to mine:

function getArticleByTagSql($tag) {
        $dbo = $this->getDataSource();
        $subQuery = $dbo->buildStatement(
                        array(
                    'fields' => array('DISTINCT(ArticlesTag.article_id)'),
                    'table' => "articles_tags",
                    'joins' => array(
                        array('table' => 'tags',
                            'alias' => 'Tag',
                            'type' => 'INNER',
                            'conditions' => array('ArticlesTag.tag_id = Tag.id')
                        )
                    ),
                    'alias' => "ArticlesTag",
                    'conditions' => array("Tag.tag" => $tag),
                    'order' => null,
                    'group' => "ArticlesTag.article_id",
                    'limit' => null
                        ), 
                $this
        );
        $subQuery = ' Article.id  IN (' . $subQuery . ')';
        return $dbo->expression($subQuery);
    }

Controller:

$this->set('articles', $this->paginate(array(
            'conditions' => $this->Article->getArticleByTagSql('work')
        )));

However, whatever I type in the paginate() method as a key – in this case ‘conditions’ appears in the query and I can’t figure out why – I keep getting unknown column ‘conditions’ errors. The error is with the paginate function, the data returns correctly without it, but I cannot use the paginator without it. It’s like a catch 22

Cheers,
Dan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:17:30+00:00Added an answer on May 23, 2026 at 12:17 pm

    Daniel this question is actually answered in the Cake documentation when describing the HABTM relationship betweens Receipe/Tag

    I answered this question for someone who had a similar issue with an Article/Tag models, that answer used a subquery

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I need to clean up various Word 'smart' characters in user input, including but
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.