Yii – “Model.id” not defined for CArrayDataProvider and CSqlDataProvider - keyfield - yii

In my controller I want to display all the users of an application so I have used this code:
public function actionClientu($id)
{
$model=$this->loadModel($id);
$appUsers= $model->users;
$dataProvider=new CArrayDataProvider($appUsers);
$this->render('clientu',array(
'dataProvider'=>$dataProvider,
));
}
but I have this error:
"Undefined AppUser.id"
it refer to this line:
$dataProvider=new CActiveDataProvider('AppUser', $appUsers);
Any help please ?

I solved my problem ! here is the cause of the problem:
my model keyfield isn't named id and I haven’t defined which column should be used.
So I just have to set the keyField like this:
new CArrayDataProvider($users, array('keyField' => 'user_id'));
new CSqlDataProvider($users, array('keyField' => 'user_id'));

Related

Yii - Calling api request within CConsole

I have the following issue:
I have a local db with comments and I need to do some actions with some users I'm getting via an API request.
Code is something like this:
class RunCronCommand extends CConsoleCommand {
public function actionIndex() {
...
$comments = Comment::model()->findAll('status = :status', array(':status' => Comment::ACTIVE));
foreach ($comments as $comment) {
$profile = Yii::app()->api->get('/users/'. $comment->user_id . '/getProfile');
}
...
}
When I execute the command I'm getting this error
exception 'CException' with message 'Property "CConsoleApplication.api" is not defined.' in /var/www/core/trunk/common/lib/Yii/base/CComponent.php:131
Any thoughts?
thanks in advance!
Thank you for your reply.
Got it fixed by adding the class into console/config/main.php
'components' => array(
'api' => array(
'class' => 'common.extensions.Api.Api'
),
You will have to rethink the design of the application.
From your example, you might want to use Curl, although I think this might be overkill/
To run an action in a controller, you can use something like
Yii::import('application.modules.moduleName.controllers.ControllerName');
$controller_instance = new ControllerName("Default");
$controller_instance->actionIndex();
Look here for additional information.

Modelname and its behaviors do not have a method or closure named "getData". in yii

i dont know where am i going wrong i did play round with it but did not achieve anything.
i wanna display data using cListview but unable to do so
i have a function in model
Model
public function psearch1()
{
$name=$_GET['search'];
$criteria=new CDbCriteria;
$criteria->alias="t";
$criteria->select="t.id,t.name,t.model";
$criteria->condition='name LIKE "%'.$name.'%"';
return new CActiveDataProvider($this,array('criteria'=>$criteria,));
}
controller
public function actionPsearchindex()
{
$dataProvider=new Modelname('psearch');
$this->render('psearchindex',array(
'dataProvider'=>$dataProvider,
));
}
view
psearchindex
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_psearchindex1',
));
_psearchindex
echo CHtml::encode($data->name);
when i execute i get the following
error
Modelname and its behaviors do not have a method or closure named "getData".
this is my first attempt am doing so but unable to figure out whats wrong
a new model is not a data provider, call your custom search on it
public function actionPsearchindex()
{
//$dataProvider=new Modelname('psearch');// a new model is not a data provider
$model = new Modelname('psearch');
$this->render('psearchindex',array(
'dataProvider'=>$model->psearch1(), // this will give you a data provider that you can use
));
}

Two Different Search Functions

I have two search functions. One for public and one for admin search. They are exactly the same:
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
//$criteria->compare('ID',$this->ID);
$criteria->compare('t.IDkorisnik',Yii::app()->user->getId());
$criteria->compare('start_time',$this->start_time,true);
$criteria->compare('end_time',$this->end_time,true);
//$criteria->compare('information',$this->information,true);
$criteria->compare('country',$this->country,true);
$criteria->compare('city',$this->city,true);
$criteria->compare('start_price',$this->start_price);
$criteria->compare('min_bid',$this->min_bid);
$criteria->compare('valuta',$this->valuta,true);
$criteria->compare('title',$this->title,true);
$criteria->with=array('relationIDuzgajivac','relationIDgolub');
$criteria->compare('username',$this->uzgajivacSearch);
$criteria->compare('brojgoluba',$this->golub_search, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>50),
));
}
except in search() I have this
$criteria->compare('t.IDkorisnik',Yii::app()->user->getId());
in publichsearch() I don't.
As you may see there is relation search. when I open admin page it works in publicsearch page it doesn't work, I don't know why
I never realized that when I'm creating new action
public function actionPublicSearch()
{
$model=new Auction('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Auction']))
$model->attributes=$_GET['Auction'];
$this->render('publicsearch',array(
'model'=>$model,
));
}
this $model=new Auction('search'); within model always has to be 'search' not something like 'public_search'
and then within view, where CGridView is created
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'auction-grid',
'dataProvider'=>$model->public_search(),
dataProvider is a place where I put my search function, in this case public_search

Yii cgridview using dynamic active record model

I'm using the Yii-dynamic-active-record model class to get and put data to various database tables. Now I'd like to use cgridview to quickly build some views of this data, but I'm getting hung-up on writing a search function for this class.
I know that I could always do a foreach loop within the view and make my own html table, but if I could get the Yii way working that would be great for DRY.
*All the db tables have a ID column and this is the only column that really needs to be displayed... if you could get all the columns that would just be bonus points.
Thanks :)
Model
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('ID',$this->ID);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
View
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'entry-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'ID',
),
));
Controller
$tablename=$_GET['tname'];
$table=Entry::forTable($tablename);
$model=$table->findAll();
if(isset($_GET['Entry']))
$model->attributes=$_GET['Entry'];
$this->render('all',array(
'model'=>$model,
));
I am a little confused by what you have done here, however I think I have a solution. First off, in line 3 of your controller code, you call $model = $table->findAll(); this is likely not what you intended, as it will provide you with an ActiveRecord[]. What you want to do is to pass your $table into the gridview. So your controller code would look like this:
$tablename=$_GET['tname'];
$model=Entry::forTable($tablename);
if(isset($_GET['Entry']))
$model->attributes=$_GET['Entry'];
$this->render('all',array(
'model'=>$model,
));
you then need to pass in the table name to the CActiveDataProvider in your model:
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('ID',$this->ID);
return new CActiveDataProvider($this->tableName(), array(
'criteria'=>$criteria,
));
}
now for the "yucky" part, you need to edit the static model method of CActiveRecord to this:
if(isset(self::$_models[$className]))
return self::$_models[$className];
else
{
if(class_exists($className)){
$model=self::$_models[$className]=new $className(null);
$model->_md=new CActiveRecordMetaData($model);
$model->attachBehaviors($model->behaviors());
}
else{
$model = Entry::forTable($className);
}
return $model;
}
This tells it to load from the Entry table when a class of that name does not exist. This practice is discouraged, however its the only way I see of getting around your issue.

How to make an attribute case insensitive in Yii

I have an attribute called 'GroupzDescription'. The column name in DB is groupzdescription. This ambiguity is creating a CException-Property "Apartment.GroupzDescription" is not defined.. when it executes the following lines -
<?php echo $form->textField($model_apt,'GroupzDescription',array('size' => 60, 'maxlength' => 250)); ?>
How can I solve this to make this attribute case insensitive.
Solved.
In the model class, I added the below code
public function getGroupzDescription()
{
return $this->groupzdescription;
}
public function setGroupzDescription($groupzdescription)
{
$this->groupzdescription = $groupzdescription;
}
This seems to be working.