Hey Leute,
ich habe da gerade so ein kleines Problem mit active record, ich habe 2 tabellen erstellt:
1-> DUser (id, username usw.)
2-> DCustomerCompany (id, user_id, name usw.)
wenn ich jetzt nach der Company suche:
public function loadModel($id)
{
$model=DCustomerCompany::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
}
dann bekomme ich zwar die Company, aber das UserObject ist nicht das was in der Datenbank als user_id drin steht, in diesem fall bekomme ich die Id 3 für den User obwohl in der Company die Id 71 steht…
Ist da vielleicht was mit den relationen falsch?
Hier mal die Models:
DCustomerCompany.php:
<?php
/**
* This is the model class for table "d_customer_company".
*
* The followings are the available columns in table 'd_customer_company':
* @property string $id
* @property string $user_id
* @property string $contact
* @property string $name
*/
class DCustomerCompany extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return DCustomerCompany the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'd_customer_company';
}
/**
* @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('user_id', 'length', 'max'=>11),
array('contact, name', 'length', 'max'=>255),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, user_id, contact, name', 'safe', 'on'=>'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(
'user'=>array(self::BELONGS_TO, 'DUser', 'id')
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'contact' => 'Ansprechpartner',
'name' => 'Firmenname',
);
}
/**
* 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.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('contact',$this->contact,true);
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
DUser.php:
<?php
/**
* This is the model class for table "d_user".
*
* The followings are the available columns in table 'd_user':
* @property integer $id
* @property string $username
* @property string $password
* @property string $email
* @property string $name
* @property string $surname
* @property string $weblink
* @property string $updated_at
* @property string $role
*/
class DUser extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return DUser the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'd_user';
}
/**
* @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('username, password, email', 'length', 'max'=>128),
array('name, surname, weblink, role', 'length', 'max'=>255),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, username, password, email, name, surname, weblink, updated_at, role', 'safe', 'on'=>'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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'username' => 'Benutzername',
'password' => 'Passwort',
'email' => 'Emailadresse',
'name' => 'Vorname',
'surname' => 'Familienname',
'weblink' => 'Weblink',
'updated_at' => 'Zuletzt bearbeitet',
'role' => 'Role',
);
}
/**
* 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.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('username',$this->username,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('surname',$this->surname,true);
$criteria->compare('weblink',$this->weblink,true);
$criteria->compare('updated_at',$this->updated_at,true);
$criteria->compare('role',$this->role,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
Wäre klasse wenn mir jemand sagen könnte was falsch läuft.
Danke im Vorraus!
-Seb