Error In Findbypk()

Hi guys,

I’m trying using findbyPk() but return error:

CDbException

The value for the column "id" is not supplied when querying the table "fd_cargos".

C:\wamp\www\yii\framework\db\schema\CDbCommandBuilder.php(707)

code:

public function actionExcluir(){

$model = Cargos::model()->findByPk(113);

}

hola, cual es la estructura de tu tabla cargos ?

<?php

/**

  • This is the model class for table "fd_cargos".

  • The followings are the available columns in table ‘fd_cargos’:

  • @property integer $id

  • @property string $cargo

  • @property integer $fd_niveis_id

  • @property integer $protegido

  • @property string $status

  • @property string $data_cadastro

*/

class Cargos extends CActiveRecord

{

/**


 * Returns the static model of the specified AR class.


 * @return Cargos the static model class


 */


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}





/**


 * @return string the associated database table name


 */


public function tableName()


{


	return 'fd_cargos';


}





/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(





		array('protegido', 'numerical', 'integerOnly'=&gt;true),


		array('fd_niveis_id','required'),


		array('cargo', 'length', 'max'=&gt;255),


		array('status', 'length', 'max'=&gt;7),


		array('data_cadastro', 'safe'),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('id, cargo, fd_niveis_id, protegido, status, data_cadastro', 'safe', 'on'=&gt;'search'),


	);


}





/**


 * @return array relational rules.


 */


public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'fd_niveis' =&gt; array(self::BELONGS_TO, 'Niveis', 'fd_niveis_id'),


		'fd_operadores' =&gt; array(self::HAS_MANY, 'Operadores', 'fd_cargos_id'),


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'Id',


		'cargo' =&gt; 'Cargo',


		'fd_niveis_id' =&gt; 'Fd Niveis',


		'protegido' =&gt; 'Protegido',


		'status' =&gt; 'Status',


		'data_cadastro' =&gt; 'Data Cadastro',


	);


}








public function scopes(){


    return array(


    	'ativo'           =&gt; array('condition'=&gt;'status = &quot;ativo&quot;'),


    	'nao_protegido'   =&gt; array('condition'=&gt;'protegido = false'),


        'asc_cargo'       =&gt; array('order'=&gt;'cargo ASC'),


    );


}














public function addCargo(&#036;model){





	if( &#33;&#036;model-&gt;save() ){


		throw new CDbException(&quot;Error&quot;);


	}else{


		return false;


	} 





}

















/**


 * Retrieves a list of models based on the current search/filter conditions.


 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.


 */


public function search()


{


	// Warning: Please modify the following code to remove attributes that


	// should not be searched.





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id);





	&#036;criteria-&gt;compare('cargo',&#036;this-&gt;cargo,true);





	&#036;criteria-&gt;compare('fd_niveis_id',&#036;this-&gt;fd_niveis_id);





	&#036;criteria-&gt;compare('protegido',&#036;this-&gt;protegido);





	&#036;criteria-&gt;compare('status',&#036;this-&gt;status,true);





	&#036;criteria-&gt;compare('data_cadastro',&#036;this-&gt;data_cadastro,true);





	return new CActiveDataProvider('Cargos', array(


		'criteria'=&gt;&#036;criteria,


	));


}

}

when I use: Cargos::model()->getPrimaryKey() it returns me the id incorrect.

Array ( [id] => [fd_niveis_id] => )

Did you define ‘id’ as primary key of table “fd_cargos” in MySQL level?

Yes, see the attached print.

Hope its multiple primary key issue.

Check this

http://www.yiiframework.com/forum/index.php?/topic/15528-cactiverecord-strange-issue/

Yes, a have multiple primary key in Cargos table.

I tried to use it:

public function primaryKey(){


    return 'id';


}

but it did not work.

Why use multiple pk? Make it as foreign key relationship…