shafiq.rst khan Tue Jan 29 01:17:05 -0500 2013

Subject: Cascade the include

Hi all,
I am new to spark and I need your help
I have following three tables

Advertisements-> id title categarogy_id
Categories-> id name section_id
Sections->id name

Now how can i get the details of the section with the advertisement object by using include


Joe Reymann Wed Feb 06 19:52:31 -0500 2013

The include option in your criteria should name the association for what you want included. In your results, you should see an array of Advertisement Objects. Within each of those Advertisement Objects, you'd have a nested array of elements named the same name as your association.

Easiest way is to print_r or var_dump your results.
$advertisements = Advertisements::find($criteria);
print_r($advertisements);

shafiq.rst khan Thu Feb 07 07:00:36 -0500 2013

I have three tables as show above
and
In Advertisment model i used
static $belongs_to = array(array('categarogy'))
In Categarogy model i used
static $belongs_to = array(array('section'))
static $has_many = array(array('advertisment'))
In Section model model i used
static $has_many = array(array('categarogy '))

I what i am trying to do is
Advertisment::find('all',array('include'=>array('category'))) /****its work****/
But how we get the section model in above find
And
Section::find('all',array('include'=>array('category'))) /****its not work****/
how how we got the result for the above

Antoine Sledge Thu Feb 07 10:37:10 -0500 2013

Make sure you aren't actually using "categarogy" because that's quite a typo.
Also, it's usually just safer to always specify the class and foreign key in the array.
$has_many = array(
array('categarogy ', 'class_name'=>'Category', 'foreign_key'=>'section_id') // MAKE SURE YOU FIX "Categarogy"
)

shafiq.rst khan Fri Feb 08 00:14:03 -0500 2013

Yes Thats the typo mistake Its the categroy.
I used your answer but its still giving error
'Relationship named corporation has not been declared for class

When i am trying to used following
Section::find('all',array('include'=>array('category'))) /****its not work****/

Antoine Sledge Fri Feb 08 01:48:57 -0500 2013

if you get rid of the include, does it still work?

Ideally after you do $section = Section::all();
You can then do foreach($section->categories as $category) .. Also rename it to categories since you have multiple categories for each section.

shafiq.rst khan Fri Feb 08 01:53:44 -0500 2013

Oh Finally figure it out
Section::find('all',array('include'=>array('categories')))
Its take there table name while you call it from the has_many relationship model class object
Thanks for helping

Antoine Sledge Fri Feb 08 01:56:54 -0500 2013

Yay !

If you fill out all of the parameters, it takes whatever the first part of the $has_many array is for the relation.

IE $has_many = array('best_categories','class_name'=>'Category','foreign_key'=>'cat','conditions'=>'best=1');
$has_many = array('worst_categories','class_name'=>'Category','foreign_key'=>'cat','conditions'=>'best=0');

$section->best_categories , $section->worst_categories

shafiq.rst khan Fri Feb 08 02:19:23 -0500 2013

Oh Thats very nice . :-)
Thanks buddy its very nice.

(1-8/8)