Virtual fields problem

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Virtual fields problem

Sam Clauw-2
Hi 2 all,

I'm working on my first CakePHP blog project: http://kattenbelletjes.be/
As you can see, there's a footer section that shows the top 25 most popular tags.

There are three relevant tables I use the implement those popular tags:

POSTS: id, title, content, slug
TAGS: id, name, slug
POST_TAG_LINK: id, post_id, tag_id

I tried to make a CakePHP query via $this->tag->find, but there was a persistent SQL error that I couldn't fix.
So, I tried it on the "$this->tag->query" SQL way:

debug($this->Tag->query(
   
"SELECT
        Tag.name,
        COUNT(PostTagLink.id) AS aantal
    FROM
        tags AS Tag
    INNER JOIN
        post_tag_links AS PostTagLink
    ON
        tag.id = PostTagLink.tag_id
    WHERE
        Tag.show = 'Y'
    GROUP BY
        Tag.name
    ORDER BY
        Tag.name ASC"

));

The problem is that the output array isn't very nice:

array(
(int) 0 => array(
'Tag' => array(
'name' => 'Beauty'
),
(int) 0 => array(
'aantal' => '2'
)
),
(int) 1 => array(
'Tag' => array(
'name' => 'Koken'
),
(int) 0 => array(
'aantal' => '1'
)
),
(int) 2 => array(
'Tag' => array(
'name' => 'Lente'
),
(int) 0 => array(
'aantal' => '2'
)
),
(int) 3 => array(
'Tag' => array(
'name' => 'Wonen'
),
(int) 0 => array(
'aantal' => '4'
)
)
)

I wan't something like this instead:

array(
(int) 0 => array(
'Tag' => array(
'name' => 'Beauty',
            'count' => '2'
)
),
(int) 1 => array(
'Tag' => array(
'name' => 'Koken',
            'count' => '1'
)
),
(int) 2 => array(
'Tag' => array(
'name' => 'Lente',
            'count' => '2'
)
),
(int) 3 => array(
'Tag' => array(
'name' => 'Wonen',
            'count' => '4'
)
)
)

Is there somebody with a solution on this?
Thanks 4 helping ;) 

--
Sign up for our Newsletter for updates.
http://cakephp.org/newsletter/signup
 
We will soon be closing this Google Group. But don't worry, we have something better coming. Stay tuned for an updated from the CakePHP Team soon.
 
Like Us on FaceBook https://www.facebook.com/CakePHP
Follow us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Virtual fields problem

LeHung
How about this?

$array = Hash::combine($array, '{n}Tag.name,{n}0.aantai,{n}');

On Fri, Mar 11, 2016 at 4:14 AM, Sam Clauw <[hidden email]> wrote:
Hi 2 all,

I'm working on my first CakePHP blog project: http://kattenbelletjes.be/
As you can see, there's a footer section that shows the top 25 most popular tags.

There are three relevant tables I use the implement those popular tags:

POSTS: id, title, content, slug
TAGS: id, name, slug
POST_TAG_LINK: id, post_id, tag_id

I tried to make a CakePHP query via $this->tag->find, but there was a persistent SQL error that I couldn't fix.
So, I tried it on the "$this->tag->query" SQL way:

debug($this->Tag->query(
   
"SELECT
        Tag.name,
        COUNT(PostTagLink.id) AS aantal
    FROM
        tags AS Tag
    INNER JOIN
        post_tag_links AS PostTagLink
    ON
        tag.id = PostTagLink.tag_id
    WHERE
        Tag.show = 'Y'
    GROUP BY
        Tag.name
    ORDER BY
        Tag.name ASC"

));

The problem is that the output array isn't very nice:

array(
(int) 0 => array(
'Tag' => array(
'name' => 'Beauty'
),
(int) 0 => array(
'aantal' => '2'
)
),
(int) 1 => array(
'Tag' => array(
'name' => 'Koken'
),
(int) 0 => array(
'aantal' => '1'
)
),
(int) 2 => array(
'Tag' => array(
'name' => 'Lente'
),
(int) 0 => array(
'aantal' => '2'
)
),
(int) 3 => array(
'Tag' => array(
'name' => 'Wonen'
),
(int) 0 => array(
'aantal' => '4'
)
)
)

I wan't something like this instead:

array(
(int) 0 => array(
'Tag' => array(
'name' => 'Beauty',
            'count' => '2'
)
),
(int) 1 => array(
'Tag' => array(
'name' => 'Koken',
            'count' => '1'
)
),
(int) 2 => array(
'Tag' => array(
'name' => 'Lente',
            'count' => '2'
)
),
(int) 3 => array(
'Tag' => array(
'name' => 'Wonen',
            'count' => '4'
)
)
)

Is there somebody with a solution on this?
Thanks 4 helping ;) 

--
Sign up for our Newsletter for updates.
http://cakephp.org/newsletter/signup
 
We will soon be closing this Google Group. But don't worry, we have something better coming. Stay tuned for an updated from the CakePHP Team soon.
 
Like Us on FaceBook https://www.facebook.com/CakePHP
Follow us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
Sign up for our Newsletter for updates.
http://cakephp.org/newsletter/signup
 
We will soon be closing this Google Group. But don't worry, we have something better coming. Stay tuned for an updated from the CakePHP Team soon.
 
Like Us on FaceBook https://www.facebook.com/CakePHP
Follow us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.