Solved

CakePHP containable behaviour troubles 1.3

Posted on 2010-11-16
5
692 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:
Mark Gilbert 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 49
TCPDF - Create PDF from Form Values and Link to PDF Download 7 24
Wordpress Query 5 25
AJAX Wordpress Not Reading Variable 2 27
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 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