开发者

Problem with CakePHP find function - Does not behave as desired - returns duplicates

开发者 https://www.devze.com 2023-03-06 15:27 出处:网络
First of all, thanks for looking. I am learning CakePHP and setting an app as I learn. I\'ve come up to a problem that I dont seem to understand how to solve.

First of all, thanks for looking. I am learning CakePHP and setting an app as I learn. I've come up to a problem that I dont seem to understand how to solve.

I have an articles controller with the following function:

function getRelated(){
    $relatedarticles = $this->Article->find(
                        'all',
                        array(
                             'fields' => array(
                            'Article.title'
                            ),
                             'limit' => 4,
                             'order' => 'Article.id ASC',
                             'recursive' => 1,
                             'conditions' => array(
                            'Article.category_id =' => '1'
                             )
                             )
                        );

    if (!empty($this->params['requested'])) {
        return $relatedarticles;
    } else {
        $this->set(compact('relatedarticles'));
    }
}

I also have setup an element with the following code which I can use throughout my app:

<div id="articles_view_related">
    Related News
    <?php 
        $relatedarticles = $this->requestAction('/articles/getRelated');
        //debug($relatedarticles);
    ?>
    <ul>
        <?php
            foreach($relatedarticles as $relatedarticle){
        ?>
        <li>
            <?php
                echo $relatedarticle['Article']['title'];
            ?>
        </li>
        <?php
            }
        ?>
    </ul>

</div>

As you can see from the code I have 'limit' set to 4, and 'category_id' set to 1. Basically, all I am looking to do is have CakePHP return 4 article titles where their category_id is 1.

MY PROBLEM: It is ONLY returning the first record in the database with the requested category_id but 4 times.:

Basically:

It returns:

  • News article number 1
  • News article number 1
  • News article number 1
  • News article number 1

When I need it to return something like this:

  • News article number 1
  • News article number 2
  • News article number 6
  • News article number 10

Please, if anyone could shed some light into this I would certainly appreciate. I have spent hours trying to figure this out.

Thanks alot,

Andre S.

UPDATED WITH CAKEPHP DEBUG OUTPUT:

Array
(
[0] => Array
    (
        [Article] => Array
            (
                [title] => Djabraba recognized as the most underdeveloped island in开发者_如何学Python the world here with us djabraba is here
                [id] => 1
            )

        [Attachment] => Array
            (
            )

        [Comment] => Array
            (
            )

    )

[1] => Array
    (
        [Article] => Array
            (
                [title] => Djabraba recognized as the most underdeveloped island in the world here with us djabraba is here
                [id] => 1
            )

        [Attachment] => Array
            (
            )

        [Comment] => Array
            (
            )

    )

[2] => Array
    (
        [Article] => Array
            (
                [title] => Djabraba recognized as the most underdeveloped island in the world here with us djabraba is here
                [id] => 1
            )

        [Attachment] => Array
            (
            )

        [Comment] => Array
            (
            )

    )

[3] => Array
    (
        [Article] => Array
            (
                [title] => Djabraba recognized as the most underdeveloped island in the world here with us djabraba is here
                [id] => 1
            )

        [Attachment] => Array
            (
            )

        [Comment] => Array
            (
            )

    )

)


The problem was somekind of bad relation that I am trying to understand:

I have the following relation in my articles.php model file:

var $belongsTo = array(
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'category_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

I removed the user relation and it now works good for this, but causing problems where user relation is needed.. does anyone know why?

[SOLVED] - SEE COMMENT BELOW...

0

精彩评论

暂无评论...
验证码 换一张
取 消