Solved

CakePHP containable behaviour troubles 1.3

Posted on 2010-11-16
5
690 Views
Last Modified: 2012-05-10
Hi, I have a model called Media that belongsTo Artist and hasMany Vote.  I am trying to fetch Vote and Artist data from within the Media model.  I am using containable to try and fetch the Artist and Vote data, without fetching another related model.  With or without the containable behaviour, the find() call does not fetch Vote data.  It happily fetches Artist data, and the other related Model I haven't mentioned.  When using containable it doesn't fetch the other Model (so containable is working on some level).

Here is the call to find() from within media.php (the model file).
 
$votes = $this->find('all',
	    array(
                'contain'=>array('Artist','Vote'),
		'fields'=>array('Media.artist_id','COUNT(*) AS votes'),
		'conditions'=>
		    array(
			"Vote.created BETWEEN '" . $start . "' AND '" . $end . "'",
			'Artist.department_id' => $department
		    ),
		    'group'=>'Media.artist_id',
		    'order'=>array('votes DESC'),
		)
								);

Open in new window


Media has actAs = array('Containable');, and var $belongsTo = 'Artist';, and var $hasMany = 'Vote';.  Vote has var $belongsTo = 'Media';.

The SQL that is generated:
SELECT `Media`.`artist_id`, COUNT(*) AS votes, `Media`.`id` FROM `media` AS `Media` LEFT JOIN `artists` AS `Artist` ON (`Media`.`artist_id` = `Artist`.`id`) WHERE `Vote`.`created` BETWEEN '2010-11-08 00:00:00' AND '2010-11-14 23:59:59' AND `Artist`.`department_id` = 1 GROUP BY `Media`.`artist_id` ORDER BY `votes` DESC

Open in new window


Obviously I get the error "Unknown column 'Vote.created' in 'where clause'".

Can anyone help me out?

Thanks
 
0
Comment
Question by:willevans
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
ingwa earned 500 total points
ID: 34365313
Should it not rather be something like:

$votes = $this->find('all',
	    array(
                'contain'=>array('Artist','Vote'),
		'fields'=>array('Media.artist_id','COUNT(*) AS votes'),
		'conditions'=>
		    array(
			"votes.created BETWEEN '" . $start . "' AND '" . $end . "'",
			'Artist.department_id' => $department
		    ),
		    'group'=>'Media.artist_id',
		    'order'=>array('votes DESC'),
		)

Open in new window


Reason being is you select the table votes but then call Vote.created so Vote doesn't exist.
0
 

Expert Comment

by:Chavia
ID: 35772801
As far as I know hasMany-Models aren't joined in find calls. They are fetched afterwards.

Try to fetch your records from the Vote Model, containing  Artist and Media models, group by Media.artist_id.
0
 

Expert Comment

by:Chavia
ID: 35772910
Like this:

$votes = $this->Vote->find('all',
	    array(
                'contain'=>array('Media'=> array('Artist') ),
		'fields'=>array('Media.artist_id','COUNT(*) AS votes'),
		'conditions'=>
		    array(
			"Vote.created BETWEEN '" . $start . "' AND '" . $end . "'",
			'Artist.department_id' => $department
		    ),
		    'group'=>'Media.artist_id',
		    'order'=>array('votes DESC'),
		)
								);

Open in new window

0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 36042324
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question