开发者

CakePHP - How can I count images per album in Album-paginate? (Image belongsTo Album)

I am currently writing my first CakePHP site and it's still all a bit new to me. I am working on an Image Gallery. I would like to have picture counts per album on the frontpage.

Models:

<?php
class Album extends AppModel {
        var $name = 'Album';
        var $displayField = 'name';

        var $belongsTo = array(
                'ParentAlbum' => array(
                        'className' => 'Album',
                        'counterCache' => true,
                        'foreignKey' => 'parent_id'
                )
        );

        var $hasMany = array(
                'ChildAlbum' => array(
                        'className' => 'Album',
                        'foreignKey' => 'parent_id',
                        'dependent' => false
                ),
                'Image' => array(
                        'className' => 'Image',
                        'foreignKey' => 'album_id',
                        'dependent' => false
                )
        );
}

<?php
class Image extends AppModel {
        var $name = 'Image';
        var $displayField = 'Name';
        var $belongsTo = array(
                'Album' => array(
                        'className' => 'Album',
                        'counterCache' => true
                )
        );
}

Controller:

[chronoz@directadmin01 public_html]# cat controllers/albums_controller.php
<?php
class AlbumsController extends AppController {

        var $name = 'Albums';

        var $paginate = array(
        'limit' => 95,
        'order' => array(
        'Album.name' => 'asc')
        );

        function index() {
                $this->Album->recursive = 0;
                $this->set('albums', $this->paginate());
                $this->set("title_for_layout","CakeGal");
        }

        function view($id = null) {
                if (!$id) {
                        $this->Session->setFlash(__('Invalid album', true));
                        $this->redirect(array('action' => 'index'));
                }
                $this->set('album', $this->Album->read(null, $id));
                $this->set("title_for_layout", $this->开发者_如何学编程;Album->data['Album']['name']);
        }
}

View:

[chronoz@directadmin01 public_html]# cat views/albums/index.ctp
<?php foreach ($albums as $album): ?>
<li class="thumbnail" style="width: 164px; margin-left: 26px;">
<a class="collection-image-link" href="/albums/view/<?php echo $album['Album']['id']; ?>
"><img class="photos" src="/thumbs/<?php echo $album['Album']['id']; ?>.jpg" title="" alt="" /></a>
<h2><a href="/albums/view/<?php echo $album['Album']['id']; ?>"><?php echo $album['Album']['name']; ?></a></h2>
<p class="album-meta">Contains <?php echo $album_count; ?> pictures</p>
</li><!-- end .album -->
<?php endforeach; ?>

General advice would also be welcome! It's after all my first CakePHP project and first time I am asking for help on a forum about cakephp. I am able to count the number of albums, but I want to count the number of images per album and post them as $album_count.


The foreign key between Image and Album should be album_id, not parent_album, you should change the field's name in your database. Follow the Cake convention.

To keep a count of images in an album: use counterCache so you don't have to keep track of the count yourself, and it'll be available in the album record.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜