Question

Hi Im making a gallery on my news website for my portfolio having a minor glitch however. I'm trying to select a distinct gallery so that it doesn't repeat itself, this is the code I have so far

function viewgalleries() {

    $viewgalleries = $this->Gallery->find('all', array(
        'fields' => 'DISTINCT Gallery.gallery_name'));
    if (!empty($this->request->params['requested'])) {
return $viewgalleries; }
    $this->set('gallery', $viewgalleries); 

}

and this is my view..

     <?php $viewgalleries = $this->requestAction('/Galleries/viewgalleries');?>

    <div class="allgalleries">

 <table class="galleries">
<?php foreach ($viewgalleries as $galleries): ?>
    <tr>
       <td>
         <span class="gallerylink">
            <?php echo $this->Html->link($galleries['Gallery']['gallery_name'], 
                  array('controller' => 'Gallery', 'action' => 'view', 
                  $galleries['Gallery']['id'])); ?>
         </span>
       </td>
    </tr>
  <?php endforeach; ?>
  </table>
    </div>

This gives me want I want however it isn't selecting the id field in the table so its throwing me this error...

  Notice (8): Undefined index: id [APP/View/Galleries/viewgalleries.ctp, line 10]

Below it does show me the name of the gallery however Champions League Final 2012

Any ideas as to how I could still select the id from the database??

Thanks in advance or any help.

Was it helpful?

Solution 3

Instead of using the select distinct function as it proved to be quite tricky I instead decided to re-format my gallery my adding validation techniques such isUnique in order to let allow the passing of the same gallery name, this was achieved in the Models/Gallery.php with the following statement:

public $validate = array(
    'gallery_name' => array(
        'rule' => 'isUnique',
        'message' => 'This gallery already exists.'));

Hope this helps others.

OTHER TIPS

Try changing the fields argument key in the find like this:

'fields' => array('DISTINCT Gallery.gallery_name, Gallery.id')

This will work for you. Kindly ask if it not worked.

Тry changing the fields key in the find like this:

'fields' => array('DISTINCT Gallery.gallery_name', 'Gallery.id')

Also can you try:

$this->ModelName->query('SELECT 
                 DISTINCT Gallery.gallery_name,
                 Gallery.id 
                 FROM kickoff.galleries
                 AS Gallery WHERE 1 = 1');

Also there is another workaround that can solve your problem. A query construction as the following would yield the same result set as SELECT DISTINCT:

select * from TABLE group by FIELDNAME having count(FIELDNAME)>=1;

So in your case it should be:

   SELECT Gallery.id, Gallery.gallery_name
   FROM kickoff.galleries 
   GROUP BY Gallery.gallery_name 
   HAVING COUNT(Gallery.gallery_name)=>1

But keep in mind that COUNT() doesn't count NULL values, so if you are counting values by a field that has NULL values, that rows won't be counted by COUNT(). Best option here is to have "NOT NULL" in the table declaration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top