Related models in forms messing up data

Hi all,

I have a form for the Photos model. The Photos model is related to the Stores model like so:

Store Model

public function relations()

{


	return array(


              'photos' => array(self::HAS_MANY, 'Photo', 'storeId'),


              ...

Photo Model

public function relations()


{


	return array(


		'store' => array(self::BELONGS_TO, 'Store', 'storeId'),


	);


}

In the Photos form, I am trying to get a dropdown with the list of all the stores like so:

<div class="row">


	<?php echo $form->labelEx($model,'storeId'); ?>


	[b]<?php echo $form->dropDownList($model,'storeId', 


        CHtml::listData(Store::model()->findAll(), 'id', 'name'); ?>[/b]


	<?php echo $form->error($model,'storeId'); ?>


</div>

What’s happening is that the page rendering breaks down at the bolded line. If I remove the line, the form displays properly. I have a feeling this might be because the store model’s relationship with the photo model, but can’t quite figure out why, or how to fix this.

Can somebody shed some light on what could be happening? Thanks!

Edit: Store instead of Store, as pointed out by Tommy

Edit 2:

I just noticed that if I change it to:

Store::model()->find(), the page is rendered correctly, but the dropdown list is empty!

Did you mean Stores instead of Store. You use both in your post.

/Tommy

Sorry, the model’s called Store. Fixed the post.

Seeing your relations code, i think you are storing the store id in store table as storeId instead of id. also double check if you are storing the store name as ‘name’ in store table.

try this


		[b]<?php echo $form->dropDownList($model,'storeId', 

            CHtml::listData(Store::model()->findAll(), 'storeid', 'name'); ?>[/b]

Hi Mukesh,

No, I’m storing the id in the store table as ‘id’, not ‘storeId’. The store name is also stored as ‘name’. Have I defined the relation incorrectly?

Figured it out!

I temporarily fixed the issue by adding the following code:

$criteria = new CDbCriteria();

$criteria->limit = 10;

$stores = Store::model()->findAll($criteria);

It seems the problem is that the Store table has too many records (more than 35000), which is causing some problems. Maybe this is a memory issue?

Displaying 35k records in a dropdownlist doesn’t make sense. Later you probably may want to use a CJuiAutoComplete widget.

Edit: noticed you are working on a geodistance approach.

/Tommy

Hi Tommy,

The geodistance is only for search results, and the problem I was facing was for user input (when adding a photo, the admin can assign it to any store). I realize a better method would be to get the admin to select a store first (using the store admin grid) and then add photos to it.

Just wanted to make sure I understood the problem I was having better, so that I wouldn’t run into a similar problem later.

Thanks for your help!