Dataprovider Pagesize Problem With Related Models

Hi guys.

In my model’s search() function, I retrieve parent records and their related child records via “with” statements; and I also use “together = true”.

I display these records in a CListView. The CListView’s HTML looks like this (simplified):




<tr>

	<td>	<b>Parent Record Label</b>	</td>

	<td>	<p>Parent Record Data</p>	</td>

</tr>


<tr>

	<td>	<b>Child Record Heading</b> </td>

	<td>	<u>Label Column1</u> </td>

	<td>	<u>Label Column2</u> </td>

</tr>

		

<tr>

	<td>				

	</td>

	<td>

		<?php foreach ($data->relation_to_child_model as $child):?>

			<?php echo $child->data1; ?>	</br>

		<?php endforeach; ?>	

	</td>

	<td>

		<?php foreach ($data->relation_to_child_model as $child):?>

			<?php echo $child->data2; ?>	</br>

		<?php endforeach; ?>	

	</td>

</tr>



The result looks something like this:

Parent Record Label: _____ Parent Record Data

Child Record Heading: ____ Label Column1 ___ Label Column2

___________________________Child Data1 ______ Child Data2

___________________________Child Data1 ______ Child Data2

Parent Record Label: _____ Parent Record Data

Child Record Heading: ____ Label Column1 ___ Label Column2

___________________________Child Data1 ______ Child Data2

___________________________Child Data1 ______ Child Data2

___________________________Child Data1 ______ Child Data2

___________________________Child Data1 ______ Child Data2

___________________________Child Data1 ______ Child Data2

My problem is this:

Let’s say the PageSize = 10 (in dataprovider generation in search() function).

If the data consists of 15 parent records, then CListView will display 10 records with a pager to allow the user to page to the remaining parent records - nothing wrong so far.

However, if the data consists of a parent record with 15 related child records, then CListView still only displays 10 of these records, but WITHOUT showing a pager to allow the user to page to the remaining child records.

Any ideas?

Many thanx.

I think you have to do the lazy loading of the related models.

Please check this wiki.

http://www.yiiframework.com/wiki/428/drills-search-by-a-has_many-relation/

OK, it shows all the records if I take out “together = true”. But then I can’t sort the child records.

So I will have to figure out how to sort the related records in their own query.




<?php

$children = $data->relation_to_child_model(array(

    'order' => 'something ASC',

));

?>

<?php foreach ($children as $child):?>

	<?php echo $child->data1; ?>	</br>

<?php endforeach; ?>	

<?php foreach ($children as $child):?>

	<?php echo $child->data2; ?>	</br>

<?php endforeach; ?>	



Thanx Softark

I got it working by incorporating the ‘order’ clause into the ‘with’ clause; and not using ‘together=true’.




$criteria->with = array(

	'rel_to_child1' => array( 

		'with' => array(

			'rel_to_child2' => array(

				'with' => array(

					'rel_to_child3'=>array(

						'select'=>'child3_field',

						'order'=>'child3_field ASC'

					), 

					'rel_to_child4'=>array(

						'select'=>'child4_field'),

					),

				),

			),

		),

	);