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 8812749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:40:31+00:00 2026-06-14T03:40:31+00:00

How can I in YII get the related model for each time it has

  • 0

How can I in YII get the related model for each time it has been associated to a given model or at least get a count for how many times it has happened?

The problem:
I have a table of Orders and a table of Products.
Each product is associated to the order 0 or more times.

How do I through the standard relational tool in YII tell how many times a product is on an order?
I have created a many-to-many relationship through a joint-table and use it to declare my relations

'products'=>array(self::MANY_MANY, 'Product', 'index_order_products(order_id, product_id)')

Problem is when I call $order->products it only returns the unique products, not a product for each time it is related.

I only need to know how many there are of each product on the order, so I’ve tried

'products'=>array(self::MANY_MANY, 'Product', 'index_order_products(order_id, product_id)', 'select'=>'*, COUNT(product_id) as count', 'group'=>'product_id')

But if I then try calling $order->products[0]->count it says count hasn’t been defined.
I’ve managed to circumvent this by creating a empty column count in the database. This way I get the number I need, but surely there must be a better way.

What would be the correct way of doing something like this in YII?

  • 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-14T03:40:33+00:00Added an answer on June 14, 2026 at 3:40 am

    Well correct way would be using Statistical Query . It is for these purposes.

    The non elegant way

    But if I then try calling $order->products[0]->count it says count
    hasn’t been defined.

    this is because you are accessing count from object that is not returned when relation was evaluated.
    Use CVarDumper::Dump($order->products) to see what you get in case of query

    or the other way could be count($order->products[0])

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

Sidebar

Related Questions

How can I get this in Yii framework using Chtml::link <a href=# title=Movie 1
I'm trying to get full text search working in Yii and I can't seem
I know this question has been asked numerous number of times, both here and
I am trying to write a validation rule in Yii but can't get the
In yii's CGridView I can get the current row number by using $row .
In Yii there are many different ways to get CSS into your pages. The
One of my last works is Yii based hardware catalog. Each item can be
On CakePHP we can get ALL POST/GET using $this->params On Yii, I am not
How can i pull data from different tables(models) into view in yii . actually
I am using CGridView in Yii, how can I display a single record in

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.