Моя модель:
RetailItem:
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(
'retailItemDetail' => array(self::BELONGS_TO, 'Item', array('item_id' => 'id')),
);
}
Мой взгляд:
$criteria = new CDbCriteria();
//$criteria->condition= "item_id = $id";
$items = RetailItem::model()->findAll($criteria);
CVarDumper::dump($items[0],3,true);
И результат:
......
[relations] => array()
......
Почему array()
пуст?
Вот пример:
//sql
CREATE TABLE transaction (
id INT(11) NOT NULL AUTO_INCREMENT,
userId INT(11) NULL DEFAULT NULL,
PRIMARY KEY (id)
)
COLLATE=utf8_general_ci
ENGINE=InnoDB;
CREATE TABLE user (
id INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
)
COLLATE=utf8_general_ci
ENGINE=InnoDB;
INSERT INTO user (id) VALUES (1);
INSERT INTO user (id) VALUES (2);
INSERT INTO transaction (id, userId) VALUES (2, 1);
INSERT INTO transaction (userId) VALUES (1);
//models
class User2 extends CActiveRecord
{
public function tableName()
{
return 'user';
}
//... etc
}
class Transaction2 extends CActiveRecord
{
public function tableName()
{
return 'transaction';
}
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, 'User2', 'userId'),
);
}
//... etc
}
Как использовать:
$test = Transaction2::model()->findAll();
echo '<pre>';
print_r($test[0]->user);
echo '</pre>';
die();