Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8341535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:30:50+00:00 2026-06-09T05:30:50+00:00

For storing user-defined bookmarks on some site, I have a table with the composite

  • 0

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:

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"),
    );
}

and a primaryKey() method::

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:30:51+00:00Added an answer on June 9, 2026 at 5:30 am

    Your issue may be solved in this way:

    1. Add a surrogate PK to the Bookmark table (e.g. auto-incremental sequence).

    2. Remove primaryKey() function.

    You may also use more convenient code:

    public function relations()
    {
        return array(
            //...
            'wholeBook' => array(self::BELONGS_TO, 'Book', 'book_id', 'on'=>"page_id IS NULL", 'joinType'=>'INNER JOIN'),
            //...
        );
    }
    

    Then in the controller just:

    $bookmarks = Bookmark::model()->with('wholeBook')->findAllByAttributes(array('user_id'=>1));
    

    As a matter of fact, with UNIQUE key instead of PRIMARY and primaryKey() you use hack to evade impossibility of using NULL column in composite PKs.
    ActiveRecord is a sort of ORM, so any logic in SQL must be translated into AR (Yii automatically loads DB schemas), and it must be correct logic.

    If I were you I’d normalize SQL to something like this:
    Normalized DB structure

    Because of those are two different types of bookmarks with different relations. You should join them only in view logic, not in relation structure. IMHO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure which aims at storing user defined data (i.e. from a
I am using mysql and have a table (Log_Table) storing user login dates, a
Are there known issues with storing user defined types within index organized tables in
I have 2 tables - one storing user information (id, username, password) and the
I have a global user defined type fooin a dll that is responible for
I am trying to add a user-defined plane object to a priority queue: int
Have anyone tried to import user defined classes in jasper report (.jrxml file)? I
I'm storing a sensitive, 16 character user string in a MySQL table via the
I have a function that takes the input of a user defined string and
I have a function that takes the input of a user defined string and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.