Yii: не могу получить отношения

1

Моя модель:

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() пуст?

Теги:
condition
yii

1 ответ

0

Вот пример:

//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();
  • 0
    Извините, я хочу получить некоторые данные, такие как $ items [0] -> retailItemDetail-> description. Но я не могу. Я не знаю, что с ним не так, потому что я пытаюсь скопировать отношения в другую модель и работать правильно.
  • 0
    item_id это столбец в RetailItemDetail , id это столбец в Item это правильно?
Показать ещё 5 комментариев

Ещё вопросы

Сообщество Overcoder
Наверх
Меню