如何在 Yii 中渲染 216,912 行城市数据而不挂起?
我在城市下拉菜单方面遇到问题。情况是这样的, 我有3张桌子。即国家、州、城市,我能够生成一个下拉菜单 对于国家、州..我也能够为城市生成下拉菜单,但是问题是,当我将代码推送到生产服务器时,由于内存消耗过多,页面变成白色,这是我的代码
//model
public function fetchCityName()
{
$connection = Yii::app()->db->createCommand("SELECT CityName from gg_t_worldareascities ORDER BY CityName ASC")->queryColumn();
return $connection;
}
//view
<?php
$this->widget('CAutoComplete', array(
'model' => $model,
'attribute' => 'cityID',
'data' => $model->fetchCityName(),
'multiple' => false,
'htmlOptions' => array('size' => 25),
));
?>
I am having a problem with a city dropdown menu. the situation is like this,
I have 3 tables. namely countries, states, cities, I was able to generate a dropdown menu
for Countries, States.. I was able to generate a dropdown menu for Cities also but, the problem is, when I pushed the code to production server, the page turned white due to too much memory consumption, here's my code
//model
public function fetchCityName()
{
$connection = Yii::app()->db->createCommand("SELECT CityName from gg_t_worldareascities ORDER BY CityName ASC")->queryColumn();
return $connection;
}
//view
<?php
$this->widget('CAutoComplete', array(
'model' => $model,
'attribute' => 'cityID',
'data' => $model->fetchCityName(),
'multiple' => false,
'htmlOptions' => array('size' => 25),
));
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有 216,912 个
option
元素,它应该敲响警钟,告诉您有更好的方法:)我可以建议
If you have 216,912
option
elements, it should ring an alarm bell that there is a better way :)May I suggest
我不做 Yii,但是国家、州、城市之间应该有某种关系吧?没有人会选择台北市然后选择美国加利福尼亚州作为国家和州。
对于州和城市,我建议仅在用户选择国家/州后通过 Javascript/AJAX 获取可接受的选择。
I don't do Yii, but there should be some kind of relationships between countries, states, cities right? No one would select city of Taipei then choose CA, U.S.A. as the country and state.
For the States and Cities, I would suggest fetch the acceptable choices via Javascript/AJAX only after the user have selected the Country/State.
请参阅有关在 Yii 中创建依赖下拉菜单的 wiki:
http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown/" yiiframework.com/wiki/24/creating-a-dependent-dropdown/
我认为你最好使用自动完成功能 - 文档在这里:
http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete
它是广泛使用的 jQuery UI Autocomplete。
See this wiki on creating dependent dropdowns in Yii:
http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown/
I think you'd be better going with an autocomplete though - the docs are here:
http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete
It is the jQuery UI Autocomplete which is widely used.