Changes
Title
unchanged
Common Yii questions
Category
unchanged
FAQs
Yii version
unchanged
Tags
unchanged
yii, common, questions, beginners
Content
changed
[...]
return false;
}
```
if you don't want it to validate just do **$method->save(false);**
Other possible reason is that "beforeSave" method (if overridden) doesn't return "true".
<hr/>
### How to connect to database in controller?
#### Answer:
The best approach is to set db con
enection in your main.php config.
The second one would be just using the CDbConnection::__construct[...]
#### Answer:
First of all you should always read the error...
The problem is actually indeed that you trying to use a function on some variable that is not an object, and because it is not an object - it doesn't hasve the method you want to use!
so uUse **var_dump** or **echo CVarDumper::dump($param, 10, true);
** or **print_r** ,
TOto understand why it is not an object!
fFor example if you tried to get a user via active record it can be null if it
idoes not exist
s, you need to check it...
<hr/>
### Why Form /
ActiveForm
is not doing ajax validation ?
#### Answer:[...]
```php
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'
top-websites-cr-form
-id',
'enableAjaxValidation'=>true,
'clientOptions' => array(
'validateOnSubmit'=>true,
'validateOnChange'=>true,
'validateOnType'=>false,
),
)); ?>
```[...]
If you create your own form, and trying to do this... better create CRUD with gii, and see how it is done there!
yYou need to echo the validation results if this is an ajax request!
viaBy using:
```php
echo CActiveForm::validate($model);
```
<hr/>
### How to use find
() / findByAttributes
() etc.
#### Answer:[...]
```
oOther way is
to specify [CDbCriteria](http://www.yiiframework.com/doc/api/1.1/CDbCriteria "http://www.yiiframework.com/doc/api/1.1/CDbCriteria")
.
<hr/>[...]
```php
Yii::app()->db->commandBuilder->createUpdateCommand(
'{{table}}',
'{{table}}',
array(
'counter' => new CDbExpression( 'counter + :counter' , array(':counter'=>1))
),
new CDbCriteria(
array(
"condition" => "id = :id" ,
"params" => array(
'counter'"id"=>1
)
)
)
)->execute();
```
<hr/>
### How to use model()->find() / model()->findAll()
#### Answer:
There are three ways:<br>
First CdbCriteria:
```php
$criteria =
> new CDb
Expression( 'counter + :counter' , array(':counter'=>1))
),
new CDbCriteria(array(
"condition" => "id = :id" ,
"params" => array(
"id"=>1
)
))
)->execute(Criteria();
$criteria->compare('first_name', 'Paul');
$criteria->compare('last_name', 'Smith');
$users = User::model()->findAll($criteria);
```
Second:
```php
User::model()->findAll('first_name=? AND last_name=?', array('Paul', 'Smith'));
```
Third:
```php
User::model()->findAllByAttributes(array('first_name'=>'Paul', 'last_name'=>'Smith'));
```
The result will be collection of models, to get an array you can use DAO or QueryBuilder
<hr/>
### Property ClassName.xxx is not defined , what is the problem?
#### Answer:
First of all, actually the error message say all what you need to know!
This is very common error in component configuration, and widget configuration.
This is not a framework issue!
To solve it, you should make sure that the **ClassName** has a field with the name you are trying to use.
This can be a capital letter issue, or just the property aka field not exists in the class...
Read the class reference to see if there is a property that you want to use, or if this is an extension or widget - open the class file, and see if there is the property and you spelled it well
for example if this is the class:
```php
class Foo{
public $bar = 123;
}
```
And you trying to access **Foo.bor** , it not exists there!
See also: [1](http://www.yiiframework.com/forum/index.php?/topic/16190-property-is-not-defined/ "")
[2](http://www.yiiframework.com/forum/index.php?/topic/8054-property-usersalt-is-not-defined/ "")
[3](http://www.yiiframework.com/forum/index.php?/topic/5209-gathering-tabular-input-property-item-is-not-defined/ "")
[4](http://www.yiiframework.com/forum/index.php?/topic/3452-property-productcontroller-locale-is-not-defined/ "")
[5](http://www.yiiframework.com/forum/index.php?/topic/2378-property-cmysqltableschema-primarykey-is-not-defined/ "")
<hr/>
### Trying get uploaded file via CUploadedFilee::getInstanceByName() and getting null every time
#### Answer:
I will just show you working example:
```php
//this is how to get the file
$file = CUploadedFile::getInstanceByName('data');
//upload form
CHtml::beginForm('', 'post',array('enctype'=>'multipart/form-data'));
//the file field
CHtml::fileField('data', '');
```
<hr/>
### what's the difference between exit and Yii::app()->end()
#### Answer:
Both terminates the application. But end method calls onEndRequest event before doing the exit...
In this case as in a lot of others the source code explains itself very-well
```php
public function end($status=0, $exit=true)
{
if($this->hasEventHandler('onEndRequest'))
$this->onEndRequest(new CEvent($this));
if($exit)
exit($status);
}
```