Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CakePHP containable behaviour troubles 1.3

Posted on 2010-11-16
5
Medium Priority
?
729 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
Mark Gilbert earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

610 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