Hi, wise people! For storing user-defined bookmarks on some site, I have a table with the composite key:
CREATE TABLE bookmarks (
user_id int not null,
book_id int not null,
page_id int,
...
);
CREATE UNIQUE INDEX ON bookmarks(user_id, book_id, page_id);
Note, that page_id can be NULL, and user_id and book_id can not. When page_id is null, the bookmark is set for the whole book, otherwise - for certain page.
Corresponding ActiveRecord class defines some relations and a primaryKey() method::
public function relations() {
return array(
"user" => array(self::BELONGS_TO, "User", "user_id"),
"book" => array(self::BELONGS_TO, "Book", "book_id"),
"page" => array(self::BELONGS_TO, "Page", "page_id"),
);
}
public function primaryKey() {
return array("user_id", "book_id", "orig_id");
}
Now I want to get all bookmarks for the whole book for some user. So, I do:
$bookmarks = Bookmark::model()->findAll(array(
"condition" => "t.user_id = :user_id AND t.page_id IS NULL",
"params" => array(":user_id" => 1),
));
It works great, returning 4 records, but obviously, I want to use some related data from books table:
$bookmarks = Bookmark::model()->findAll(array(
"with" => "book",
"condition" => "t.user_id = :user_id AND t.page_id IS NULL",
"params" => array(":user_id" => 1),
));
and now I get 0 records (count($bookmarks) == 0), although the generated SQL statement selects all needed data, it is just not recognised by CActiveRecord class. Another weird thing is, that when I try to fetch all page bookmarks, everything is okay:
$bookmarks = Bookmark::model()->findAll(array(
"with" => "book",
"condition" => "t.user_id = :user_id AND t.page_id IS NOT NULL",
"params" => array(":user_id" => 1),
));
What am I doing wrong? How to make expression in the second example return some data? PHP 5.4.0, Yii 1.1.8, PostgreSQL 9.1.4, +32°C outside.