[SOLVED]drop down list

the model is pasted at the midpart of this thread,

here’s my controller and view ,( but I don’t think there’s something wrong with it





<?php


class CompanyController extends Controller

{

	

	private $_member = null;

	/**

	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning

	 * using two-column layout. See 'protected/views/layouts/column2.php'.

	 */

	public $layout='//layouts/column2';


	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

                        'memberContext + create',

		);

	}


	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


	/**

	 * Displays a particular model.

	 * @param integer $id the ID of the model to be displayed

	 */

	public function actionView($id)

	{

		$staffDataProvider = new CActiveDataProvider('staff', array(

			'criteria' => array(

				'condition' => 'CompanyID=:CompanyID',

				'params' =>array(':CompanyID'=>$this->loadModel($id)->CompanyID),

			),

			'pagination' => array(

				'pageSize' => 1,

			),

		));

		

		$this->render('view',array(

			'model'=>$this->loadModel($id),

			'staffDataProvider'=>$staffDataProvider,

		));

	}


	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new Wsmembersdetails;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Company']))

		{

			$model->attributes=$_POST['Company'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->CompanyID));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}


	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Company']))

		{

			$model->attributes=$_POST['Company'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->CompanyID));

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}


	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'index' page.

	 * @param integer $id the ID of the model to be deleted

	 */

	public function actionDelete($id)

	{

		if(Yii::app()->request->isPostRequest)

		{

			// we only allow deletion via POST request

			$this->loadModel($id)->delete();


			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

			if(!isset($_GET['ajax']))

				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

		}

		else

			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

	}


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Company');

		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}


	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$model=new Company('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Company']))

			$model->attributes=$_GET['Company'];


		$this->render('admin',array(

			'model'=>$model,

		));

	}


	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 * @param integer the ID of the model to be loaded

	 */

	public function loadModel($id)

	{

		$model=Wsmembersdetails::model()->findByPk((int)$id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


	/**

	 * Performs the AJAX validation.

	 * @param CModel the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='company-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}

	

	protected function loadMember($member_id)

	{

		if($this->_member === null)

		{

			$this->_member = Member::model()->findByPk($member_id);

			if($this->_member === null)

			{

				throw new CHttpException(404, 'The requested project does not exist.');

			}

		}

		return $this->_member;

	}

	

	public function filterMemberContext($filterChain)

	{

		$member_id = null;

		if(isset($_GET['mid']))

			$member_id = $_GET['mid'];

		else

			if(isset($_POST['mid']))

				$member_id = $_POST['mid'];

				

		$this->loadMember($member_id);

		

		$filterChain->run();

	}

}



_form.php view ( I removed all the codes to see the supposed to be array of cities, but it doesn’t pull the data from db )





<?php var_dump(GgTWorldareascities::model()->findAll()); ?>



Hi sasori,

One thing, I just saw that you have put a table prefix… please make sure that your table name is between brackets in your model name.

FOR EXAMPLE: if i have table gg_t_cities, table name should be {{cities}}

Make sure you surround the table name by brackets WITHOUT the table prefix!!!

I just did at the model




class Worldareascities extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @return Worldareascities the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	public function tableName()

	{

		return '{{worldareascities}}';

	}




plus this code at the _form view




<?php echo $form->dropDownList($model,'City',CHtml::listData(Worldareascities::model()->findAll(),'CityID','CityName')); ?>



result

Which controller action gives you the error? I assume index(), then please post the code of the index.php view file


       /**

         * Lists all models.

         */

        public function actionIndex()

        {

                $dataProvider=new CActiveDataProvider('Company');

                $this->render('index',array(

                        'dataProvider'=>$dataProvider,

                ));

        }



EDIT: if it is actionCreate() or another action please post the corresponding view file e.g. create.php

it’s the CompanyController that’s giving me the error , at the actionCreate I suppose ?

here’s the code for the index view




<?php

$this->breadcrumbs=array(

	'Company Details',

);


$this->menu=array(

	array('label'=>'Create Company Details', 'url'=>array('create','mid'=>1)),

	array('label'=>'Manage Company Details', 'url'=>array('admin')),

);

?>


<h1>Company Details</h1>


<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); ?>




So,

please provide code for model Wsmembersdetails and for view create.php

Marco

i already posted it, I just masked the controller name at this thread, here’s the link

WsmembersdetailsController

here’s the create view




<?php

$this->breadcrumbs=array(

	'Company Details'=>array('index'),

	'Create',

);


$this->menu=array(

	array('label'=>'List Company Details', 'url'=>array('index')),

	array('label'=>'Manage Company Details', 'url'=>array('admin')),

);

?>


<h1>Create Company Details</h1>


<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>



That’s the controller. But in the actionCreate() of the controller you instanciate a Wsmembersdetails model


        public function actionCreate()

        {

                $model=new Wsmembersdetails;

...




Does this model exist? (As you don’t use any properties of this said model in your view, it is probably not the cause for your error.)

as I said, I just masked the controller name, I should have masked that $model = new Company too

( we don’t want posting everything right? , am just being cautious not to post everything even though am asking for help with coding )

Sasori, this is very weird… Yii knows very well that if you put ‘{{worldareascities}}’ and you have a table prefix ‘gg_t_’, it will look for table gg_t_worldareascities. If you have that error means that worldareascities is not in your database. Therefore you must double check that.

By having var_dump(cities::model()->findAll(); not even returning FALSE to the document is also the weirdest thing. I recommend you to double check your tables, your code and let Yii take care of everything at first.

  • Do create models using GII from your tables and go to its default to see whether it works or not.

  • If you think there is a problem with that table without any IDs (as you said) I recommend you create a different table structure (this is what i do with mines):


| City


| ID (integer, autoincrement)

| name

| countryId



| Country


| ID (integer, autoincrement)

| name

| short ---- add as many things as you wish

| phonePrefix

|---------------

Create your models and related as (country -> ‘cities’=>array(self:HAS_MANY, ‘City’, ‘countryId’) and in City self::BELONGS_TO… etc

If you need the country SQL list, let me know and I will forward that to you.

It could be that without an indexed numeric ID, Yii’s having trouble… but I don’t think that is the issue.

I think we have covered all the aspects related to your issue and still, we have not found an answer for such simple task as findAll(). So please, review your DB and consider other approaches in order to achieve your objectives. Maybe you were right about not having indexes or primary keys.

the table does exists, here

and yeah it doesn’t have primary key, and those 3 other gg_t tables above doesn’t have a primary key as well.

I was asked to extract those datas from csv files and i loaded them in those tables, they will be used by other devs, but if you have some sort of list data like that, which is currently arranged with their primary key, can you share it to me please ?

  1. cities around the world = 216,912 rows

  2. countries around the world = 249 rows

  3. states around the world= 3,994 rows

Hi buddy,

If you have that data already in the DB I recommend you to just change its correspondent field to primary :), great DB you have (I will check in my resources whether I have that or not -countries for sure)

If you have gg_t_worldareascities then my friend, check your main.php config file if ‘db’ has this:

‘db’=>array(

 'tablePrefix' =&gt; 'gg_t_', &lt;--- &#33;&#33;&#33;

then in your model this should work: ‘{{worldareascities}}’

PS: I have checked my resources, I have a database with countries and its capitals… not all cities and states.

Nice resource: http://www.geodatasource.com/cities-free.html

thanks for the share, I have seen that, but that’s not enough data, I have seen that before, I’ll stick with my existing… here’s what i did now,

  1. added primary keys to all 4 tables

  2. added constraint with their corresponding foreign keys and referencing tables

and guess what,

this worked with the countries drop down




<?php echo $form->dropDownList($model,'Country',CHtml::listData(Worldareascountries::model()->findAll(),'CountryID','CountryName')); ?>



see

and this one works with the states




echo $form->dropDownList($model,'Region',CHtml::listData(Worldareasstates::model()->findAll(),'StateID','StateName')); ?>



see

but the cities doesn’t work, it turns the page white, lol … could it be possible that it sucks all the memory of my machine that’s why this is happening ?

Great we found the problem. I will check into the guts of Yii for that, I was following this post for that reason and it seems we were right.

If you have, how many cities?, better get your hands on http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete

take care… glad you found the problem

I have a total of 216,912 cities

…what’s CJuiAutoComplete link you posted ?

is this the one that will allow me to e.g

  • when I select a country, it’ll automagically only show the corresponding states or cities on the other dropdowns ?

-if so, can you give me a sample code snippet ?

CjuiAutocomplete is a textbox that will authomatically filter while user write in it.

For dependend dropDown see this wiki.

About the city, yes, the problem was the memory usage.

There have been lot of discussion about the fact that ActiveRecord costs a lot in terms of memory, I think that is impossible to query out 216,912 AR.

Create dependend dropdown or use cjuiAutocomplete. In your case, I think that the best solution is to use 2 depended dropdown for states and region and an autocomplete for cities, because cities can be a lot in a region.

My congrats for have solved your problem, now we will remember to consider the size of the query among the possible causes of white page.

Thanks to both of you for not leaving me behind with a huge Question mark on the face hehe.

can you give me a sample snippet of that

cjuiAutocomplete

here http://www.yiiframework.com/forum/index.php?/topic/10000-cjuiautocomplete-sourceurl/page__view__findpost__p__51479