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

  • Home
  • SEARCH
  • 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 7079267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:39:03+00:00 2026-05-28T06:39:03+00:00

I am semi-frustrated with this Yii CGridView problem and any help or guidance would

  • 0

I am semi-frustrated with this Yii CGridView problem and any help or guidance would be highly appreciated.

I have two related tables shops (shop_id primary) and contacts (shop_id foreign) such that a single shop may have multiple contacts. I’m using CGridview for pulling records and sorting and my relation function in shops model is something like:

  'shopscontact' => array(self::HAS_MANY, 'Shopsmodel', 'shop_id');

On the shop grid, I need to display the shop row with any one of the available contacts. My attempt to filter, search the Grid has worked pretty fine, but I’m stuck in one very strange problem. The respective grid column does not display the value that is intended.

On CGridview file, I’m doing something like

  array(
    'name' => 'shopscontact.contact_firstname',
    'header' => 'First Name',        
    'value' => '$data->shopscontact->contact_firstname'
    ),

to display the contact’s first name. However, even under circumstances that searching/sorting are both working (I found out by checking the db associations), the grid column comes out empty! 🙁 And when I do a var_dump

 array(
    'name' => 'shopscontact.contact_firstname',
    'header' => 'First Name',
    'value' => 'var_dump($data->shopscontact)'
    ),

The dump shows record values in _private attributes as follows:

  private '_attributes' (CActiveRecord) => 
    array
      'contact_firstname' => string 'rec1' (length=4)
      'contact_lastname' => string 'rec1 lsname' (length=11)
      'contact_id' => string '1' (length=1)

< Edit: >

My criteria code in the model is as follows:

  $criteria->with = array(
    'owner', 
    'states', 
    'shopscontacts' => array(
      'alias' => 'shopscontacts',
      'select' => 'shopscontacts.contact_firstname,shopscontacts.contact_lastname',
      'together' => true
    )
  );

< / Edit >

How do I access the values in their respective columns? Please help! 🙁

  • 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-05-28T06:39:04+00:00Added an answer on May 28, 2026 at 6:39 am

    Hmm, I have not used the with() and together() methods much. What’s interesting is how in the ‘value’ part of the column, $data->shopscontacts loads up the relation fresh, based on the relations() definition (and is not based on the criteria you declared).

    A cleaner way to handle the array output might be like this:

    'value' => 'array_shift($data->shopscontacts)->contact_lastname'
    

    Perhaps a better way to do this, though, would be to set up a new (additional) relation, like this in your shops model:

    public function relations()
    {
      return array(
        'shopscontacts' => array(self::HAS_MANY, 'Shopsmodel', 'shop_id'), // original
        'firstShopscontact' => array(self::HAS_ONE, 'Shopsmodel', 'shop_id'), // the new relation
      );
    }
    

    Then, in your CGridView you can just set up a column like so:

    'columns'=>array(
      'firstShopscontact.contact_lastname',
    ),
    

    Cheers

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

Sidebar

Related Questions

This is semi-programming related. I'm working with more than 1 person on a website.
Okay so i have a semi weridish problem with re.sub. Take the following code:
I have a semi-complicated problem and hoping that someone here will be able to
This is a semi-related question to question to the following question I just raised:
I have a semi-working example that you can look at. This appears to work
I have gotten this semi autogenerated code, but I am uncertain where the Post
I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
This is semi-related to my previous question . As that previous question states, I
I would ask a semi-theorical question about web services and client-server architecture. I have
This is semi-related to this question: How do I get the storage of each

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.